We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Document Modification does not reflect in output PDF using Java

I observe strange behavior of LayoutCollector when saving Word document to PDF.

Given WORD document structure (it’s just working example, the exact structure does not matter):

  • Document
    • Section
      • Body
        • Paragraph
          • Shape
          • StructuredDocumentTag (Rich Text)
            • Run

If you call LayoutCollector’s getStartPageIndex for the shape before changing text of the Run node then you will see the text is changed in docx document and no changes in PDF document.

Aspose Words library version: 20.12

I created minimal reproducible code example:

// Set license first.
// Creates paragraph with shape and SDT (Rich Text) nodes inside it.
var wordDocument = new Document();
var body = wordDocument.getSections().get(0).getBody();

var paragraph = new Paragraph(wordDocument);
body.appendChild(paragraph);

var shape = new Shape(wordDocument, ShapeType.TEXT_PLAIN_TEXT);
shape.getTextPath().setText("Image text");
paragraph.appendChild(shape);

var sdt = new StructuredDocumentTag(wordDocument, SdtType.RICH_TEXT, 1);
paragraph.appendChild(sdt);

var layoutCollector = new LayoutCollector(wordDocument);
// Comment out next line to see the difference.
layoutCollector.getStartPageIndex(shape);

var run = (Run) sdt.getChild(NodeType.RUN, 0, true);
run.setText("New RUN text");

wordDocument.save("test.docx");
wordDocument.save("test.pdf", SaveFormat.PDF);

@PapaVadik

Please call Document.updatePageLayout after modifying the document to get the desired output.

wordDocument.updatePageLayout();
wordDocument.save(MyDir + "test.pdf", SaveFormat.PDF);

@tahir.manzoor

Thank you! It works now.

The remark in API docs explains why it is necessary to add updatePageLayout before saving the document to PDF:

This method is automatically invoked when you first convert a document to PDF, XPS, image or print it. However, if you modify the document after rendering and then attempt to render it again - Aspose.Words will not update the page layout automatically. In this case you should call UpdatePageLayout() before rendering again.