static void Main(string[] args) { Console.WriteLine("PDFObject Sample:"); // ReSharper disable once UnusedVariable using (Library lib = new Library()) { String sInput = Library.ResourceDirectory + "Sample_Input/sample_links.pdf"; String sOutput = "PDFObject-out.pdf"; if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sOutput = args[1]; } Console.WriteLine("Input file: " + sInput + ". Writing to output " + sOutput); Document doc = new Document(sInput); Page page = doc.GetPage(0); LinkAnnotation annot = (LinkAnnotation)page.GetAnnotation(1); URIAction uri = (URIAction)annot.Action; // Print some info about the URI action, before we modify it Console.WriteLine("Initial URL: " + uri.URI); Console.WriteLine("Is Map property: " + uri.IsMap); // Modify the URIAction // // A URI action is a dictionary containing: // Key: S Contents: a name object with the value "URI" (required) // Key: URI Contents: a string object for the uniform resource locator (required) // Key: IsMap Contents: a boolean for whether the link is part of a map (optional) // (see section 8.5.3, "Action Types", of the PDF Reference) // // We will change the URI entry and delete the IsMap entry for this dictionary PDFDict uri_dict = uri.PDFDict; // Extract the dictionary // Create a new string object PDFString uri_string = new PDFString("http://www.google.com", doc, false, false); uri_dict.Put("URI", uri_string); // Change the URI (replaces the old one) uri_dict.Remove("IsMap"); // Remove the IsMap entry // Check that we deleted the IsMap entry Console.WriteLine("Does this dictionary have an IsMap entry? " + uri_dict.Contains("IsMap")); doc.Save(SaveFlags.Full, sOutput); doc.Close(); // Check the modified contents of the link doc = new Document(sOutput); page = doc.GetPage(0); annot = (LinkAnnotation)page.GetAnnotation(1); uri = (URIAction)annot.Action; Console.WriteLine("Modified URL: " + uri.URI); Console.WriteLine("Is Map property (if not present, defaults to false): " + uri.IsMap); } }
static void Main(string[] args) { Console.WriteLine("ModifyLinkAnnotDests Sample:"); using (Library lib = new Library()) { Console.WriteLine("Initialized the library."); // String sInput = Library.ResourceDirectory + "Sample_Input/Custom/IdexxJoeParzel/url_issue_1.pdf"; // Example of Action with javascript launch // String sOutput = "../url_issue_1-out.pdf"; // String sInput = Library.ResourceDirectory + "Sample_Input/Custom/IdexxJoeParzel/url_issue_2.pdf"; // Example of Actiom with URI (annot 2) String sOutput = "../url_issue_2-out.pdf"; // if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sOutput = args[1]; } Console.WriteLine("Input file: " + sInput); Document doc = new Document(sInput); int totalPages = doc.NumPages; int pageNum = 0; int annotNum = 0; int numReplaced = 0; for (pageNum = 0; pageNum < doc.NumPages; pageNum++) { Page pg = doc.GetPage(pageNum); for (annotNum = 0; annotNum < pg.NumAnnotations; annotNum++) { Annotation ann = pg.GetAnnotation(annotNum); Console.WriteLine("Page: " + pageNum + " Annot: " + annotNum + " Title: " + ann.Title); if (ann is LinkAnnotation) { if (ann.PDFDict.Contains("A")) { Console.WriteLine("Action found"); PDFDict actionDict = (PDFDict)ann.PDFDict.Get("A"); //Console.WriteLine("JS = " + jsPDFString.ToString()); if (actionDict.Contains("JS")) { PDFString jsPDFString = (PDFString)actionDict.Get("JS"); Console.WriteLine("JS = " + jsPDFString.Value); PDFString newvalue = new PDFString("app.launchURL(\"https://www.datalogics.com\", true);", doc, false, false); actionDict.Put("JS", newvalue); numReplaced++; } if (actionDict.Contains("URI")) { PDFString uriPDFString = (PDFString)actionDict.Get("URI"); Console.WriteLine("URI = " + uriPDFString.Value); PDFString newvalue = new PDFString("https://www.datalogics.com", doc, false, false); actionDict.Put("URI", newvalue); numReplaced++; } } } } } // for page loop if (numReplaced > 0) { Console.WriteLine("Replaced " + numReplaced + " link destinations."); doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput); } } }