Beispiel #1
0
        static void AddPackage(PDFDoc doc, string file, string desc)
        {
            NameTree files = NameTree.Create(doc, "EmbeddedFiles");
            FileSpec fs    = FileSpec.Create(doc, file, true);

            byte[] file1_name = System.Text.Encoding.UTF8.GetBytes(file);
            files.Put(file1_name, fs.GetSDFObj());
            fs.GetSDFObj().PutText("Desc", desc);

            Obj collection = doc.GetRoot().FindObj("Collection");

            if (collection == null)
            {
                collection = doc.GetRoot().PutDict("Collection");
            }

            // You could here manipulate any entry in the Collection dictionary.
            // For example, the following line sets the tile mode for initial view mode
            // Please refer to section '2.3.5 Collections' in PDF Reference for details.
            collection.PutName("View", "T");
        }
        static void Main(string[] args)
        {
            Console.WriteLine("NameTree Sample:");
            using (Library lib = new Library())
            {
                Console.WriteLine("Initialized the library.");

                // Create a new document and blank first page
                Document doc  = new Document();
                Rect     rect = new Rect(0, 0, 612, 792);
                Page     page = doc.CreatePage(Document.BeforeFirstPage, rect);
                Console.WriteLine("Created new document and first page.");

                // Create a NumberTree and put a key-value pair in it
                NameTree nametree = new NameTree(doc);

                // Use put() to put a key-value pair in it
                PDFString key   = new PDFString("Bailout", doc, false, false);;
                PDFString value = new PDFString("Smorgasbord", doc, false, false);
                nametree.Put(key, value);
                Console.WriteLine("\nCreated NameTree and added first key-value pair.");

                // Put another key-value pair in it
                nametree.Put(new PDFString("Brandish", doc, false, false), new PDFString("Copasetic", doc, false, false));

                // Retrieve second entry
                PDFString keysearchstring = new PDFString("Brandish", doc, false, false);
                PDFObject lookup          = nametree.Get(keysearchstring);
                Console.WriteLine("\nRetrieving two entries:");
                Console.WriteLine(lookup);

                // Retrieve first entry
                lookup = nametree.Get(new PDFString("Bailout", doc, false, false));
                Console.WriteLine(lookup);

                // Use remove() method to remove first entry
                nametree.Remove(new PDFString("Bailout", doc, false, false));

                // Get both entries, and demonstrate that first is now gone
                Console.WriteLine("\nAfter removing entry 1, we now have:");
                lookup = nametree.Get(new PDFString("Bailout", doc, false, false));
                Console.WriteLine(lookup);
                lookup = nametree.Get(new PDFString("Brandish", doc, false, false));
                Console.WriteLine(lookup);

                // Create two new NameTrees and set each to our original numbertree
                NameTree abc = nametree;
                NameTree xyz = nametree;

                // Show they are equal
                if (abc.Equals(xyz))
                {
                    Console.WriteLine("\nThe two NameTrees abc and xyz are the same");
                }
                else
                {
                    Console.WriteLine("\nThe two NameTrees abc and xyz are not the same");
                }

                // Create two new NameTrees
                abc = new NameTree(doc);
                xyz = new NameTree(doc);

                // Show they are not equal
                if (abc.Equals(xyz))
                {
                    Console.WriteLine("\nThe two NameTrees abc and xyz are the same");
                }
                else
                {
                    Console.WriteLine("\nThe two NameTrees abc and xyz are not the same");
                }

                // Get the PDFDict from the NameTree
                PDFDict dict = nametree.PDFDict;
                Console.WriteLine("\nThe PDFDict from the NameTree:");
                Console.WriteLine(dict);

                // Kill the NameTree object
                nametree.Dispose();
                Console.WriteLine("\nKilled the NameTree object.");

                //////////////////////////////////////////////////
                // Now use Document methods to operate on NameTree
                //////////////////////////////////////////////////

                // Create a NameTree in the document using createNameTree() method
                NameTree docCreatedNameTree = doc.CreateNameTree("MyNameTree");
                docCreatedNameTree.Put(new PDFString("Argyle", doc, false, false), new PDFString("Seamstress", doc, false, false));
                Console.WriteLine("Created a NameTree object in the document.");

                // Look for the NameTree in the document by using the getNameTree() method
                Console.WriteLine("\nTwo searches for NameTree using getNameTree() method; first fails, second succeeeds:");
                docCreatedNameTree = doc.GetNameTree("Garbage");
                Console.WriteLine(docCreatedNameTree);
                docCreatedNameTree = doc.GetNameTree("MyNameTree");
                Console.WriteLine(docCreatedNameTree);

                // Remove the NameTree from the document by using the remove NameTree() method
                Console.WriteLine("\nRemove the NameTree from the document.");
                doc.RemoveNameTree("Garbage");
                docCreatedNameTree = doc.GetNameTree("MyNameTree");
                Console.WriteLine(docCreatedNameTree);
                doc.RemoveNameTree("MyNameTree");
                docCreatedNameTree = doc.GetNameTree("MyNameTree");
                Console.WriteLine(docCreatedNameTree);

                // Kill the doc object
                doc.Dispose();
            }
        }