Ejemplo n.º 1
0
        /// <summary>
        //-----------------------------------------------------------------------------------
        // This sample illustrates how to embed various raster image formats
        // (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
        //-----------------------------------------------------------------------------------
        /// </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/";

            try
            {
                using (PDFDoc doc = new PDFDoc())
                    using (ElementBuilder bld = new ElementBuilder())      // Used to build new Element objects
                        using (ElementWriter writer = new ElementWriter()) // Used to write Elements to the page
                        {
                            Page page = doc.PageCreate();                  // Start a new page
                            writer.Begin(page);                            // Begin writing to this page

                            // ----------------------------------------------------------
                            // Embed a JPEG image to the output document.
                            Image img = Image.Create(doc, input_path + "peppers.jpg");

                            // You can also directly add any .NET Bitmap. The following commented-out code
                            // is equivalent to the above line:
                            //    System.Drawing.Bitmap bmp;
                            //    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(input_path + "peppers.jpg");
                            //    Image img = Image.Create(doc, bmp);

                            Element element = bld.CreateImage(img, 50, 500, img.GetImageWidth() / 2, img.GetImageHeight() / 2);
                            writer.WritePlacedElement(element);

                            // ----------------------------------------------------------
                            // Add a PNG image to the output file
                            img     = Image.Create(doc, input_path + "butterfly.png");
                            element = bld.CreateImage(img, new Matrix2D(100, 0, 0, 100, 300, 500));
                            writer.WritePlacedElement(element);

                            // ----------------------------------------------------------
                            // Add a GIF image to the output file
                            img     = Image.Create(doc, input_path + "pdfnet.gif");
                            element = bld.CreateImage(img, new Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350));
                            writer.WritePlacedElement(element);

                            // ----------------------------------------------------------
                            // Add a TIFF image to the output file
                            img     = Image.Create(doc, input_path + "grayscale.tif");
                            element = bld.CreateImage(img, new Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50));
                            writer.WritePlacedElement(element);

                            writer.End();           // Save the page
                            doc.PagePushBack(page); // Add the page to the document page sequence

                            // ----------------------------------------------------------
                            // Add a BMP image to the output file

                            /*
                             * bmp = new System.Drawing.Bitmap(input_path + "pdftron.bmp");
                             *                  img = Image.Create(doc, bmp);
                             *                  element = bld.CreateImage(img, new Matrix2D(bmp.Width, 0, 0, bmp.Height, 255, 700));
                             *                  writer.WritePlacedElement(element);
                             *
                             *                  writer.End();	// Finish writing to the page
                             *                  doc.PagePushBack(page);
                             */

                            // ----------------------------------------------------------
                            // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.

                            page = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 612, 794));
                            writer.Begin(page); // begin writing to this page

                            // Note: encoder hints can be used to select between different compression methods.
                            // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
                            ObjSet hint_set = new ObjSet();
                            Obj    enc      = hint_set.CreateArray(); // Initialize encoder 'hint' parameter
                            enc.PushBackName("JBIG2");
                            enc.PushBackName("Lossy");

                            img     = pdftron.PDF.Image.Create(doc, input_path + "multipage.tif", enc);
                            element = bld.CreateImage(img, new Matrix2D(612, 0, 0, 794, 0, 0));
                            writer.WritePlacedElement(element);

                            writer.End();           // Save the page
                            doc.PagePushBack(page); // Add the page to the document page sequence

                            // ----------------------------------------------------------
                            // Add a JPEG2000 (JP2) image to the output file

                            // Create a new page
                            page = doc.PageCreate();
                            writer.Begin(page); // Begin writing to the page

                            // Embed the image.
                            img = pdftron.PDF.Image.Create(doc, input_path + "palm.jp2");

                            // Position the image on the page.
                            element = bld.CreateImage(img, new Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80));
                            writer.WritePlacedElement(element);

                            // Write 'JPEG2000 Sample' text string under the image.
                            writer.WriteElement(bld.CreateTextBegin(pdftron.PDF.Font.Create(doc, pdftron.PDF.Font.StandardType1Font.e_times_roman), 32));
                            element = bld.CreateTextRun("JPEG2000 Sample");
                            element.SetTextMatrix(1, 0, 0, 1, 190, 30);
                            writer.WriteElement(element);
                            writer.WriteElement(bld.CreateTextEnd());

                            writer.End(); // Finish writing to the page
                            doc.PagePushBack(page);


                            doc.Save(output_path + "addimage.pdf", SDFDoc.SaveOptions.e_linearized);
                            Console.WriteLine("Done. Result saved in addimage.pdf...");
                        }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 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();
        }