TabStop Leader is Lost after Updating TOC Field using Java

Hello,

In the test document, I try to force the paragraph in the table of contents to have ownership
“setKeepTogether” set to true.

Steps to reproduce the expected result:
1.Create some texts as with the Header (1-9) style set
2. Create tables with 1 row
3. Create the TOC in the table
Expectations: After updating TOC, the table stops cannot disappear.

code:

        final Table tableContainer = builder.startTable();
        final Cell cell = builder.insertCell();
        cell.getCellFormat().setFitText(false);
        // set width, height table etc ...
        final String newStyleName = "CustomTOC".concat(String.valueOf(r.nextInt(2_000_000)));
        Style appliedStyle = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, newStyleName);
        appliedStyle.getParagraphFormat().setKeepTogether(true);
        builder.getParagraphFormat().setStyleName(newStyleName);
        final FieldToc field = (FieldToc) builder.insertTableOfContents("\\o \\h \\z \\u");
        field.setPreserveLineBreaks(true);
        builder.endTable();


@aolo23

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • 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.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Code:

    public static void disappearingTabStopsAfterTocUpdate() {
        try {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);


            final Table tableContainer = builder.startTable();
            final Cell cell = builder.insertCell();
            cell.getCellFormat().setFitText(false);
            final Paragraph firstParagraph = cell.getFirstParagraph();

            Run run = new Run(builder.getDocument(), "TOC title");
            Font font = run.getFont();
            font.setSize(20);
            font.setBold(true);
            font.setColor(new Color(50,87,65));
            firstParagraph.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
            firstParagraph.appendChild(run);


            final PreferredWidth preferredWidth = PreferredWidth.fromPoints(ConvertUtil.millimeterToPoint(190));
            final StyleCollection styles = builder.getDocument().getStyles();

            TableStyle tableStyle = (TableStyle) styles.add(StyleType.TABLE, "TabStyle");
            tableStyle.setAllowBreakAcrossPages(false);
            tableStyle.setAllowBreakAcrossPages(false);
            tableStyle.setBottomPadding(0);
            tableStyle.setLeftPadding(0);
            tableStyle.setRightPadding(0);
            tableStyle.setTopPadding(0);
            tableContainer.setStyle(tableStyle);
            tableContainer.setPreferredWidth(preferredWidth);
            tableContainer.setAbsoluteVerticalDistance(ConvertUtil.millimeterToPoint(10));
            tableContainer.setAbsoluteHorizontalDistance(ConvertUtil.millimeterToPoint(10));

            final String newStyleName = "CustomTOC1";
            Style appliedStyle = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, newStyleName);
            appliedStyle.getParagraphFormat().setKeepTogether(true);
            appliedStyle.setBaseStyleName("TOC 1");

            appliedStyle.getFont().setColor(new Color(123, 56, 14));
            builder.getParagraphFormat().setStyleName(newStyleName);
            builder.getParagraphFormat().setKeepTogether(true);
            final FieldToc field = (FieldToc) builder.insertTableOfContents("\\o \\h \\z \\u");
            field.setPreserveLineBreaks(true);

            builder.endTable();
            builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
            builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
            builder.writeln("Heading 1.1");
            builder.writeln("Heading 1.2");
            builder.writeln("Heading 1.3");
            builder.writeln("Heading 1.4");

            doc.updateFields();
            doc.save(System.getProperty("user.dir") + File.separator + "example.docx");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Explains:

The above code generates the document with the table of contents. It is enough to click “update field” and the characters separating the content of the paragraph from the numbering disappear.
From what I checked it is related to the lack of setting
“keep together” for the paragraph containing the TOC.

@aolo23

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-21564. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-21564) have been fixed in this Aspose.Words for .NET 21.2 update and this Aspose.Words for Java 21.2 update.