PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PdfLinkAnnotation: Link to the 1st Page Fails in WriteObject
https://forum.pdfsharp.net/viewtopic.php?f=2&t=958
Page 1 of 1

Author:  bergamot [ Thu Nov 26, 2009 11:15 am ]
Post subject:  PdfLinkAnnotation: Link to the 1st Page Fails in WriteObject

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 & Reason

If 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 Bug
I 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);
            }
        }

Author:  Thomas Hoevel [ Thu Nov 26, 2009 12:08 pm ]
Post subject:  Re: PdfLinkAnnotation: Link to the 1st Page Fails in WriteObject

Hi!
bergamot wrote:
"destIndex--" looks like a typo here, probably the original idea was like this: [...]

I must check this.

I guess the idea was to convert from 1-based page number to 0-based page index.

Author:  bergamot [ Fri Nov 27, 2009 1:36 pm ]
Post subject:  Re: PdfLinkAnnotation: Link to the 1st Page Fails in WriteObject

Oh, yes, you a right, Thomas. That might be a feature as well. So, is it 1-based or 0-based after all?

Author:  Thomas Hoevel [ Mon Nov 30, 2009 2:00 pm ]
Post subject:  Re: PdfLinkAnnotation: Link to the 1st Page Fails in WriteObject

Hi!

MigraDoc Online Help wrote:
destinationPage
Type: System.Int32
The one-based destination page number.


The code is mainly correct ...
... but future releases will throw an exception if CreateDocumentLink is called with a page number less than 1.
Pass 1 (not 0) to link to the first page.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/