Example #1
0
        static void Main(string[] args)
        {
            PDFNet.Initialize();

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


            //------------------------------------------------------------------
            Console.WriteLine("Opening the test file...");

            try
            {
                // Here we create a SDF/Cos document directly from PDF file. In case you have
                // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
                using (SDFDoc doc = new SDFDoc(input_path + "fish.pdf"))
                {
                    doc.InitSecurityHandler();

                    Console.WriteLine("Modifying info dictionary, adding custom properties, embedding a stream...");

                    Obj trailer = doc.GetTrailer();                     // Get the trailer

                    // Now we will change PDF document information properties using SDF API

                    // Get the Info dictionary.
                    DictIterator itr = trailer.Find("Info");
                    Obj          info;
                    if (itr.HasNext())
                    {
                        info = itr.Value();
                        // Modify 'Producer' entry.
                        info.PutString("Producer", "PDFTron PDFNet");

                        // Read title entry (if it is present)
                        itr = info.Find("Author");
                        if (itr.HasNext())
                        {
                            info.PutString("Author", itr.Value().GetAsPDFText() + "- Modified");
                        }
                        else
                        {
                            info.PutString("Author", "Joe Doe");
                        }
                    }
                    else
                    {
                        // Info dict is missing.
                        info = trailer.PutDict("Info");
                        info.PutString("Producer", "PDFTron PDFNet");
                        info.PutString("Title", "My document");
                    }

                    // Create a custom inline dictionary within Info dictionary
                    Obj custom_dict = info.PutDict("My Direct Dict");

                    // Add some key/value pairs
                    custom_dict.PutNumber("My Number", 100);

                    Obj my_array = custom_dict.PutArray("My Array");

                    // Create a custom indirect array within Info dictionary
                    Obj custom_array = doc.CreateIndirectArray();
                    info.Put("My Indirect Array", custom_array);

                    // Create indirect link to root
                    custom_array.PushBack(trailer.Get("Root").Value());

                    // Embed a custom stream (file my_stream.txt).
                    MappedFile   embed_file = new MappedFile(input_path + "my_stream.txt");
                    FilterReader mystm      = new FilterReader(embed_file);
                    custom_array.PushBack(doc.CreateIndirectStream(mystm));

                    // Save the changes.
                    Console.WriteLine("Saving modified test file...");
                    doc.Save(output_path + "sdftest_out.pdf", 0, "%PDF-1.4");
                }

                Console.WriteLine("Test completed.");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Example #2
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            // Initialize PDFNet before calling any other PDFNet function.
            PDFNet.Initialize();

            string input_path     = "../../TestFiles/";
            string output_path    = "../../TestFiles/Output/";
            string input_filename = "US061222892-a.pdf";

            PDFDoc pdf_doc = new PDFDoc(input_path + input_filename);

            pdf_doc.InitSecurityHandler();

            SDFDoc cos_doc  = pdf_doc.GetSDFDoc();
            int    num_objs = cos_doc.XRefSize();

            for (int i = 1; i < num_objs; ++i)
            {
                Obj obj = cos_doc.GetObj(i);
                if (obj != null && !obj.IsFree() && obj.IsStream())
                {
                    // Process only images
                    DictIterator itr = obj.Find("Subtype");
                    if (!itr.HasNext() || itr.Value().GetName() != "Image")
                    {
                        continue;
                    }

                    pdftron.PDF.Image input_image = new pdftron.PDF.Image(obj);
                    pdftron.PDF.Image new_image   = null;

                    // Process only gray-scale images
                    if (input_image.GetComponentNum() != 1)
                    {
                        continue;
                    }

                    int bpc = input_image.GetBitsPerComponent();
                    if (bpc != 1)                     // Recompress 1 BPC images
                    {
                        continue;
                    }

                    // Skip images that are already compressed using JBIG2
                    itr = obj.Find("Filter");
                    if (itr.HasNext() && itr.Value().IsName() &&
                        itr.Value().GetName() == "JBIG2Decode")
                    {
                        continue;
                    }

                    FilterReader reader = new FilterReader(obj.GetDecodedStream());

                    ObjSet hint_set = new ObjSet();
                    Obj    hint     = hint_set.CreateArray();
                    hint.PushBackName("JBIG2");
                    hint.PushBackName("Lossless");
                    hint.PushBackName("Threshold");
                    hint.PushBackNumber(0.4);
                    hint.PushBackName("SharePages");
                    hint.PushBackNumber(10000);

                    new_image = pdftron.PDF.Image.Create(
                        cos_doc,
                        reader,
                        input_image.GetImageWidth(),
                        input_image.GetImageHeight(),
                        1,
                        ColorSpace.CreateDeviceGray(),
                        hint                          // A hint to image encoder to use JBIG2 compression
                        );

                    Obj new_img_obj = new_image.GetSDFObj();

                    // Copy any important entries from the image dictionary
                    itr = obj.Find("ImageMask");
                    if (itr.HasNext())
                    {
                        new_img_obj.Put("ImageMask", itr.Value());
                    }

                    itr = obj.Find("Mask");
                    if (itr.HasNext())
                    {
                        new_img_obj.Put("Mask", itr.Value());
                    }

                    cos_doc.Swap(i, new_image.GetSDFObj().GetObjNum());
                }
            }

            pdf_doc.Save(output_path + "US061222892_JBIG2.pdf", SDFDoc.SaveOptions.e_remove_unused);
            pdf_doc.Close();
        }
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            // Example 1:
            // Extract images by traversing the display list for
            // every page. With this approach it is possible to obtain
            // image positioning information and DPI.
            try
            {
                using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                    using (ElementReader reader = new ElementReader())
                    {
                        doc.InitSecurityHandler();

                        PageIterator itr;
                        for (itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
                        {
                            reader.Begin(itr.Current());
                            ImageExtract(doc, reader);
                            reader.End();
                        }

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

            Console.WriteLine("----------------------------------------------------------------");

            // Example 2:
            // Extract images by scanning the low-level document.
            try
            {
                using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                {
                    doc.InitSecurityHandler();
                    image_counter = 0;

                    SDFDoc cos_doc  = doc.GetSDFDoc();
                    int    num_objs = cos_doc.XRefSize();
                    for (int i = 1; i < num_objs; ++i)
                    {
                        Obj obj = cos_doc.GetObj(i);
                        if (obj != null && !obj.IsFree() && obj.IsStream())
                        {
                            // Process only images
                            DictIterator itr = obj.Find("Subtype");
                            if (!itr.HasNext() || itr.Value().GetName() != "Image")
                            {
                                continue;
                            }

                            itr = obj.Find("Type");
                            if (!itr.HasNext() || itr.Value().GetName() != "XObject")
                            {
                                continue;
                            }

                            pdftron.PDF.Image image = new pdftron.PDF.Image(obj);

                            Console.WriteLine("--> Image: {0}", ++image_counter);
                            Console.WriteLine("    Width: {0}", image.GetImageWidth());
                            Console.WriteLine("    Height: {0}", image.GetImageHeight());
                            Console.WriteLine("    BPC: {0}", image.GetBitsPerComponent());

                            string fname = output_path + "image_extract2_" + image_counter.ToString();
                            image.Export(fname);                              // or ExporAsPng() or ExporAsTiff() ...

                            // Convert PDF bitmap to GDI+ Bitmap...
                            //Bitmap bmp = image.GetBitmap();
                            //bmp.Save(fname, ImageFormat.Png);
                            //bmp.Dispose();

                            // Instead of converting PDF images to a Bitmap, you can also extract
                            // uncompressed/compressed image data directly using element.GetImageData()
                            // as illustrated in ElementReaderAdv sample project.
                        }
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }