I’m currently using GemBox.Document and Gembox.Pdf to convert HTML content to PDF, where headings ( h1,h2 , etc.) are automatically converted into bookmarks using PdfBookmarksCreateOptions.Headings.
While the bookmarks are successfully created and visible in the PDF outline, I need to retrieve the exact position (coordinates) of each bookmark within the PDF — specifically the X and Y location on the page. This is important for a use case where multiple bookmarks exist on the same page, and I need to distinguish their vertical placement and use outline setdestination to set coordinates. So that when we click on bookmarks it should scroll to top position on pdf viewer
Hi Pardeep,
GemBox.Document currently exports outlines in PDF files with the destination page, but not with coordinates on the destination page.
So, perhaps you could use this to update the outline destinations:
static void Main()
{
var document = DocumentModel.Load("input.html");
document.Save("gembox-document-output.pdf");
using (var pdf = PdfDocument.Load("gembox-document-output.pdf"))
{
SetOutlinesDestination(pdf.Outlines);
pdf.Save("gembox-pdf-output.pdf");
}
}
static void SetOutlinesDestination(PdfOutlineCollection outlines)
{
foreach (PdfOutline outline in outlines)
{
PdfText outlineText;
var texts = outline.Destination.Page.Content.GetText().Find(outline.Title).ToList();
switch (texts.Count)
{
case 0:
throw new InvalidOperationException("Outline text not found: " + outline.Title);
case 1:
outlineText = texts[0];
break;
default:
// Use PdfText.Format to find the best match.
// For example, header text usually has the largest font size.
double maxFontSize = texts.Max(t => t.Format.Text.Font.Size);
outlineText = texts.First(t => t.Format.Text.Font.Size == maxFontSize);
break;
}
outline.SetDestination(
outline.Destination.Page,
PdfDestinationViewType.LeftTopZoom,
outlineText.Bounds.Left,
outlineText.Bounds.Top,
0);
if (outline.Outlines.Count > 0)
SetOutlinesDestination(outline.Outlines);
}
}
Does this solve your issue?
Regards,
Mario
I already tried this but it gives me null exception
Bounds = ‘outlineText.Bounds’ threw an exception of type ‘System.NullReferenceException’
Please send us your input HTML so we can reproduce the issue and investigate it.
<!DOCTYPE html>
<html>
<head>
<meta charset=""UTF-8""/>
<style>
body {
font-family: Arial, sans-serif;
font-size: 12px;
margin: 0;
padding: 10px;
line-height: 1.4;
}
.bookmark-header {
background: #e5e5e5;
text-align: center;
font-weight: bold;
font-size: 13px;
padding: 10px 0;
margin: 20px 0 10px 0;
border: 1px solid #ccc;
}
.section-content {
margin: 10px 0 50px 0;
padding: 20px;
height: 400px;
border: 1px solid #ddd;
background: #f9f9f9;
}
.page-break {
page-break-before: always;
}
</style>
</head>
<body>
<h1 class=""bookmark-header"">Section 1 - 5</h1>
<h2 class=""bookmark-header"">Section 1 </h2>
<div class=""section-content"">
<p><strong>This is the first section.</strong></p>
<p>When you click on the Section 1 bookmark, you should be taken to the very top of this section.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<h2 class=""bookmark-header"">Section 2 </h2>
<div class=""section-content"">
<p><strong>This is the second section data.</strong></p>
<p>When you click on the Section 2 bookmark, you should be taken to the very top of this section.</p>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<h2 class=""bookmark-header"">Section 3</h2>
<div class=""section-content"">
<p><strong>This is the third section (SAME PAGE).</strong></p>
<p>When you click on the Section 3 bookmark, you should be taken to the very top of this section.</p>
<p>This section is on the same page as Section 2, so this tests the specific issue you're experiencing.</p>
<p>The bookmark should still navigate to the top of this section, not somewhere in the middle.</p>
</div>
<div class=""page-break""></div>
<h2 class=""bookmark-header"">Section 4 </h2>
<div class=""section-content"">
<p><strong>This is the fourth section on a new page.</strong></p>
<p>When you click on the Section 4 bookmark, you should be taken to the very top of this section.</p>
<p>This section is on a new page to test cross-page bookmark navigation.</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.</p>
</div>
<span style=""display:none;"">BOOKMARK_MARKER:Section5</span>
<h2 class=""bookmark-header"">Section 5 </h2>
<div class=""section-content"">
<p><strong>This is the fifth section, also on the same page as Section 4.</strong></p>
<p>When you click on the Section 5 bookmark, you should be taken to the very top of this section.</p>
<p>This tests the same-page bookmark issue on the second page.</p>
<p>Totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt.</p>
</div>
</body>
</html>
This is the html using
I was unable to reproduce your issue, you can download the resulting PDF files from:
https://www.gemboxsoftware.com/temp/forum-1610/gembox-document-output.pdf
https://www.gemboxsoftware.com/temp/forum-1610/gembox-pdf-output.pdf
Try again using the latest versions of GemBox.Document and GemBox.Pdf:
https://www.nuget.org/packages/GemBox.Document/
https://www.nuget.org/packages/GemBox.Pdf/
Does this solve your issue?
If the problem remains, please send us a small Visual Studio project that reproduces your issue so we can investigate it.
Regards,
Mario
Yes after nuget update it start working,
thank you