Issue with Docx conversion to image using Aspose.Words for Java

Hello Team,
we recently procured Aspose.Word for Java and are facing issues with document conversion to image (png/jpeg)

We are performing the below steps to generate JPEG, however, the image produced does not render the text fetched via bookmarks, it only renders the actual content of the docx file.

public static void main(String[] args) throws Exception {
    //ExStart:LoadAndSaveToStream
    //ExStart:OpenFromStream
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(LoadAndSaveToStream.class);
    // Open the stream. Read only access is enough for Aspose.Words to load a document.
    InputStream stream = new FileInputStream(dataDir + "Document.doc");
    // Load the entire document into memory.
    Document doc = new Document(stream);
    // You can close the stream now, it is no longer needed because the document is in memory.
    stream.close();
    //ExEnd:OpenFromStream

    // ... do something with the document
    // Convert the document to a different format and save to stream.
    ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
    doc.save(dstStream, SaveFormat.RTF);
    FileOutputStream output = new FileOutputStream(dataDir + "Document Out.rtf");
    output.write(dstStream.toByteArray());
    output.close();
    //ExEnd:LoadAndSaveToStream

    System.out.println("Document loaded from stream and then saved successfully.");
}

aspose-words/Aspose.Words-for-Java/blob/master/Examples/src/main/java/com/aspose/words/examples/quickstart/LoadAndSaveToStream.java

We are also using Aspose.Words for .Net and the docx is getting converted to image properly using memstream. However, with Java we are facing issue in conversion to image.

Would appreciate your assistance.

Thanks.

Attached is the docx file for your reference.
NewTestSbcDocumnet_90951_1621608133909.docx (211.4 KB)

Strange, other posts are getting replies, this post is not… Aspose team any ETA on when can i get a response?
Thanks.

@avneetsingh

Could you please ZIP and attach the screenshots of problematic sections of output images? Please also share the page numbers of document those have incorrect output. We will investigate the issue and provide you more information on it.

please find the zip file attached with the screen shots and sample code we are using…

SBC Doc Image.zip (3.0 MB)

@avneetsingh

We have tested the scenario using the latest version of Aspose.Words for Java 21.5 with following code example and have not found the shared issue. So, please use Aspose.Words for Java 21.5. We have attached the output images with this post for your kind reference.
Images.zip (2.3 MB)

Document doc = new Document(MyDir + "NewTestSbcDocumnet_90951_1621608133909 (1).docx");
ImageSaveOptions imageoptions = new ImageSaveOptions(SaveFormat.JPEG);
for(int i = 0; i < doc.getPageCount(); i ++)
{
	imageoptions.setPageSet(new PageSet(i));
	doc.save("C:\\tmp\\page_"+i+".jpg", imageoptions);
}

Please note that Aspose.Words requires TrueType fonts when rendering document to fixed-page formats (JPEG, PNG, PDF or XPS). You need to install fonts that are used in your document on the machine where you are converting documents to JPEG. Please refer to the following articles:
Using TrueType Fonts
Manipulating and Substitution TrueType Fonts

We tried changing the version as you have suggested, but we are getting same output as blank document for the values which are being added by the bookmarks…
Just to let you know our steps are as follows:

  1. we creating object from input stream.
  2. then replacing all the bookmarks.
  3. and then writing that doc to multiple images pagewise.
  4. then combining them as one single image for the whole doc…

Step 1: code to load document(it is blank template, which is also attached with this email)

inputStream = loadSbcDocumentTemplate();
int pos;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((pos = inputStream.read()) != -1)
    bos.write(pos);
byte[] dataBytes = bos.toByteArray();
ByteArrayInputStream byteStream = new ByteArrayInputStream(dataBytes);
Document doc = new Document(byteStream);

Step 2: setting bookmark

private void setBookMark(String bookMark, String value, Document doc)
{
    try
    {
        if (!Strings.isNullOrEmpty(value) && value.trim().length() > 5 &&
                (value.substring(0, 4).toUpperCase().equals("WWW.") || value.substring(0, 5).toUpperCase().equals("HTTP:") ||
                        value.substring(0, 6).toUpperCase().equals("HTTPS:") || value.substring(value.trim().length() - 4, value.trim().length()).equals(".COM") ||
                        value.toUpperCase().contains(".COM/")))
        {
            try
            {
                DocumentBuilder docBuilder = new DocumentBuilder(doc);

                docBuilder.moveToBookmark(bookMark);
                docBuilder.insertHyperlink(value, value, false);
            }
            catch (Exception e)
            {
                Bookmark bMark2 = doc.getRange().getBookmarks().get(bookMark);
                bMark2.setText(value);
            }
        }
        else
        {
            Bookmark bMark = doc.getRange().getBookmarks().get(bookMark);
            bMark.setText(value);
        }

    }
    catch (Exception ex)
    {
        log.error("Error while setting BookMark : {} and error message : {}", bookMark, ex.getMessage());
    }
}

Step 3 and 4: Saving the doc as Image and combining them( combineImage is an enternal method which is working fine but the following code of generating page wise image with the data prodcued by the bookmarks as set in step 2 is not getting the value of bookmarks)

if (outputType.equals("IMAGE"))
{
    log.info("region document conversion to image - start");
    BufferedImage finalImage = null;

    List<BufferedImage> imagesToCombine = new ArrayList<>();
    log.info("No. of pages before document conversion to image : {}", doc.getPageCount());
    for (Integer i = 0; i < doc.getPageCount(); i++)
    {
        memStream = new ByteArrayOutputStream();
        log.info("New ByteArray length : {} for JPEG image count : {} ", memStream.toByteArray().length, i);
        ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);

        PageSet pageSet = new PageSet(i);

        options.setPageSet(pageSet);

        log.info("Document Page Information before save JPEG to ByteArrayStream : {}", doc.getPageInfo(i));
        doc.save(memStream, options);

        log.info("JPEG image size : {} of document page no : {} ", memStream.toByteArray().length, i);
        BufferedImage tempImage = ImageIO.read(new ByteArrayInputStream(memStream.toByteArray()));

        log.info("JPEG BufferedImage size : width {} and Height : {} of document page no : {} ", tempImage.getWidth(), tempImage.getHeight(), i);
        imagesToCombine.add(tempImage);
        memStream.close();
    }
    log.info("Before combineBitmap :: Total no. of JPEG images to be combine : {}", imagesToCombine);
    finalImage = combineBitmap(imagesToCombine);

    log.info("After combineBitmap :: Width : {} and Height : {} of final JPEG type of images : {} ", finalImage.getWidth(), finalImage.getHeight(), finalImage.getType());

    imagesToCombine = null;
    outStream = new ByteArrayOutputStream();
    boolean result = ImageIO.write(finalImage, "JPG", outStream);
    log.info("Combine Image lenght : {}", outStream.toByteArray().length);
    toReturn.setSbcDoc(outStream.toByteArray());
    outStream.close();
}

SBCDocumentTemplate2017.docx (202.5 KB)

@avneetsingh

Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue and provide you more information on it.

Hi Tahir,

Issue 1:
We have created a sample Java application according to your suggested code and per aspose docs. You can see the issue as on generated image book marks are not replaced. We saved the word document in docx format to check and found bookmarks are there. You can found those generated docs and images into usr/logs folder.

You may need to apply license to run the app. Otherwise it will work with trial version.

Issue 2:
Also when the word document template change to image whole formatting and font style got change, it should take as template one.

asposeTest 2.zip (321.3 KB)

@avneetsingh

We are working over your query and will get back to you soon.

@avneetsingh

Please check the attached output Word document and output images. Docs.zip (2.0 MB)

The bookmark contents are updated and shown in images. Secondly, the formatting and fonts style are correct. Please check the attached images and let us know if there is still any issue with output documents.

Images are looking good, i can see the font styles are correct also.

Did you make any changes over the sample source code provided by us. Please attach here.

@avneetsingh

We did not change the code. We tested the scenario using the latest version of Aspose.Words for Java 21.5. So, please use Aspose.Words for Java 21.5.

We are also running with Aspose Words for Java 21.5 as you can check in the dependency of aspose in the project we have shared with you… however we are not getting the desired output. Not sure how it is woking for you and not for us. Which Java version you are using.

We are using open jdk 8. If possible can we have a working session for few minute as this has taken a long time to resolve.

image.png (24.0 KB)

@avneetsingh

Please call Document.UpdatePageLayout method before converting document to images.

Moreover, please implement IWarningCallback interface as shown below to check the missing font notification.

doc.setWarningCallback(new IWarningCallback() {
    @Override
    public void warning(WarningInfo warningInfo) {
        if (WarningType.FONT_SUBSTITUTION == warningInfo.getWarningType()) {
            System.out.println(warningInfo.getDescription());
        }
    }
});

If you still face problem, please share your working environment e.g. operating system, complete Java version. We will investigate this issue further and provide you more information on it.