Ejemplo n.º 1
0
        static Boolean ConvertSpecificFormats()
        {
            //////////////////////////////////////////////////////////////////////////
            Boolean err = false;

            try
            {
                using (PDFDoc pdfdoc = new PDFDoc())
                {
                    Console.WriteLine("Converting from XPS");

                    pdftron.PDF.Convert.FromXps(pdfdoc, inputPath + "simple-xps.xps");
                    pdfdoc.Save(outputPath + "xps2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
                    Console.WriteLine("Saved xps2pdf v2.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc())
                {
                    Console.WriteLine("Converting from EMF");
                    pdftron.PDF.Convert.FromEmf(pdfdoc, inputPath + "simple-emf.emf");
                    pdfdoc.Save(outputPath + "emf2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
                    Console.WriteLine("Saved emf2pdf v2.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc())
                {
                    // Add a dictionary
                    ObjSet set     = new ObjSet();
                    Obj    options = set.CreateDict();

                    // Put options
                    options.PutNumber("FontSize", 15);
                    options.PutBool("UseSourceCodeFormatting", true);
                    options.PutNumber("PageWidth", 12);
                    options.PutNumber("PageHeight", 6);

                    // Convert from .txt file
                    Console.WriteLine("Converting from txt");
                    pdftron.PDF.Convert.FromText(pdfdoc, inputPath + "simple-text.txt", options);
                    pdfdoc.Save(outputPath + "simple-text.pdf", SDFDoc.SaveOptions.e_remove_unused);
                    Console.WriteLine("Saved simple-text.pdf");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                using (PDFDoc pdfdoc = new PDFDoc(inputPath + "newsletter.pdf"))
                {
                    // Convert PDF document to SVG
                    Console.WriteLine("Converting pdfdoc to SVG");
                    pdftron.PDF.Convert.ToSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
                    Console.WriteLine("Saved pdf2svg v2.svg");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PNG image to XPS
                Console.WriteLine("Converting PNG to XPS");
                pdftron.PDF.Convert.ToXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
                Console.WriteLine("Saved butterfly.xps");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                // Convert MSWord document to XPS
                Console.WriteLine("Converting DOCX to XPS");
                pdftron.PDF.Convert.ToXps(inputPath + "simple-word_2007.docx", outputPath + "simple-word_2007.xps");
                Console.WriteLine("Saved simple-word_2007.xps");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to XPS
                Console.WriteLine("Converting PDF to XPS");
                pdftron.PDF.Convert.ToXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
                Console.WriteLine("Saved newsletter.xps");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to HTML
                Console.WriteLine("Converting PDF to HTML");
                pdftron.PDF.Convert.ToHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
                Console.WriteLine("Saved newsletter as HTML");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to EPUB
                Console.WriteLine("Converting PDF to EPUB");
                pdftron.PDF.Convert.ToEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
                Console.WriteLine("Saved newsletter.epub");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            //////////////////////////////////////////////////////////////////////////
            try
            {
                // Convert PDF document to multipage TIFF
                Console.WriteLine("Converting PDF to multipage TIFF");
                pdftron.PDF.Convert.TiffOutputOptions tiff_options = new pdftron.PDF.Convert.TiffOutputOptions();
                tiff_options.SetDPI(200);
                tiff_options.SetDither(true);
                tiff_options.SetMono(true);
                pdftron.PDF.Convert.ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
                Console.WriteLine("Saved newsletter.tiff");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
                err = true;
            }

            return(err);
        }
Ejemplo n.º 2
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.º 3
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            // The first step in every application using PDFNet is to initialize the
            // library and set the path to common PDF resources. The library is usually
            // initialized only once, but calling Initialize() multiple times is also fine.
            PDFNet.Initialize();

            try
            {
                // Optional: Set ICC color profiles to fine tune color conversion
                // for PDF 'device' color spaces. You can use your own ICC profiles.
                // Standard Adobe color profiles can be download from Adobes site:
                // http://www.adobe.com/support/downloads/iccprofiles/iccprofiles_win.html
                //
                // Simply drop all *.icc files in PDFNet resource folder or you specify
                // the full pathname.
                //---
                // PDFNet.SetResourcesPath("../../../../../resources");
                // PDFNet.SetColorManagement();
                // PDFNet.SetDefaultDeviceCMYKProfile("USWebCoatedSWOP.icc"); // will search in PDFNet resource folder.
                // PDFNet.SetDefaultDeviceRGBProfile("AdobeRGB1998.icc");

                // Optional: Set predefined font mappings to override default font
                // substitution for documents with missing fonts. For example:
                //---
                // PDFNet.AddFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf");
                // PDFNet.AddFontSubst("StoneSans", "comic.ttf");  // search for 'comic.ttf' in PDFNet resource folder.
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf");
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf");
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf");
                //
                // If fonts are in PDFNet resource folder, it is not necessary to specify
                // the full path name. For example,
                //---
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Korea1, "AdobeMyungjoStd-Medium.otf");
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_CNS1, "AdobeSongStd-Light.otf");
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_GB1, "AdobeMingStd-Light.otf");
            }
            catch (Exception)
            {
                Console.WriteLine("The specified color profile was not found.");
            }

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


            using (PDFDraw draw = new PDFDraw())
            {
                //--------------------------------------------------------------------------------
                // Example 1) Convert the first PDF page to PNG at 92 DPI.
                // A three step tutorial to convert PDF page to an image.
                try
                {
                    // A) Open the PDF document.
                    using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        doc.InitSecurityHandler();

                        // B) The output resolution is set to 92 DPI.
                        draw.SetDPI(92);

                        // C) Rasterize the first page in the document and save the result as PNG.
                        Page pg = doc.GetPage(1);
                        draw.Export(pg, output_path + "tiger_92dpi.png");

                        Console.WriteLine("Example 1: tiger_92dpi.png");

                        // Export the same page as TIFF
                        draw.Export(pg, output_path + "tiger_92dpi.tif", "TIFF");
                    }
                }
                catch (PDFNetException e) {
                    Console.WriteLine(e.Message);
                }

                //--------------------------------------------------------------------------------
                // Example 2) Convert the all pages in a given document to JPEG at 72 DPI.
                ObjSet hint_set = new ObjSet();               // A collection of rendering 'hits'.
                Console.WriteLine("Example 2:");
                try
                {
                    using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        doc.InitSecurityHandler();

                        draw.SetDPI(72);                         // Set the output resolution is to 72 DPI.

                        // Use optional encoder parameter to specify JPEG quality.
                        Obj encoder_param = hint_set.CreateDict();
                        encoder_param.PutNumber("Quality", 80);

                        // Traverse all pages in the document.
                        for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
                        {
                            string output_filename = string.Format("newsletter{0:d}.jpg", itr.GetPageNumber());
                            Console.WriteLine("newsletter{0:d}.jpg", itr.GetPageNumber());
                            draw.Export(itr.Current(), output_path + output_filename, "JPEG", encoder_param);
                        }
                    }

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

                try                  // Examples 3-6
                {
                    // Common code for remaining samples.
                    using (PDFDoc tiger_doc = new PDFDoc(input_path + "tiger.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        tiger_doc.InitSecurityHandler();
                        Page page = tiger_doc.GetPage(1);

                        //--------------------------------------------------------------------------------
                        // Example 3) Convert the first page to GDI+ Bitmap. Also, rotate the
                        // page 90 degrees and save the result as RAW.
                        draw.SetDPI(100);                         // Set the output resolution is to 100 DPI.
                        draw.SetRotate(Page.Rotate.e_90);         // Rotate all pages 90 degrees clockwise.

                        BitmapInfo buf = draw.GetBitmap(page, PDFDraw.PixelFormat.e_rgb, false);

                        // Save the raw RGB data to disk.
                        string filename = "tiger_100dpi_rot90.raw";

                        System.IO.File.WriteAllBytes(output_path + filename, buf.Buffer);

                        Console.WriteLine("Example 3: tiger_100dpi_rot90.raw");
                        draw.SetRotate(Page.Rotate.e_0);                          // Disable image rotation for remaining samples.

                        //--------------------------------------------------------------------------------
                        // Example 4) Convert PDF page to a fixed image size. Also illustrates some
                        // other features in PDFDraw class such as rotation, image stretching, exporting
                        // to grayscale, or monochrome.

                        // Initialize render 'gray_hint' parameter, that is used to control the
                        // rendering process. In this case we tell the rasterizer to export the image as
                        // 1 Bit Per Component (BPC) image.
                        Obj mono_hint = hint_set.CreateDict();
                        mono_hint.PutNumber("BPC", 1);

                        // SetImageSize can be used instead of SetDPI() to adjust page  scaling
                        // dynamically so that given image fits into a buffer of given dimensions.
                        draw.SetImageSize(1000, 1000);                                  // Set the output image to be 1000 wide and 1000 pixels tall
                        draw.Export(page, output_path + "tiger_1000x1000.png", "PNG", mono_hint);
                        Console.WriteLine("Example 4: tiger_1000x1000.png");

                        draw.SetImageSize(200, 400);                                // Set the output image to be 200 wide and 300 pixels tall
                        draw.SetRotate(Page.Rotate.e_180);                          // Rotate all pages 90 degrees clockwise.

                        // 'gray_hint' tells the rasterizer to export the image as grayscale.
                        Obj gray_hint = hint_set.CreateDict();
                        gray_hint.PutName("ColorSpace", "Gray");

                        draw.Export(page, output_path + "tiger_200x400_rot180.png", "PNG", gray_hint);
                        Console.WriteLine("Example 4: tiger_200x400_rot180.png");

                        draw.SetImageSize(400, 200, false);                         // The third parameter sets 'preserve-aspect-ratio' to false.
                        draw.SetRotate(Page.Rotate.e_0);                            // Disable image rotation.
                        draw.Export(page, output_path + "tiger_400x200_stretch.jpg", "JPEG");
                        Console.WriteLine("Example 4: tiger_400x200_stretch.jpg");

                        //--------------------------------------------------------------------------------
                        // Example 5) Zoom into a specific region of the page and rasterize the
                        // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image).
                        page.SetCropBox(new Rect(216, 522, 330, 600));                          // Set the page crop box.

                        // Select the crop region to be used for drawing.
                        draw.SetPageBox(Page.Box.e_crop);
                        draw.SetDPI(900);                          // Set the output image resolution to 900 DPI.
                        draw.Export(page, output_path + "tiger_zoom_900dpi.png", "PNG");
                        Console.WriteLine("Example 5: tiger_zoom_900dpi.png");

                        // -------------------------------------------------------------------------------
                        // Example 6)
                        draw.SetImageSize(50, 50);         // Set the thumbnail to be 50x50 pixel image.
                        draw.Export(page, output_path + "tiger_zoom_50x50.png", "PNG");
                        Console.WriteLine("Example 6: tiger_zoom_50x50.png");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }

                Obj cmyk_hint = hint_set.CreateDict();
                cmyk_hint.PutName("ColorSpace", "CMYK");

                //--------------------------------------------------------------------------------
                // Example 7) Convert the first PDF page to CMYK TIFF at 92 DPI.
                // A three step tutorial to convert PDF page to an image.
                try
                {
                    // A) Open the PDF document.
                    using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        doc.InitSecurityHandler();

                        // B) The output resolution is set to 92 DPI.
                        draw.SetDPI(92);

                        // C) Rasterize the first page in the document and save the result as TIFF.
                        Page pg = doc.GetPage(1);
                        draw.Export(pg, output_path + "out1.tif", "TIFF", cmyk_hint);
                        Console.WriteLine("Example 7: out1.tif");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }

                //--------------------------------------------------------------------------------
                // Example 8) Export raster content to PNG using different image smoothing settings.
                try
                {
                    // A) Open the PDF document.
                    using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        doc.InitSecurityHandler();

                        // B) Get the page matrix
                        Page     pg  = doc.GetPage(1);
                        Page.Box box = Page.Box.e_crop;
                        Matrix2D mtx = pg.GetDefaultMatrix(true, box);
                        // We want to render a quadrant, so use half of width and height
                        double pg_w = pg.GetPageWidth(box) / 2;
                        double pg_h = pg.GetPageHeight(box) / 2;

                        // C) Scale matrix from PDF space to buffer space
                        double dpi             = 96.0;
                        double scale           = dpi / 72.0; // PDF space is 72 dpi
                        int    buf_w           = (int)(Math.Floor(scale * pg_w));
                        int    buf_h           = (int)(Math.Floor(scale * pg_h));
                        int    bytes_per_pixel = 4; // BGRA buffer
                        int    buf_size        = buf_w * buf_h * bytes_per_pixel;
                        mtx.Translate(0, -pg_h);    // translate by '-pg_h' since we want south-west quadrant
                        mtx = new Matrix2D(scale, 0, 0, scale, 0, 0) * mtx;

                        // D) Rasterize page into memory buffer, according to our parameters
                        byte[]        buf;
                        PDFRasterizer rast = new PDFRasterizer();
                        buf = rast.Rasterize(pg, buf_w, buf_h, buf_w * bytes_per_pixel, bytes_per_pixel, true, mtx);

                        // buf now contains raw BGRA bitmap.
                        Console.WriteLine("Example 8: Successfully rasterized into memory buffer.");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }
                //--------------------------------------------------------------------------------
                // Example 9) Export raster content to PNG using different image smoothing settings.
                try
                {
                    using (PDFDoc text_doc = new PDFDoc(input_path + "lorem_ipsum.pdf"))
                    {
                        text_doc.InitSecurityHandler();

                        draw.SetImageSmoothing(false, false);
                        string filename = "raster_text_no_smoothing.png";
                        draw.Export(text_doc.GetPageIterator().Current(), output_path + filename);
                        Console.WriteLine("Example 9 a): " + filename + ". Done.");

                        filename = "raster_text_smoothed.png";
                        draw.SetImageSmoothing(true, false /*default quality bilinear resampling*/);
                        draw.Export(text_doc.GetPageIterator().Current(), output_path + filename);
                        Console.WriteLine("Example 9 b): " + filename + ". Done.");

                        filename = "raster_text_high_quality.png";
                        draw.SetImageSmoothing(true, true /*high quality area resampling*/);
                        draw.Export(text_doc.GetPageIterator().Current(), output_path + filename);
                        Console.WriteLine("Example 9 c): " + filename + ". Done.");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                //--------------------------------------------------------------------------------
                // Example 10) Export separations directly, without conversion to an output colorspace
                try
                {
                    using (PDFDoc separation_doc = new PDFDoc(input_path + "op_blend_test.pdf"))
                    {
                        separation_doc.InitSecurityHandler();
                        Obj separation_hint = hint_set.CreateDict();
                        separation_hint.PutName("ColorSpace", "Separation");
                        draw.SetDPI(96);
                        draw.SetImageSmoothing(true, true);
                        draw.SetOverprint(PDFRasterizer.OverprintPreviewMode.e_op_on);

                        string filename = "merged_separations.png";
                        draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "PNG");
                        Console.WriteLine("Example 10 a): " + filename + ". Done.");

                        filename = "separation";
                        draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "PNG", separation_hint);
                        Console.WriteLine("Example 10 b): " + filename + "_[ink].png. Done.");

                        filename = "separation_NChannel.tif";
                        draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "TIFF", separation_hint);
                        Console.WriteLine("Example 10 c): " + filename + ". Done.");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }
            }              // using PDFDraw
        }
Ejemplo n.º 4
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();
        }