Table in header got different size between Word and PDF

I use a Table to fill information in the header of my document.
But I want the tabel wider so that more information can be shown.
But when I save the document as pdf their’s a difference in location and size off the pdf.
(I also miss al line off my header in the example.)
Should I move the table on a other way or is this a bug.

PS I’m using: Aspose.Words (Java) version 16.2.0.0

try
{
    String userprofile = System.getenv("USERPROFILE");
    Document lDocument = new Document(userprofile + "\\Desktop\\A test word document.docx");
    File lNewFileDocument = new File(userprofile + "\\Desktop\\A test word document-edit.docx");
    File lNewFilePdf = new File(userprofile + "\\Desktop\\A test word document-edit.pdf");

    DocumentBuilder lDocumentBuilder = new DocumentBuilder(lDocument);
    Section lCurrentSection = lDocumentBuilder.getCurrentSection();
    com.aspose.words.PageSetup lPageSetup = lCurrentSection.getPageSetup();
    lPageSetup.setHeaderDistance(20d);

    // lDocumentBuilder.moveToHeaderFooter( HeaderFooterType.HEADER_PRIMARY );
    Table lTable = lDocumentBuilder.startTable();

    for (int lRowIndex = 0; lRowIndex < 5; lRowIndex++)
    {
        for (int lColIndex = 0; lColIndex < 3; lColIndex++)
        {
            Cell lCell = lDocumentBuilder.insertCell();
            // Reset de Font en Aligment naar standaard. (De wijzigingen gaan door naar nieuwe cellen)
            lDocumentBuilder.getCellFormat().clearFormatting();
            lDocumentBuilder.getCellFormat().getBorders().clearFormatting();
            lDocumentBuilder.getCellFormat().getBorders().setLineWidth(1.0);
            lDocumentBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
            lDocumentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
            lDocumentBuilder.getParagraphFormat().setSpaceBefore(0d);
            lDocumentBuilder.getParagraphFormat().setSpaceAfter(0d);
            lDocumentBuilder.getFont().clearFormatting();
            lDocumentBuilder.getFont().setName("Verdana");
            lDocumentBuilder.getFont().setSize(8.5d);
            lDocumentBuilder.getFont().setBold(false);
            lDocumentBuilder.getCellFormat().getShading().clearFormatting();

            switch (lColIndex)
            {
                case 0:
                    lCell.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(15d));
                    break;
                case 1:
                    lCell.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(43d));
                    break;
                case 2:
                    lCell.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20d));
                    break;
            }

            // First row, left and middle Merge First
            if (lRowIndex == 0 && lColIndex < 2)
            {
                lCell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
            }
            else if (lRowIndex > 0 && lColIndex < 2)
            { // Second row and further, left and middle merge previous.
                lCell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
            }
            else
            { // else dont merge
                lCell.getCellFormat().setVerticalMerge(CellMerge.NONE);
            }

            /*
            | left | middle | right-0 |
            | | | right-1 |
            | | | {page} |
            | | | right-3 |
            | | | right-4 |
            */

            if (lColIndex == 2)
            {
                lDocumentBuilder.write("right");

                Cell lCellValue = lDocumentBuilder.insertCell();
                // Reset de Font en Aligment naar standaard. (De wijzigingen gaan door naar nieuwe cellen)
                lDocumentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
                lDocumentBuilder.getFont().setName("Verdana");
                lDocumentBuilder.getFont().setSize(8.5d);
                lDocumentBuilder.getFont().setBold(false);
                lCellValue.getCellFormat().setVerticalMerge(lCell.getCellFormat().getVerticalMerge());
                lDocumentBuilder.getCellFormat().getShading().clearFormatting();
                lCellValue.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(22d));

                if (lRowIndex == 3)
                {
                    lDocumentBuilder.insertField("PAGE \\* MERGEFORMAT");
                    // lDocumentBuilder.insertField( "DOCPROPERTY LastSavedTime \\* MERGEFORMAT" );
                }
                else
                {
                    lDocumentBuilder.write(lRowIndex + "");
                }
            }
            else
            {
                if (lRowIndex == 1)
                {
                    if (lColIndex == 0)
                    {
                        lDocumentBuilder.write("left");
                    }
                    else
                    {
                        lDocumentBuilder.write("middle");
                    }
                }
            }//if
        } // for col
        lDocumentBuilder.endRow();
    }
    lDocumentBuilder.endTable();
    // De tabel in de header hetzelfde formaat geven als die van de html.
    // BUG Aspose de header/footer heeft niet dezelfde breedte in Word als bij pdf.
    // Section lCurrentSection = lDocumentBuilder.getCurrentSection();
    // com.aspose.words.PageSetup lPageSetup = lCurrentSection.getPageSetup();
    lTable.setLeftIndent(-lPageSetup.getLeftMargin() + 15); //
    lTable.setPreferredWidth(PreferredWidth.fromPercent(120.5d)); //Voor Word documenten
                                                                  // lTable.setPreferredWidth( PreferredWidth.fromPercent( 110.0d ) ); //Voor PDF gegenereerde

    lDocument.save(lNewFileDocument.getAbsolutePath());
    lDocument.save(lNewFilePdf.getAbsolutePath());
}
catch (Exception ex)
{
    LOG.error("AsposeTest ", ex);
}

Hi Ron,

Thanks for your inquiry. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-13470. Our product team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi Ron,

Thanks for being patient.

Regarding WORDSNET-13470, we believe the issue is with the code you’re using on your end.

The issue occurs because the code inserts a PAGE field using the InsertField() method that immediately attempts to update the field.
The field is inserted during table construction before the last row is added.

PAGE field update requires document layout update. So the layout is built before the table construction is finished and the table has only 4 rows in the layout model. Also, auto table preferred width is used for the layout because table preferred width in percent units is not set yet. Later, the layout model built with an unfinished table is used for saving to PDF.

The issue may be corrected by using InsertField() overload that does not update the field immediately:

// Do not use insertField(string) because it will build the document layout immediately, before the table construction is finished.
// lDocumentBuilder.insertField( "PAGE \\* MERGEFORMAT" );
// Use insertField(string, string) instead.
DocumentBuilder.insertField( "PAGE \\* MERGEFORMAT" , null);

In order to update the field value, Document.UpdateFields() can be used before saving the document, after all the changes to the document model are done. Hope, this helps.

Best regards,

Hi Ron,

Regarding WORDSNET-13470, our product team has completed the work on your issue and has come to a conclusion that this issue and the undesired behavior you’re observing is actually not a bug. So, we have closed this issue as ‘Not a Bug’. Please use the workaround from my previous post.

Best regards,

I’ve tested your workaround and it works as it should.
It also fixed my other issue 719536 (WORDSNET-13438 ).

Using the updateFields was already in my code, so I only had to add the additional parameter.

Thank you for your help.

Jeroen