diff --git a/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs b/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
index 8042e993e..099b2a440 100644
--- a/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
+++ b/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
@@ -256,15 +256,11 @@ private static void HandleLinks(PdfDocument document, HtmlContainer container, X
if (link.IsAnchor)
{
// create link to another page in the document
- var anchorRect = container.GetElementRectangle(link.AnchorId);
- if (anchorRect.HasValue)
+ var anchorPageIdx = AnchorPage(container, tree, slotToPage, link.AnchorId);
+ // document links to the same page as the link is not allowed
+ if (anchorPageIdx.HasValue && pageIndex != anchorPageIdx.Value)
{
- var anchorSlot = SlotContaining(tree, anchorRect.Value.Top);
- // document links to the same page as the link is not allowed
- if (anchorSlot.HasValue && slotToPage.TryGetValue(anchorSlot.Value, out var anchorPageIdx) && pageIndex != anchorPageIdx)
- {
- document.Pages[pageIndex].AddDocumentLink(new PdfRectangle(xRect), anchorPageIdx + 1); // PdfSharp page numbers are one-based
- }
+ document.Pages[pageIndex].AddDocumentLink(new PdfRectangle(xRect), anchorPageIdx.Value + 1); // PdfSharp page numbers are one-based
}
}
else
@@ -281,6 +277,22 @@ private static void HandleLinks(PdfDocument document, HtmlContainer container, X
/// or null if it falls in no materialized fragmentainer's band (e.g. an anchor inside a
/// content-empty page slot that was skipped, or past the end of the document).
///
+ ///
+ /// The PDF page index the fragment of an anchor link points to, null if it points nowhere.
+ ///
+ ///
+ /// An empty fragment, and top without an element of that id, is the top of the document, HTML 7.4.6.4.
+ ///
+ private static int? AnchorPage(HtmlContainer container, FragmentTree tree, Dictionary slotToPage, string fragment)
+ {
+ var anchorRect = fragment.Length > 0 ? container.GetElementRectangle(fragment) : null;
+ if (!anchorRect.HasValue)
+ return fragment.Length == 0 || string.Equals(fragment, "top", StringComparison.OrdinalIgnoreCase) ? 0 : null;
+
+ var anchorSlot = SlotContaining(tree, anchorRect.Value.Top);
+ return anchorSlot.HasValue && slotToPage.TryGetValue(anchorSlot.Value, out var anchorPageIdx) ? anchorPageIdx : null;
+ }
+
private static int? SlotContaining(FragmentTree tree, double y)
{
foreach (var fragmentainer in tree.Fragmentainers)
diff --git a/Source/Test/HtmlRenderer.PdfSharp.Test/HandleLinksPaginationTests.cs b/Source/Test/HtmlRenderer.PdfSharp.Test/HandleLinksPaginationTests.cs
index 188021cd9..fc15fc985 100644
--- a/Source/Test/HtmlRenderer.PdfSharp.Test/HandleLinksPaginationTests.cs
+++ b/Source/Test/HtmlRenderer.PdfSharp.Test/HandleLinksPaginationTests.cs
@@ -73,4 +73,27 @@ public async Task Link_AfterAContentEmptySpacer_LandsOnTheCorrectMaterializedPag
// (indexing document.Pages out of range) or silently land on the wrong page.
Assert.IsLessThanOrEqualTo(6, document.Pages.Count, "the 2500pt content-empty gap should be skipped, not paginated through as blank pages");
}
+
+ [TestMethod]
+ [Spec("html", "browsing-the-web.html#scrolling-to-a-fragment")]
+ [DataRow("#")]
+ [DataRow("#top")]
+ [DataRow("#TOP")]
+ public async Task EmptyOrTopFragment_LinksToTheFirstPage(string href)
+ {
+ var config = new PdfGenerateConfig { PageSize = PageSize.A4 };
+ config.SetMargins(20);
+
+ var filler = string.Concat(Enumerable.Repeat("filler line of body text
", 150));
+ var html = $"{filler}back to the top
";
+
+ using var document = await PdfGenerator.GeneratePdf(html, config);
+ using var stream = new MemoryStream();
+ document.Save(stream);
+
+ var lastPage = document.Pages[document.Pages.Count - 1];
+ Assert.AreEqual(1, lastPage.Annotations.Count);
+ var destination = lastPage.Annotations[0].Elements["/Dest"]!.ToString();
+ StringAssert.StartsWith(destination, $"[{document.Pages[0].Reference!.ObjectNumber} 0 R ");
+ }
}