PDFsharp & MigraDoc Foundation

PDFsharp Sample: Export Images - Additional Code
Page 1 of 1

Author:  VacentViscera [ Tue Jun 19, 2018 8:32 pm ]
Post subject:  PDFsharp Sample: Export Images - Additional Code

I've been working with the PDFSharp Libraries for a bit and really love the stuff you guys are doing. I wanted to do my part after figuring out a way to do something you mentioned on the following site:

The following stub is mentioned on the above site:

Other image formats are not yet implemented, here is the stub:

static void ExportAsPngImage(PdfDictionary image, ref int count)
int width = image.Elements.GetInteger(PdfImage.Keys.Width);
int height = image.Elements.GetInteger(PdfImage.Keys.Height);
int bitsPerComponent = image.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);

// TODO: You can put the code here that converts vom PDF internal image format to a Windows bitmap
// and use GDI+ to save it in PNG format.
// It is the work of a day or two for the most important formats. Take a look at the file
// PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.
// We don't need that feature at the moment and therefore will not implement it.
// If you write the code for exporting images I would be pleased to publish it in a future release
// of PDFsharp.

The below code uses the method stub supplied and uses bitmap conversion of the image from the PdfDictionary input image object and the System.Runtime.InteropServices.Marshal class to copy the bitmap data to a memory pointer and output it as an Image for conversion from BMP to other formats, though this step is not required.

Note: This is pretty much entirely credited to the user der_chirurg on StackOverflow: https://stackoverflow.com/users/1314677/der-chirurg

Original Article on StackOverflow: https://stackoverflow.com/questions/100 ... h-pdfsharp

C# Code:

static void ExportAsPngImage(PdfDictionary inputObject, ref int count)
            Image outputImage;

            int width = inputObject.Elements.GetInteger(PdfImage.Keys.Width);
            int height = inputObject.Elements.GetInteger(PdfImage.Keys.Height);
            int bitsPerComponent = inputObject.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);

            PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
            byte[] imageData = flate.Decode(inputObject.Stream.Value);           

            System.Drawing.Imaging.PixelFormat pixelFormat;

            switch (bitsPerComponent)
                case 1:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
                case 8:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
                case 24:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
                    throw new Exception("Unknown pixel format: " + bitsPerComponent);

            Bitmap bmp = new Bitmap(width, height, pixelFormat);
            var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, pixelFormat);
            int length = (int)Math.Ceiling(width * bitsPerComponent / 8.0);

            for (int i = 0; i < height; i++)
                int offset = i * length;
                int scanOffset = i * bmpData.Stride;
                Marshal.Copy(imageData, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length);


            outputImage = (Image)bmp;

            //return outputImage; //Uncomment and use this if you want to return this to another method, as I actually ended up doing in my code.

            //Comment out everything below here if you will use this method to return to another method instead of writing directly to disk.

            outputImage.Save("extractedImages\\testExtractedImage" + count + ".png", System.Drawing.Imaging.ImageFormat.Png);
            //Above saves to "extractedImages" directory in which exe is running as "testExtractedImage1.png" etc. for file name.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group