private void updateDictionaryKey(PDFDict pdfDict, string newKey, string oldKey)
        {
            PDFObject valueInDict = pdfDict.Get(oldKey);

            pdfDict.Remove(oldKey);
            pdfDict.Put(newKey, valueInDict);
        }
Exemple #2
0
        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);
            }
        }
 private void updateDictionaryValue(PDFDict pdfDict, string key, PDFObject newValue)
 {
     pdfDict.Remove(key);
     pdfDict.Put(key, newValue);
 }
        static void Main(string[] args)
        {
            Console.WriteLine("FlashAnnotCreate Sample:");

            using (Library lib = new Library())
            {
                Console.WriteLine("Initialized the library.");

                // Names of the flash-format file to embed.
                String sInput1 = "../../Resources/Sample_Input/JobReady-info.swf";
                // Name of the PDF file to use for the annotation appearance - the
                // first page of the PDF is imported
                String sInput2 = "../../Resources/Sample_Input/cottage.pdf";
                String sOutput = "../FlashAnnotCreate-out.pdf";

                if (args.Length > 0)
                {
                    sInput1 = args[0];
                }

                if (args.Length > 1)
                {
                    sInput2 = args[1];
                }

                if (args.Length > 2)
                {
                    sOutput = args[2];
                }

                Console.WriteLine("Using flash file " + sInput1 + " and generating appearance from " + sInput2 + ", saving output file : " + sOutput);

                // Create a document and a 5" x 4" page for the Flash annotation
                Document doc      = new Document();
                Rect     mediaBox = new Rect(0, 0, 5 * 72, 4 * 72);
                Page     page     = doc.CreatePage(Document.BeforeFirstPage, mediaBox);

                // Set the annotation to be 3" x 2", with its lower-left corner 1" over and 1" up
                // from the bottom-left corner of the page.
                //
                // The size of the annotation should match the design size of your flash file.
                // If the two are drastically disparate, oddities with object placement in the
                // flash file may result.
                Rect annotRect = new Rect(1 * 72, 3 * 72, 4 * 72, 1 * 72);

                // Create the annotation and acquire the COS-layer representation.
                // Most of the specific functionality for annotations is set via
                // direct manipulation of the COS-layer representation of the annotation */
                Annotation newAnnot = new Annotation(page, "Screen", annotRect);
                PDFDict    cosAnnot = newAnnot.PDFDict;

                // Set the annotation title and flags
                newAnnot.Flags = AnnotationFlags.Print | AnnotationFlags.ReadOnly;
                newAnnot.Title = "Flash annotation sample";

                // There are several layers of objects to be created and nested in order to make
                // an annotation that plays Flash file content. The 2 major tasks are creating
                // the appearance for the annotation, and creating the annotation's action:
                // the appearance is what's shown before the Flash file is clicked on & invoked,
                // whereas the action states what to do in order to play the Flash file. */

                // The annotation action has many subdictionaries that are required.
                // This example starts by creating the topmost item:
                // set the action type to Rendition for SWF (Flash) files
                Datalogics.PDFL.Action action = new Datalogics.PDFL.Action(doc, "Rendition");
                PDFDict actionDict            = action.PDFDict;
                // Make a back-link to the annotation
                actionDict.Put("AN", cosAnnot);

                // Specify what to do: in this case, the OP value of 0 means
                // if no rendition is associated with the annotation specified
                // by AN, play the rendition specified by R, associating it
                // with the annotation. If a rendition is already associated
                // with the annotation, it is stopped, and the new rendition
                // is associated with the annotation.
                actionDict.Put("OP", new PDFInteger(0, doc, false));

                // Make a rendition object to use for playing:
                PDFDict rendObj = new PDFDict(doc, true);
                // Rendition object is of the "media rendition" type
                rendObj.Put("S", new PDFName("MR", doc, false));

                // Add a name for the rendition (optional)
                rendObj.Put("N", new PDFString("Rendition for Flash embedding sample", doc, false, false));

                // Make the media clip dictionary:
                PDFDict mediaClipObj = new PDFDict(doc, true);

                // Required: this is a media clip data object:
                mediaClipObj.Put("S", new PDFName("MCD", doc, false));

                // Specify what sort of media clip this is (MIME type):
                mediaClipObj.Put("CT", new PDFString("application/x-shockwave-flash", doc, false, false));

                // Add a permissions dictionary (for making temp files for playback)
                PDFDict permObj = new PDFDict(doc, false);
                // Indicate a temp file may be made to play the rendition:
                permObj.Put("TF", new PDFString("TEMPACCESS", doc, false, false));
                // Add the permissions dictionary to the rendition
                mediaClipObj.Put("P", permObj);

                PDFStream fileStmObj = new PDFStream(new System.IO.FileStream(sInput1, System.IO.FileMode.Open), doc, new PDFDict(doc, false), new PDFArray(doc, false));

                // Make a new file reference
                PDFDict fileRefObj = new PDFDict(doc, true);
                // Which needs an embedded file
                PDFDict efObj = new PDFDict(doc, false);
                // Add the filestream above to the embedded file dict
                efObj.Put("F", fileStmObj);
                // Add the embedded file to the file reference
                fileRefObj.Put("EF", efObj);
                // Set the type of this object
                fileRefObj.Put("Type", new PDFName("Filespec", doc, false));
                // And add the actual file name this was based upon
                fileRefObj.Put("F", new PDFString(sInput1, doc, false, false));

                // Place the file specification in the media clip dictionary:
                mediaClipObj.Put("D", fileRefObj);

                // Associate the media clip dictionary with the rendition:
                rendObj.Put("C", mediaClipObj);

                // Associate the rendition with the action:
                actionDict.Put("R", rendObj);

                // Associate the action with the annotation:
                cosAnnot.Put("A", actionDict);

                try
                {
                    // The other major part is creating the appearance for the annotation.
                    // This example will import a PDF page and use it's first page as the appearance.
                    using (Document importPDDoc = new Document(sInput2))
                    {
                        Page    importPDPage = importPDDoc.GetPage(0);
                        Content tempContent  = new Content();

                        // The page is added to the PDE Content in order to acquire the PDEForm
                        // created in the process. This PDEForm will become the basis of the
                        // annotation appearance.
                        //
                        // NOTE: the actual appearance of the page in the annotation will be scaled to fit
                        // within the boundaries of the annotation - so, if the page being used is of drastically
                        // different x/y proportions from the annotation, it will appear distorted. */
                        tempContent.AddPage(Content.BeforeFirst, doc, importPDPage, null, null, 0, null);

                        if (tempContent.NumElements == 1 && tempContent.GetElement(0) is Form)
                        {
                            Form annotAPForm = tempContent.GetElement(0) as Form;
                            // Place the form XObject as the "normal" appearance in an appearance dictionary
                            PDFDict apDict = new PDFDict(doc, false);
                            apDict.Put("N", annotAPForm.Stream);
                            // And place the appearance dictionary in the annotation
                            cosAnnot.Put("AP", apDict);
                        }
                        else
                        {
                            Console.WriteLine("Unexpected page import result. Annotation will have no appearance.");
                        }
                    }
                }
                catch (ApplicationException ex)
                {
                    if (ex.Message.StartsWith("PDF"))
                    {
                        Console.WriteLine("Exception %x (%s) while importing annotation appearance:");
                        Console.WriteLine(ex.Message);
                        Console.WriteLine("* Annotation will not have a visible appearance but is still in PDF file");
                    }
                    else
                    {
                        throw;
                    }
                }

                // Create a backlink to the page this annotation appears on
                // in the annotation. This is required for Screen annotations
                // with rendition actions.
                cosAnnot.Put("P", page.PDFDict);

                page.Dispose();

                doc.Save(SaveFlags.Full, sOutput);
            }
        }
        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);
                }
            }
        }