PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Nov 19, 2018 5:39 pm

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Fri Apr 11, 2014 2:30 pm 

Joined: Fri Apr 11, 2014 1:37 pm
Posts: 1

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!


        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);


            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);");


            PdfDictionary dict = new PdfDictionary();
            PdfArray array = new PdfArray();
            dict.Elements["/Names"] = array;
            array.Elements.Add(new PdfString("EmbeddedJS"));

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

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

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], mronosa and 6 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group