A well-structured Word document often contains headings, when converting such Word document to PDF, you may want to preserve the headings in the Word document as bookmarks in the result PDF document.
In this article, I will show you how to convert a Word document to PDF and keep headings as bookmarks programmatically in Java using Spire.Doc for Java library.
Add Dependencies
There are two ways to include Spire.Doc for Java in your Java project.
For maven projects, add the following dependencies to your project’s pom.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http: //repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId> e-iceblue </groupId> <artifactId>spire.doc </artifactId> <version> 4.7 . 0 </version> </dependency> </dependencies> |
For non-maven projects, download Spire.Doc for Java pack from here: Download- Spire.Doc for Java, extract the zip file, then add Spire.Doc.jar in the lib folder into your project as a dependency.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import com.spire.doc.Document; import com.spire.doc.ToPdfParameterList; public class ConvertWordToPDFAndKeepHeadingsAsBookmarks { public static void main(String[] args){ //Create a Document instance Document document = new Document(); //Load a Word document document.loadFromFile( "Input.docx" ); //Create a ToPdfParameterList instance ToPdfParameterList pdfParameterList = new ToPdfParameterList(); //Specify whether to preserve headings as bookmarks when converting Word to PDF pdfParameterList.setCreateWordBookmarks( true ); pdfParameterList.createWordBookmarksUsingHeadings( true ); //Save the Word document to PDF document.saveToFile( "Output.pdf" , pdfParameterList); } } |
Word Document
Converted PDF
Comments
Post a Comment