Esempio n. 1
0
        static PatternColor CreateAxialShading(PDFDoc doc)
        {
            // Create a new Shading object ------------
            Obj pattern_dict = doc.CreateIndirectDict();

            // Initialize pattern dictionary. For details on what each parameter represents
            // please refer to Tables 4.30 and 4.26 in PDF Reference Manual
            pattern_dict.PutName("Type", "Pattern");
            pattern_dict.PutNumber("PatternType", 2);             // 2 stands for shading

            Obj shadingDict = pattern_dict.PutDict("Shading");

            shadingDict.PutNumber("ShadingType", 2);
            shadingDict.PutName("ColorSpace", "DeviceCMYK");

            // Set the coordinates of the axial shading to the output
            shadingDict.PutRect("Coords", 0, 0, 612, 794);

            // Set the Functions for the axial shading
            Obj funct = shadingDict.PutDict("Function");
            Obj C0    = funct.PutArray("C0");

            C0.PushBackNumber(1);
            C0.PushBackNumber(0);
            C0.PushBackNumber(0);
            C0.PushBackNumber(0);

            Obj C1 = funct.PutArray("C1");

            C1.PushBackNumber(0);
            C1.PushBackNumber(1);
            C1.PushBackNumber(0);
            C1.PushBackNumber(0);

            Obj domain = funct.PutArray("Domain");

            domain.PushBackNumber(0);
            domain.PushBackNumber(1);

            funct.PutNumber("FunctionType", 2);
            funct.PutNumber("N", 1);

            return(new PatternColor(pattern_dict));
        }
        static void AnnotationLowLevelAPI(PDFDoc doc)
        {
            Page page = doc.GetPage(1);

            Obj annots = page.GetAnnots();

            if (annots == null)
            {
                // If there are no annotations, create a new annotation
                // array for the page.
                annots = doc.CreateIndirectArray();
                page.GetSDFObj().Put("Annots", annots);
            }

            // Create the Text annotation
            Obj text_annot = doc.CreateIndirectDict();

            text_annot.PutName("Subtype", "Text");
            text_annot.PutBool("Open", true);
            text_annot.PutString("Contents", "The quick brown fox ate the lazy mouse.");
            text_annot.PutRect("Rect", 266, 116, 430, 204);

            // Insert the annotation in the page annotation array
            annots.PushBack(text_annot);

            // Create a Link annotation
            Obj link1 = doc.CreateIndirectDict();

            link1.PutName("Subtype", "Link");
            Destination dest = Destination.CreateFit(doc.GetPage(2));

            link1.Put("Dest", dest.GetSDFObj());
            link1.PutRect("Rect", 85, 705, 503, 661);
            annots.PushBack(link1);

            // Create another Link annotation
            Obj link2 = doc.CreateIndirectDict();

            link2.PutName("Subtype", "Link");
            Destination dest2 = Destination.CreateFit(doc.GetPage(3));

            link2.Put("Dest", dest2.GetSDFObj());
            link2.PutRect("Rect", 85, 638, 503, 594);
            annots.PushBack(link2);

            // Note that PDFNet APi can be used to modify existing annotations.
            // In the following example we will modify the second link annotation
            // (link2) so that it points to the 10th page. We also use a different
            // destination page fit type.

            link2.Put("Dest",
                      Destination.CreateXYZ(doc.GetPage(10), 100, 792 - 70, 10).GetSDFObj());

            // Create a third link annotation with a hyperlink action (all other
            // annotation types can be created in a similar way)
            Obj link3 = doc.CreateIndirectDict();

            link3.PutName("Subtype", "Link");
            link3.PutRect("Rect", 85, 570, 503, 524);

            // Create a URI action
            Obj action = link3.PutDict("A");

            action.PutName("S", "URI");
            action.PutString("URI", "http://www.pdftron.com");
            annots.PushBack(link3);
        }
Esempio n. 3
0
        static void Create3DAnnotation(PDFDoc doc, Obj annots)
        {
            // ---------------------------------------------------------------------------------
            // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
            // for collections of three-dimensional objects, such as those used by CAD software,
            // to be embedded in PDF files.
            Obj link_3D = doc.CreateIndirectDict();

            link_3D.PutName("Subtype", "3D");

            // Annotation location on the page
            Rect bbox = new Rect(25, 180, 585, 643);

            link_3D.PutRect("Rect", bbox.x1, bbox.y1, bbox.x2, bbox.y2);
            annots.PushBack(link_3D);

            // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
            // that determines how the state of the annotation and its associated artwork can change.
            Obj activation_dict_3D = link_3D.PutDict("3DA");

            // Set the annotation so that it is activated as soon as the page containing the
            // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
            activation_dict_3D.PutName("A", "PO");

            // Embed U3D Streams (3D Model/Artwork).
            MappedFile   u3d_file   = new MappedFile(input_path + "dice.u3d");
            FilterReader u3d_reader = new FilterReader(u3d_file);

            Obj u3d_data_dict = doc.CreateIndirectStream(u3d_reader);

            u3d_data_dict.PutName("Subtype", "U3D");
            link_3D.Put("3DD", u3d_data_dict);

            // Set the initial view of the 3D artwork that should be used when the annotation is activated.
            Obj view3D_dict = link_3D.PutDict("3DV");

            view3D_dict.PutString("IN", "Unnamed");
            view3D_dict.PutString("XN", "Default");
            view3D_dict.PutName("MS", "M");
            view3D_dict.PutNumber("CO", 27.5);

            // A 12-element 3D transformation matrix that specifies a position and orientation
            // of the camera in world coordinates.
            Obj tr3d = view3D_dict.PutArray("C2W");

            tr3d.PushBackNumber(1); tr3d.PushBackNumber(0); tr3d.PushBackNumber(0);
            tr3d.PushBackNumber(0); tr3d.PushBackNumber(0); tr3d.PushBackNumber(-1);
            tr3d.PushBackNumber(0); tr3d.PushBackNumber(1); tr3d.PushBackNumber(0);
            tr3d.PushBackNumber(0); tr3d.PushBackNumber(-27.5); tr3d.PushBackNumber(0);

            // Create annotation appearance stream, a thumbnail which is used during printing or
            // in PDF processors that do not understand 3D data.
            Obj            ap_dict = link_3D.PutDict("AP");
            ElementBuilder builder = new ElementBuilder();
            ElementWriter  writer  = new ElementWriter();

            writer.Begin(doc);

            writer.WritePlacedElement(builder.CreateImage(
                                          Image.Create(doc, input_path + "dice.jpg"),
                                          0, 0, bbox.Width(), bbox.Height()));

            Obj normal_ap_stream = writer.End();

            normal_ap_stream.PutName("Subtype", "Form");
            normal_ap_stream.PutRect("BBox", 0, 0, bbox.Width(), bbox.Height());
            ap_dict.Put("N", normal_ap_stream);
        }
Esempio n. 4
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            // Relative path to the folder containing test files.
            string input_path  = "../../TestFiles/";
            string output_path = "../../TestFiles/Output/";


            // The following example illustrates how to create and edit the outline tree
            // using high-level Bookmark methods.
            try
            {
                using (PDFDoc doc = new PDFDoc(input_path + "numbered.pdf"))
                {
                    doc.InitSecurityHandler();

                    // Lets first create the root bookmark items.
                    Bookmark red   = Bookmark.Create(doc, "Red");
                    Bookmark green = Bookmark.Create(doc, "Green");
                    Bookmark blue  = Bookmark.Create(doc, "Blue");

                    doc.AddRootBookmark(red);
                    doc.AddRootBookmark(green);
                    doc.AddRootBookmark(blue);

                    // You can also add new root bookmarks using Bookmark.AddNext("...")
                    blue.AddNext("foo");
                    blue.AddNext("bar");

                    // We can now associate new bookmarks with page destinations:

                    // The following example creates an 'explicit' destination (see
                    // section '8.2.1 Destinations' in PDF Reference for more details)
                    Destination red_dest = Destination.CreateFit(doc.GetPage(1));
                    red.SetAction(pdftron.PDF.Action.CreateGoto(red_dest));

                    // Create an explicit destination to the first green page in the document
                    green.SetAction(pdftron.PDF.Action.CreateGoto(
                                        Destination.CreateFit(doc.GetPage(10))));

                    // The following example creates a 'named' destination (see
                    // section '8.2.1 Destinations' in PDF Reference for more details)
                    // Named destinations have certain advantages over explicit destinations.
                    String             key         = "blue1";
                    pdftron.PDF.Action blue_action = pdftron.PDF.Action.CreateGoto(key,
                                                                                   Destination.CreateFit(doc.GetPage(19)));

                    blue.SetAction(blue_action);

                    // We can now add children Bookmarks
                    Bookmark sub_red1 = red.AddChild("Red - Page 1");
                    sub_red1.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(1))));
                    Bookmark sub_red2 = red.AddChild("Red - Page 2");
                    sub_red2.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(2))));
                    Bookmark sub_red3 = red.AddChild("Red - Page 3");
                    sub_red3.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(3))));
                    Bookmark sub_red4 = sub_red3.AddChild("Red - Page 4");
                    sub_red4.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(4))));
                    Bookmark sub_red5 = sub_red3.AddChild("Red - Page 5");
                    sub_red5.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(5))));
                    Bookmark sub_red6 = sub_red3.AddChild("Red - Page 6");
                    sub_red6.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(6))));

                    // Example of how to find and delete a bookmark by title text.
                    Bookmark foo = doc.GetFirstBookmark().Find("foo");
                    if (foo.IsValid())
                    {
                        foo.Delete();
                    }

                    Bookmark bar = doc.GetFirstBookmark().Find("bar");
                    if (bar.IsValid())
                    {
                        bar.Delete();
                    }

                    // Adding color to Bookmarks. Color and other formatting can help readers
                    // get around more easily in large PDF documents.
                    red.SetColor(1, 0, 0);
                    green.SetColor(0, 1, 0);
                    green.SetFlags(2);                                          // set bold font
                    blue.SetColor(0, 0, 1);
                    blue.SetFlags(3);                                           // set bold and italic

                    doc.Save(output_path + "bookmark.pdf", 0);
                    Console.WriteLine("Done. Result saved in bookmark.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }


            // The following example illustrates how to traverse the outline tree using
            // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
            // Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
            try
            {
                // Open the document that was saved in the previous code sample
                using (PDFDoc doc = new PDFDoc(output_path + "bookmark.pdf"))
                {
                    doc.InitSecurityHandler();

                    Bookmark root = doc.GetFirstBookmark();
                    PrintOutlineTree(root);

                    Console.WriteLine("Done.");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            // The following example illustrates how to create a Bookmark to a page
            // in a remote document. A remote go-to action is similar to an ordinary
            // go-to action, but jumps to a destination in another PDF file instead
            // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
            // Reference Manual for details.
            try
            {
                using (PDFDoc doc = new PDFDoc(output_path + "bookmark.pdf"))
                {
                    doc.InitSecurityHandler();

                    // Create file specification (the file referred to by the remote bookmark)
                    Obj file_spec = doc.CreateIndirectDict();
                    file_spec.PutName("Type", "Filespec");
                    file_spec.PutString("F", "bookmark.pdf");

                    FileSpec           spec        = new FileSpec(file_spec);
                    pdftron.PDF.Action goto_remote = pdftron.PDF.Action.CreateGotoRemote(spec, 5, true);

                    Bookmark remoteBookmark1 = Bookmark.Create(doc, "REMOTE BOOKMARK 1");
                    remoteBookmark1.SetAction(goto_remote);
                    doc.AddRootBookmark(remoteBookmark1);

                    // Create another remote bookmark, but this time using the low-level SDF/Cos API.
                    Bookmark remoteBookmark2 = Bookmark.Create(doc, "REMOTE BOOKMARK 2");
                    doc.AddRootBookmark(remoteBookmark2);
                    Obj gotoR = remoteBookmark2.GetSDFObj().PutDict("A");
                    {                                // Create the 'Action' dictionary.
                        gotoR.PutName("S", "GoToR"); // Set action type
                        gotoR.PutBool("NewWindow", true);

                        // Set the file specification
                        gotoR.Put("F", file_spec);

                        // Set the destination.
                        Obj dest = gotoR.PutArray("D");
                        dest.PushBackNumber(9);                          // jump to the tenth page. Note that Acrobat indexes pages from 0.
                        dest.PushBackName("Fit");                        // Fit the page
                    }

                    doc.Save(output_path + "bookmark_remote.pdf", SDFDoc.SaveOptions.e_linearized);
                    Console.WriteLine("Done. Result saved in bookmark_remote.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }