Dear team,
We are extracting images from docx using aspose jave, how to save images in pdf and we are using below source code to extraction
source code ;
NodeCollection shapes = interimdoc.getChildNodes(NodeType.SHAPE, true);
int imageIndex = 1;
for (Shape shape : (Iterable<Shape>)shapes)
{
if (shape.hasImage())
{
String imageFileName = MessageFormat.format("File.ExtractImages.{0}{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
shape.getImageData().save(folderPath + imageFileName);
imageIndex++;
}
}
input document : Revised Manuscript (3).docx (1.4 MB)
please do needful
@e503824 You can use code like the following. It create an intermediate document for each shape, imports the shape into this document and then saves the document as PDF:
Document doc = new Document("C:\\Temp\\in.docx");
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
int imageIndex = 1;
for (Shape shape : (Iterable<Shape>)shapes)
{
if (shape.hasImage())
{
// Create an intermediate document to where shape will be imported to.
Document itermDoc = (Document)doc.deepClone(false);
// use section imported from the source document to keep the same page size and orientation.
itermDoc.appendChild(itermDoc.importNode(
shape.getAncestor(NodeType.SECTION),
false,
ImportFormatMode.USE_DESTINATION_STYLES));
// Add required nodes since we did not import child nodes from the source document.
itermDoc.ensureMinimum();
// Import shape and put it into the document.
Node importedShape = itermDoc.importNode(shape, true, ImportFormatMode.USE_DESTINATION_STYLES);
itermDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);
// Save as PDF.
itermDoc.save("C:\\Temp\\out_" + imageIndex + ".pdf");
imageIndex++;
}
}