PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

Export Images from PDF created with pdfsharp
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2146
Page 1 of 1

Author:  xeo [ Thu Sep 20, 2012 9:51 am ]
Post subject:  Export Images from PDF created with pdfsharp

Hello,
I am trying to use the export image example to export images from a pdf.
The pdf was created with pdfsharp, basically it contains only jpeg images created with this code:
Code:
PdfPage page = document.AddPage();
page.Size = PageSize.A4;
XGraphics gfx = XGraphics.FromPdfPage(page);
using (XImage image = XImage.FromFile(imageFile))
{
   gfx.DrawImage(image, 0, 0, page.Width, page.Height);
   }


When I run the code sample I get an exception in this method:

Code:
static void ExportImage(PdfDictionary image, ref int count)
        {
            string filter = image.Elements.GetName("/Filter");
            switch (filter)
            {
                case "/DCTDecode":
                    ExportJpegImage(image, ref count);
                    break;
                /*case "/FlateDecode":
                    ExportAsPngImage(image, ref count);
                    break;*/
            }
        }


when it tries to read the filter it says object is not a string.

Any advice?
Thank you

Author:  Thomas Hoevel [ Thu Sep 20, 2012 12:17 pm ]
Post subject:  Re: Export Images from PDF created with pdfsharp

Hi!
Sometimes PDFsharp (and other tools) use both "/FlateDecode" and "/DCTDecode" if this uses less space than only "/DCTDecode". This is a new feature for PDFsharp and the Export sample does not implement this.

You have to call Decode from the FlateDecode class before saving the JPEG file if both filters are specified.
See also:
viewtopic.php?p=5600#p5600

Author:  xeo [ Fri Sep 21, 2012 6:08 am ]
Post subject:  Re: Export Images from PDF created with pdfsharp

thank you it works like a charm!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/