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

Document-Links of imported pages do not work
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3382
Page 1 of 1

Author:  (void) [ Wed Jun 22, 2016 8:13 pm ]
Post subject:  Document-Links of imported pages do not work

While working on a library, that merges multiple Pdf-Documents, i observed an odd behavior when the imported Pages contained Document-Links. (Link-Annotations)

Beside from the links not working at all in the final document, i found out, the links were still referencing a page from the original document, that was imported.
Further debugging revealed, the pages from the original document were all present in the final document (and therefore duplicates) as well as the /Pages-Array of the original document.

This was observed using PdfSharp 1.32, but a quick look at the 1.50 source-code suggests, the problem is still present there.

I was able to work around this issue by writing a small utility-function

Code:
        private void FixLinkAnnotations(PdfPage page, PdfDocument doc, int pageOffset)
        {
            for (var i = 0; i < page.Annotations.Count; i++)
            {
                var annot = page.Annotations[i];
                var subType = annot.Elements.GetString(PdfAnnotation.Keys.Subtype);
                if (subType == "/Link")
                {
                    var dest = annot.Elements.GetArray("/Dest");    // may also be a named destination
                    var rect = annot.Elements.GetRectangle(PdfAnnotation.Keys.Rect);
                    if (dest != null && rect != null && dest.Elements.Count > 0)
                    {
                        var targetPage = dest.Elements.GetReference(0);
                        for (var p = 0; p < doc.PageCount; p++)
                        {
                            if (doc.Pages[p].Reference == targetPage)
                            {
                                page.Annotations.Elements[i] = PdfLinkAnnotation.CreateDocumentLink(rect, p + pageOffset);
                                // TODO: copy additional properties from original annotation ?
                            }
                        }
                    }
                }
            }
        }


Regards,
Thomas

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