We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Bookmark in word document and convert the document to PDF


My requirement is to add a bookmark to a word document and then convert that to PDF. So using aspose.words i will bookmark the document and convert that to PDF. Or Do i need aspose.pdf to get the bookmark in the generated pdf?




Hi Showji,

Thanks for your inquiry. Yes, you can insert bookmark into Word document and convert it to Pdf document using Aspose.Words. Please use the following code example to achieve your requirements.


Document doc = new Document(MyDir

  • “in.docx”);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.startBookmark("Bookmark");

builder.writeln("Text inside a bookmark.");

builder.endBookmark("Bookmark");

doc.save(MyDir + "Out.pdf", SaveFormat.PDF);


<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>AR-SA</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>Moreover, the DocumentBuilder has an internal cursor that you can navigate to a different location in a document using various DocumentBuilder.moveToXXX methods such as DocumentBuilder.moveToDocumentStart and DocumentBuilder.moveToField. After moving the cursor to a particular location, you can insert bookmark. Please read following documentation links for your kind reference.
http://www.aspose.com/docs/display/wordsjava/DocumentBuilder+Overview
http://www.aspose.com/docs/display/wordsjava/Moving+the+Cursor

Hi Tahir


Thank you for the response. I tried the code and generated the PDF. And when i open the pdf, i didn’t see any bookmark. Please review the attached PDF.

Here is the code i have tried
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark(“Hello”);
builder.writeln(“Mark1”);
builder.endBookmark(“Hello”);
builder.startBookmark(“Apple”);
builder.writeln(“Mark2”);
builder.endBookmark(“Apple”);

But the pdf contains statement like belo; Is the bookmark is not seen because i used the evaluation copy? Or i missed any library?
Evaluation Only. Created with Aspose.Words. Copyright 2003-2014 Aspose Pty Ltd.
Mark1
Mark2
Evaluation Only. Created with Aspose.Words. Copyright 2003-2014 Aspose Pty Ltd.

Regards
Showji

Hi Showji,

Thanks for your inquiry. Please note
that in evaluation mode there are some limitations applied. The
document’s content are truncated after a certain number of paragraphs
during import or export. To avoid this you can request a free 30-day
trial license which removes these evaluation restrictions. You can
request this from here:
http://www.aspose.com/corporate/purchase/temporary-license.aspx

Please use the OutlineOptions.BookmarksOutlineLevels property to achieve your requirements. Please let us know if you have any more queries.


Document doc = new Document(MyDir + "in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.startBookmark("Hello");

builder.writeln("Mark1");

builder.endBookmark("Hello");

builder.startBookmark("Apple");

builder.writeln("Mark2");

builder.endBookmark("Apple");

PdfSaveOptions option = new PdfSaveOptions();

option.getOutlineOptions().setDefaultBookmarksOutlineLevel(3);

doc.save(MyDir + "Out.pdf", option);



<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>AR-SA</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>