Page 1 of 1

Add table of content and export to PDF

PostPosted: Fri Mar 07, 2014 11:33 pm
by supachamp
I would like to know if it is possible to add a table of content section to the "wordMLPackage" which is then exported into PDF. I know how to add it to the "wordMLPackage" to be shown in the generated Word file. But I did not succeed to actually see a table of content section in the generated PDF file. I generate the PDF as follows:

Code: Select all
         
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(this.wordMLPackage);

OutputStream os;
try
{
  os = new java.io.FileOutputStream("my.pdf");
  Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
}
catch (Exception e)
{
  // TODO Auto-generated catch block
  e.printStackTrace();
}

Re: Add table of content and export to PDF

PostPosted: Thu Mar 13, 2014 1:20 pm
by jason
When you add a TOC to a Word document, you generally do this by adding a TOC field code.

Before you can actually see the TOC in PDF output, that TOC field code needs to be converted to actual TOC entries.

Word can of course do this.

There may be some code floating around to do it using docx4j. If not, it is simple enough to write (except for the page numbers!).

Plutext's Docx4j Enterprise Edition does contain code to generate and update table of content entries, including page numbers. Its not free though. You can download a trial of that from http://www.plutext.com/m/index.php/products

Note that depending what field args you supply, the PDF output may need some enhancement. For example dot leader (.... before page number) is not supported in 3.0.1.

As an alternative, you could try LibreOffice 4.1. It seems to generate TOC entries on open, and include them in exported PDF.

Re: Add table of content and export to PDF

PostPosted: Tue Apr 01, 2014 10:58 pm
by supachamp
Thanks a lot for the trial version. Unfortunately, I cannot compile the sample because class com.plutext.docx.toc.TocHelper seems to be missing in the package.

Re: Add table of content and export to PDF

PostPosted: Wed Apr 02, 2014 12:28 am
by supachamp
I managed to get the TOC working in my project. However, if I do not use the predefined heading styles, the TOC is not generated in PDF output. I use the following line to declare the TOC:

TocGenerator.generateToc(this.wordMLPackage, 0, " TOC \\o \"1-3\" \\h \\z \\t \"ChapterLevel1,1,ChapterLevel2,2,GuidelineHeadline,3\" \\u ", false);

ChapterLevel1 and ChapterLevel2 are styles which are derived from Heading1 and Heading2 and GuidelineHeadline is derived from Heading3. The Word document displays the TOC correctly.

Do I have to declare something else as well? Or do I need to use the predefined headline styles?

Thanks a lot for your help!

Re: Add table of content and export to PDF

PostPosted: Wed Apr 02, 2014 9:24 pm
by jason
Hi there, I've replicated your description.

Please try now the current trial version from http://www.plutext.com/m/index.php/products

With that, either of the following formulations ought to work:
Code: Select all

        TocGenerator.generateToc(wordMLPackage, 0, " TOC \\o \"1-3\" \\h \\z \\u ", false);
       
        TocGenerator.generateToc(wordMLPackage, 0,
              " TOC \\o \"1-3\" \\h \\z \\t \"ChapterLevel1,1,ChapterLevel2,2,GuidelineHeadline,3\" \\u ", false);



Dot leader should also work in nightly-20140402.

cheers .. Jason

Re: Add table of content and export to PDF

PostPosted: Thu Apr 03, 2014 6:01 pm
by supachamp
Thank you very much. The new version works great! Is it possible to style the TOC, especially the font of the page numbers?

Best regards.