Hello, All
Thank you for the good job, like it a lot, but you know... bugs happen. I think there is a bug in PdfLinkAnnotation
Description & ReasonIf you add a PdfLinkAnnotation pointing to the 1st document page, you will not be able to save the document.
PdfLinkAnnotation.WriteObject(PdfWriter writer) will try to get the -1st (negative) page of the document, which in turn yields
ArgumentOutOfBoundsException:
Code:
...
case LinkType.Document:
// destIndex > Owner.PageCount can happen rendering pages using PDFsharp directly
int destIndex = this.destPage;
if (destIndex > Owner.PageCount)
destIndex = Owner.PageCount;
destIndex--;
dest = this.Owner.Pages[destIndex];
...
"destIndex--" looks like a typo here, probably the original idea was like this:
Code:
...
case LinkType.Document:
// destIndex > Owner.PageCount can happen rendering pages using PDFsharp directly
int destIndex = this.destPage;
if (destIndex > Owner.PageCount)
{
destIndex = Owner.PageCount;
destIndex--;
}
dest = this.Owner.Pages[destIndex];
...
with best regards,
Alexey
Code to Reproduce the BugI have tested on the latest sources, .NET 2.0 flavour. Should fail in any flavour.
Code:
public static void ReproduceFirstPageLinkBug()
{
PdfDocument document;
PdfPage firstPage;
PdfPage secondPage;
PdfAnnotation linkToFirstPage;
document = new PdfDocument();
firstPage = document.AddPage();
secondPage = document.AddPage();
linkToFirstPage = PdfLinkAnnotation.CreateDocumentLink(secondPage.MediaBox, 0);
secondPage.Annotations.Add(linkToFirstPage);
using (MemoryStream stream = new MemoryStream(1024))
{
document.Save(stream);
}
}