Need to Merge Docx and images

Dear team,

We need to merge docx files and few images using aspose java please find input file

Input File : New Project.zip (600.5 KB)

please share me the source code to merge these files

@e503824 The question is the same as you already asked in another your thread.

Dear team,

Any think possible to share source code for this, we are getting confused

@e503824 You can use code like the following to achieve what you need:

String[] fileNames = new String[] { "Fig 4.jpeg", "Figure1.jpg", "Index.png", "Table 1.docx", "Table 2.docx", "Table 3.docx" };

Document doc = new Document("C:\\Temp\\Title Page.docx");

for (String fileName : fileNames)
{
    String filePath = "C:\\Temp\\" + fileName;

    // Try to detect format of the input file by Aspose.Words.
    FileFormatInfo info = FileFormatUtil.detectFileFormat(filePath);
    // If file format is detected append the document to the main document.
    if (info.getLoadFormat() != LoadFormat.UNKNOWN)
    {
        Document subDoc = new Document(filePath);
        doc.appendDocument(subDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
    }
    // Check whether extension corresponds image format.
    else if (fileName.endsWith(".jpeg") || fileName.endsWith(".jpg") || fileName.endsWith(".png"))
    {
        DocumentBuilder imageDocumentBuilder = new DocumentBuilder();
        imageDocumentBuilder.insertImage(filePath);
        // Append document with image as we do for regular documents
        doc.appendDocument(imageDocumentBuilder.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
    }
}

doc.save("C:\\Temp\\out.docx");

Dear team,

Thanks now working fine

1 Like