PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Dec 12, 2018 2:45 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Tue Jun 19, 2018 8:32 pm 
Offline

Joined: Tue Jun 19, 2018 8:12 pm
Posts: 1
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:
http://www.pdfsharp.net/wiki/ExportImages-sample.ashx

The following stub is mentioned on the above site:

Quote:
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:

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;
                    break;
                case 8:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
                    break;
                case 24:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
                    break;
                default:
                    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);
            }

            bmp.UnlockBits(bmpData);

            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.
      }


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group