Word Bookmarks and PDF Bookmarks

Hi,

If I have nested word bookmarks i.e

Bookmark 1 START
Bookmark 2 START
Bookmark 2 END
Bookmark 1 End

And convert the word document from DOCX to PDF using

saveOptions.BookmarksOutlineLevel - I want to only use level 2
i.e bookmark 2 in the PDF bookmarks

Is this possible ?

Secondly - When adding a bookmark in word i.e “Hello World” once converted to PDF it is displayed as “Hello_World” because the bookmark in word says “Hello_World”. How can I get it to be separate words.

Hi Neil,

Thanks for your inquiry.

Could you please attach 1) your input Word document containing the nested Bookmarks, 2) your output PDF document showing the undesired behaviour and 3) your expected PDF document showing the desired behaviour here for testing? I will then investigate the issue on my side and provide you more information.

Secondly, by Microsoft Word design, it is a limitation that you can not create a Bookmark having white spaces in it’s name. These white spaces are simply converted to underscore characters. Please read the discussion here.

Best regards,

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartBookmark("Bookmark1");
builder.Writeln("bookmark1");
builder.StartBookmark("Bookmark2");
builder.Writeln("bookmark2");
builder.EndBookmark("Bookmark2");
builder.EndBookmark("Bookmark1");

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.BookmarksOutlineLevel = 2;

builder.Document.Save(@"C:\Temp\out.docx", SaveFormat.Docx);
builder.Document.Save(@"C:\Temp\out.pdf", saveOptions);

I would expect only bookmark 2 to appear in the PDF bookmark section. Otherwise I am not sure what level 2 represents ?

If I cannot use spaces for the docx bookmarks how can I achieve bookmarks with spaces in them in the PDF ?

Hi Neil,

Thanks for your inquiry.

Please note that there is no concept of nesting Bookmarks in Microsoft Word documents and all Bookmarks are always on the same level. If you don’t specify PdfSaveOptions.BookmarksOutlineLevel property or set it’s value to 0, all Word bookmarks will not be displayed in the PDF’s document outline. When you specify 2, all Word bookmarks will be displayed in the PDF document outline up to level 2. So, there is no way you can prevent a few Word Bookmarks from displaying in document map outline in PDF. To clarify the concept (see attached screenshot), I have attached a sample input Word document here for your reference. Please try creating the two PDF documents using the following code snippet:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach(Bookmark bm in doc.Range.Bookmarks)
{
    if (bm.Name.StartsWith("_Toc"))
    {
        builder.MoveToBookmark(bm.Name, false, false);
        builder.StartBookmark("BM_" + bm.Text);
        builder.EndBookmark("BM_" + bm.Text);
    }
}
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.HeadingsOutlineLevels = 3;
// Produce outLevel2.pdf
saveOptions.BookmarksOutlineLevel = 2;
// Produce outLevel3.pdf
// saveOptions.BookmarksOutlineLevel = 3;
doc.Save(@"C:\Temp\outLevel2.pdf", saveOptions);

Secondly, I am afraid, spaces in Bookmark’s name are not allowed. May be you can use PdfSaveOptions.HeadingsOutlineLevels property to achieve what you are looking for. Please let me know if I can be of any further assistance.

Best regards,