Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
/// </summary>
/// <summary>
/// The PDF page index the fragment of an anchor link points to, null if it points nowhere.
/// </summary>
/// <remarks>
/// An empty fragment, and top without an element of that id, is the top of the document, HTML 7.4.6.4.
/// </remarks>
private static int? AnchorPage(HtmlContainer container, FragmentTree tree, Dictionary<int, int> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<p style='margin:0;'>filler line of body text</p>", 150));
var html = $"<html><body>{filler}<p><a href='{href}'>back to the top</a></p></body></html>";

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 ");
}
}
Loading