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

Free Support Forum - aspose.com

Different layout between Aspose model and exported Word file

Here’s a code snippet reproducing this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.writeln("First line");
builder.getParagraphFormat().setKeepWithNext(true);
builder.writeln("Table name");

builder.getRowFormat().setAllowBreakAcrossPages(false);
builder.startTable();
for (int i = 0; i < 15; i++) {
  builder.insertCell();
  builder.writeln("Line1");
  builder.writeln("Line2");
  builder.writeln("Line3");
  builder.endRow();
}

builder.insertCell();
builder.writeln("Line1");
builder.getParagraphFormat().setKeepWithNext(false);
builder.endTable();

doc.updatePageLayout();
log.info("Page count in memory: {}", doc.getPageCount());

try (FileOutputStream fos = new FileOutputStream(OUTPUT_PATH)) {
  OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
  saveOptions.setSaveFormat(SaveFormat.DOCX);
  saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
  doc.save(fos, saveOptions);
}

When I open exported file with word, I get 4 pages with both paragraphs on their own pages and then the table on pages 3 and 4:
Screen Shot 2022-05-03 at 16.43.54.png (2.4 KB)

But in memory layout of the document is 3 pages and if visualized in word probably looks like this:
aspose-in-memory.png (2.3 KB)

I’m aware that this is happening because Aspose is using ECMA_376_2006 in memory. But its important for us to have the same layout in memory and PDF exports as you would get after you open the saved file with ISO_29500_2008_STRICT, so two questions:

  1. Is it possible to define compliance when starting from a blank document, so that in this example we get 4 pages in memory layout?
  2. Is there a way to build the document so that its 3 pages and looks exactly how it does in aspose after its saved with ISO_29500_2008_STRICT?

By the way, the objective of ours is to keep tables from breaking as described in: https://docs.aspose.com/words/java/keeping-tables-and-rows-from-breaking-across-pages/

@cernius You can use CompatibilityOptions.optimizeFor to achieve what you need.

Document doc = new Document();
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);

Even after calling

doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);

Aspose still counts that the document is 3 pages

@cernius The following code on my side returns 4 pages:

Document doc = new Document();
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);
DocumentBuilder builder = new DocumentBuilder(doc);

builder.writeln("First line");
builder.getParagraphFormat().setKeepWithNext(true);
builder.writeln("Table name");

builder.getRowFormat().setAllowBreakAcrossPages(false);
builder.startTable();
for (int i = 0; i < 15; i++)
{
    builder.insertCell();
    builder.writeln("Line1");
    builder.writeln("Line2");
    builder.writeln("Line3");
    builder.endRow();
}

builder.insertCell();
builder.writeln("Line1");
builder.getParagraphFormat().setKeepWithNext(false);
builder.endTable();

doc.updatePageLayout();
System.out.println("Page count in memory: " + doc.getPageCount());

try (FileOutputStream fos = new FileOutputStream("C:\\Temp\\out.docx")) {
    OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
    saveOptions.setSaveFormat(SaveFormat.DOCX);
    saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
    doc.save(fos, saveOptions);
}

output:

Page count in memory: 4

Please note that when you update page layout Aspose.Words uses the fonts. If fonts used in the document are not available Aspose.Words substitutes fonts and this might lead into the incorrect page count.