Page 1 of 1

Genearting ToC without skipPageNumbers not working

PostPosted: Tue Mar 08, 2022 12:34 am
by mutasDev
Hey, I am using Docx4j in an internship project and when I try to generate a ToC with page numbers it does not work.

Executing the code with
Code: Select all
if (template.isGenerateToc()) {
            Toc.setTocHeadingText("Inhaltsverzeichnis");

            TocGenerator tocGenerator = new TocGenerator(wordPackage);
            tocGenerator.generateToc(2, " TOC \\o \"1-3\" \\h \\z \\u ", true);
}


Works perfectly fine, but results in no page numbers in the .docx (when exporting to PDF via the Gotenberg API (https://gotenberg.dev), the page numbers are existant and correct).

When I try to pass "false" for the third parameter, the application crashes with the following exception/stacktrace:

Code: Select all
org.docx4j.toc.TocException: Exception exporting package
   at org.docx4j.toc.TocGenerator.getPageNumbersMapViaFOP(TocGenerator.java:871)
   at org.docx4j.toc.TocGenerator.getPageNumbersMap(TocGenerator.java:694)
   at org.docx4j.toc.TocGenerator.populateToc(TocGenerator.java:441)
   at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:306)
   at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:234)
   at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:194)



If usefull: This is my TocHeading:
Code: Select all
private static String XML_TOCHeading_BasedOn_Nothing = "<w:style w:styleId=\"TOCHeading\" w:type=\"paragraph\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
            + "<w:name w:val=\"TOC Heading\"/>"
            // + "<w:basedOn w:val=\"Heading1\"/>"  // would be ok if provided already present, since
            + "<w:next w:val=\"Normal\"/>"
            + "<w:uiPriority w:val=\"39\"/>"
            + "<w:semiHidden/>"
            + "<w:unhideWhenUsed/>"
            + "<w:qFormat/>"
            + "<w:pPr>"
            + "<w:keepNext/>"
            + "<w:keepLines/>"
            + "<w:spacing w:after=\"150\" w:before=\"480\"/>"
            + "<w:outlineLvl w:val=\"9\"/>"
            + "</w:pPr>"
            + "<w:rPr>"
            + "<w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" w:cs=\"Arial\"/>"
            + "<w:b/>"
            + "<w:bCs/>"
            + "<w:sz w:val=\"36\"/>"
            + "<w:szCs w:val=\"36\"/>"
            + "</w:rPr>"
            + "</w:style>";




Any ideas on how to fix this exception and generate page numbers in my toc?

Re: Genearting ToC without skipPageNumbers not working

PostPosted: Thu Mar 10, 2022 6:37 pm
by jason
Answered at your cross post at https://stackoverflow.com/questions/713 ... n/71420331

Please don't cross post.