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

How to work with PDF annotation objects
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1489
Page 1 of 1

Author:  wipa [ Fri Dec 31, 2010 6:05 am ]
Post subject:  How to work with PDF annotation objects

Dear experts,

There are a lot of discussions on how to work with images in existing PDFs...
I cannot find a good answer on how to edit/delete text (Annot) objects. For example:

Code:
40 0 obj
<</Type /Annot
/Rect [360.5339 484.7710 442.1034 494.7290]
/Border [0 0 0]
/Subtype /Link
/A <</Type /Action
/S /URI
/URI (http://www.adobe.com/products/acrobat/readstep2.html)>>>>
endobj


Inside the page object it's referenced:
Code:
...
/Annots [44 0 R 45 0 R 32 0 R 33 0 R 34 0 R 35 0 R 36 0 R 37 0 R 38 0 R 39 0 R 40 0 R 41 0 R 42 0 R 43 0 R]>>
...


I've tried to loop through the annotations by PdfArray.ArrayElements arr = page.Annotations.Elements; but can only get PdfItem object array, which is not giving me any way to, for example change the "/URI" of the object.
I want to be able to do something like:
Code:
if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Link") {
  xObject.Elements.SetString("/URI", "http://somethingelse.com");
}


Any suggestions :?:

Author:  jeffhare [ Fri Dec 31, 2010 2:09 pm ]
Post subject:  Re: How to work with PDF annotation objects

This is not so hard to do, but not nearly as simple as it could be with some library adjustments...

Don't try this using foreach() because the type conversions don't work properly today as I recall...
Hope this helps, I didn't try this exact code fragment, but I do similar things in my application, so this may need some tweaks to get it working as you like.

Code:
        private void ChangeHyperlink(PdfDocument document)
        {
            int pgs = document.Pages.Count;

            for (int i = 0; i < pgs; i++)
            {
                PdfPage page = document.Pages[i];

                for (int a = 0; a < page.Annotations.Count; a++)
                {
                    PdfAnnotation annotation = page.Annotations[a];

                    ICollection<string> keys = annotation.Elements.Keys;
                    ICollection<PdfItem> elements = annotation.Elements.Values;

                    foreach (KeyValuePair<string, PdfItem> item in annotation.Elements)
                    {
                        Debug.WriteLineIf(item.Value is PdfDictionary, "PdfDictionary annotation");
                        Debug.WriteLine("Type: " + (item.Value.GetType().ToString()));
                        Debug.WriteLine("Key: " + item.Key);
                        Debug.WriteLine("Value: " + item.Value.ToString());

                        if (item.Value is PdfDictionary)
                        {
                            PdfDictionary dict = item.Value as PdfDictionary;
                            PdfString PdfUrl = dict.Elements[@"/URI"] as PdfString;
                            if (PdfUrl != null)
                            {
                                string relUri = PdfUrl.Value;
                                {
                                    string replacementUrl = "http://www.hareville.com";

                                    try
                                    {
                                        dict.Elements[@"/URI"] = new PdfString(replacementUrl);
                                        Debug.WriteLine("New Value: " + item.Value.ToString());
                                    }
                                    catch (Exception ex)
                                    {
                                        string message = String.Format(
                                            "Error Updating Link: {{0}} {{2}} with new link: {{1}}{{2}}The resulting Pdf Document will not have this link corrected."
                                            ,relUri
                                            ,replacementUrl ?? "(null replacement hyperlink)"
                                            ,Environment.NewLine);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

Author:  wipa [ Sat Jan 01, 2011 6:01 am ]
Post subject:  Re: How to work with PDF annotation objects

Thanks jeffhare. This is what I was after. Indeed the code looks "a bit" large for this type of task but it does the job :o

Author:  perholvik [ Tue Aug 07, 2012 7:29 am ]
Post subject:  Re: How to work with PDF annotation objects

jeffhare wrote:
This is not so hard to do, but not nearly as simple as it could be with some library adjustments...

Don't try this using foreach() because the type conversions don't work properly today as I recall...
Hope this helps, I didn't try this exact code fragment, but I do similar things in my application, so this may need some tweaks to get it working as you like.

Code:
        private void ChangeHyperlink(PdfDocument document)
        {
            int pgs = document.Pages.Count;

            for (int i = 0; i < pgs; i++)
            {
                PdfPage page = document.Pages[i];

                for (int a = 0; a < page.Annotations.Count; a++)
                {
                    PdfAnnotation annotation = page.Annotations[a];

                    ICollection<string> keys = annotation.Elements.Keys;
                    ICollection<PdfItem> elements = annotation.Elements.Values;

                    foreach (KeyValuePair<string, PdfItem> item in annotation.Elements)
                    {
                        Debug.WriteLineIf(item.Value is PdfDictionary, "PdfDictionary annotation");
                        Debug.WriteLine("Type: " + (item.Value.GetType().ToString()));
                        Debug.WriteLine("Key: " + item.Key);
                        Debug.WriteLine("Value: " + item.Value.ToString());

                        if (item.Value is PdfDictionary)
                        {
                            PdfDictionary dict = item.Value as PdfDictionary;
                            PdfString PdfUrl = dict.Elements[@"/URI"] as PdfString;
                            if (PdfUrl != null)
                            {
                                string relUri = PdfUrl.Value;
                                {
                                    string replacementUrl = "http://www.hareville.com";

                                    try
                                    {
                                        dict.Elements[@"/URI"] = new PdfString(replacementUrl);
                                        Debug.WriteLine("New Value: " + item.Value.ToString());
                                    }
                                    catch (Exception ex)
                                    {
                                        string message = String.Format(
                                            "Error Updating Link: {{0}} {{2}} with new link: {{1}}{{2}}The resulting Pdf Document will not have this link corrected."
                                            ,relUri
                                            ,replacementUrl ?? "(null replacement hyperlink)"
                                            ,Environment.NewLine);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


Thanks a bunch for sharing that bit of code, jeffhare! I've used it with next to no modifications to add a base URL to all relative URLs in a document; works a charm :)

/Per

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