PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 6:30 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Dec 31, 2010 6:05 am 
Offline

Joined: Fri Dec 31, 2010 5:51 am
Posts: 2
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 :?:


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 31, 2010 2:09 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
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);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 01, 2011 6:01 am 
Offline

Joined: Fri Dec 31, 2010 5:51 am
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 07, 2012 7:29 am 
Offline

Joined: Fri Aug 03, 2012 1:59 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 128 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