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

Combine Documents Problem: Hyperlinks Going to Wrong Page
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1713
Page 1 of 1

Author:  jrcencapsulated [ Tue Jul 05, 2011 9:54 pm ]
Post subject:  Combine Documents Problem: Hyperlinks Going to Wrong Page

Hi all,

I'm currently working on writing a program to combine individual chapters, each of which is a single PDF document, into one PDF document. So I'll have something like: Chapter1.pdf, Chapter2.pdf, ... and the program will combine all of those PDFs into one document WholeDocument.pdf.

I'm having a problem where after combining all of the chapters into one document the hyperlinks in each chapters' Table of Contents are pointing to the pages in the first PDF document instead of the pages in their respective document. For instance, if I only combine Chapter1.pdf and Chapter2.pdf and I go to the Table of Contents in Chapter 2 and click a link "Random Section.........page 20" it will take me to Chapter 1's page 20 instead of Chapter 2's page 20. When I step through the code in VS 2010 the page's annotation is using "/Dest" to figure out the page to link to (I'm not sure if that matters or not).

The following is a snippet of the code I'm using. It's very basic (from the example mostly). I'm not sure if this is a bug or something I'm doing wrong when combining the document. If I'm not doing anything wrong with compiling the document I'm wondering if anyone knows how I can possibly automate the linking of these /Dest annotations to link to the correct place. The /Dest appears to be using PdfReferences (iref(#,#)) to figure out the page you're talking about but I can't figure out how to tell it "Don't use Chapter 1's page 18, use Chapter 2's page 18."

Code:
            PdfDocument inputDocument1 = PdfReader.Open("Chapter1.pdf", PdfDocumentOpenMode.Import);
            PdfDocument inputDocument2 = PdfReader.Open("Chapter2.pdf", PdfDocumentOpenMode.Import);

            PdfDocument outputDocument = new PdfDocument();

            outputDocument.PageLayout = PdfPageLayout.OneColumn;

            for (int idx = 0; idx < inputDocument1.PageCount; idx++)
            {
                // Get page from the document
                PdfPage page1 = inputDocument1.PageCount > idx ?
                inputDocument1.Pages[idx] : new PdfPage();
 
                // Add pages to the output document
                page1 = outputDocument.AddPage(page1);
             }

            for (int idx = 0; idx < inputDocument1.PageCount; idx++)
            {
                // Get page from 2nd document
                PdfPage page2 = inputDocument2.PageCount > idx ?
                inputDocument2.Pages[idx] : new PdfPage();

                // Go through all the Annotations for the page
                foreach (PdfAnnotation item in page2.Annotations)
                {
                    // If the annotation has a Destination make it go to the top of the
                    // page instead of somewhere in the middle.
                    if (item.Elements["/Dest"] != null)
                    {
                        PdfArray test = (PdfArray)item.Elements["/Dest"].Clone();
                       
                        // These two lines of code make the link jump to the X, Y coordinate on the page 2=x 3=y
                        test.Elements[2] = new PdfInteger(0);
                        test.Elements[3] = new PdfInteger(0);
                        item.Elements["/Dest"] = test;
                    }
                }

                page2 = outputDocument.AddPage(page2);
            }           
 
            // Save the document...
            const string filename = "CompareDocument1_tempfile.pdf";
            outputDocument.Save(filename);


-------- Update 7/8/2011-------
Upon further investigation the links are broken in the entire PDF. The fact that they pointed to the correct page in the first chapter seems to be a coincidence. The farther down in the links you go the more off it is until midway through the second page the links pop up an error message about not being able to find the ID specified.

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