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

Change Hyperlink in PDF to Open New Browser Window
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2790
Page 1 of 1

Author:  NealProplanner [ Fri Apr 11, 2014 2:30 pm ]
Post subject:  Change Hyperlink in PDF to Open New Browser Window

Hello!

I searched the Support forum and found a couple of questions that were similar to what I need to do, but they don't give me enough information to implement it.

Our product generates PDF files using a 3rd party Rich Editor control. The PDF can contain hyperlinks. One of our customers wants to open the PDF in Internet Explorer and have the link open a new IE window, rather than replacing the current window as it does now.

The vendor of the utility that creates the PDF files gave me sample code to modify the file using a different open source PDF library, but that library requires a fee for commercial distribution that our management says we cannot pay.

So, basically, I need code to read a PDF file containing what I think you would call file links and replace them with web links(?) that would open a new browser window if the PDF is originally open in a browser. We could either update the input file or we could create a new file.

Our customer used Acrobat to modify a PDF and sent us "before" and "after" versions of it. I see that annotations with JavaScript are added, but I haven't been able to figure out how to get PDFSharp to properly add them. I could send these files to you privately if it might help.

Following is some code that I've hacked together trying to use PDFsharp with the structure that was in the vendor's sample and bits of examples I've found on the web. It doesn't do what I need but I don't know what to do to fix it.

I would be grateful for your help in solving this problem!

Neal

Code:
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return;
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return;
 
           PdfDocument inputDocument1 = PdfReader.Open(openFileDialog1.FileName, PdfDocumentOpenMode.Import);
            PdfDocument outputDocument = new PdfDocument();

            for (int pageIndex = 0; pageIndex < inputDocument1.PageCount; pageIndex++)
            {
                ModifyLinks(inputDocument1, outputDocument, pageIndex);
            }

            outputDocument.Save(saveFileDialog1.FileName);

            MessageBox.Show(saveFileDialog1.FileName + " was successfully saved.");
        }


   static void ModifyLinks(PdfDocument inputDocument, PdfDocument outputDocument, int page)
        {
            PdfPage currPage = inputDocument.Pages[page];

            currPage = outputDocument.AddPage(currPage);
            if (currPage.Annotations.Count != 0)
            {
                for (int j = currPage.Annotations.Count - 1; j >= 0; j--)
                {
                    var annot = currPage.Annotations[j];

                    PdfItem item  = annot.Elements["/A"];

                    if (item != null)
                    {
                        var uri = ((item as PdfSharp.Pdf.Advanced.PdfReference).Value as PdfDictionary).Elements[@"/URI"] as PdfString;
                        ModifyLink(outputDocument, uri);
                    }                   
                }
            }
        }

  static void ModifyLink( PdfDocument outputDocument, PdfItem uri)
        {
            PdfDictionary dictJS = new PdfDictionary();
            dictJS.Elements["/S"] = new PdfName("/JavaScript");
            dictJS.Elements["/JS"] = new PdfStringObject(outputDocument, "app.launchURL(\"" + uri.ToString() + "\", true);");

            outputDocument.Internals.AddObject(dictJS);

            PdfDictionary dict = new PdfDictionary();
            PdfArray array = new PdfArray();
            dict.Elements["/Names"] = array;
            array.Elements.Add(new PdfString("EmbeddedJS"));
            array.Elements.Add(PdfInternals.GetReference(dictJS));
            outputDocument.Internals.AddObject(dict);

            PdfDictionary group = new PdfDictionary();
            group.Elements["/JavaScript"] = PdfInternals.GetReference(dict);

            outputDocument.Internals.Catalog.Elements["/Names"] = group;
        }

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