PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jul 01, 2024 8:17 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue Mar 20, 2012 4:05 pm 
Offline

Joined: Tue Mar 20, 2012 3:33 pm
Posts: 5
Hi all,

i am using PDFSharp (Version: 1.31.1789.0) and i got an exception, because a pdf file, which i use, contains one image and that seems to use 2 different encodings. I tried to use the first encoding of the Array, but if i use that encoding, which is "/FlateDecode" i got an OutOfMemoryError, when i first save the image and then try to read it programmatically.

Code:
static void ExportImage(PdfDictionary image, ImageFileInfo ifi)
{
            string filter="";
            try
            {
                    //the following line runs in an exception, because image.Elements.GetName("/Filter") contains an object array with two values ("/FlateDecode" and "/DCTDecode" and not one single value
                    filter = image.Elements.GetName("/Filter");
            }catch (Exception ex)
            {
                     PdfSharp.Pdf.PdfItem objFilter = (PdfSharp.Pdf.PdfItem)image.Elements["/Filter"];             
                     filter = objFilter.ToString();
            }
            if (filter.Contains("/DCTDecode"))
            {
                ifi.ImageFileName += ".jpg";
                ExportJpegImage(image, ifi.ImageFileName);
            }
             else if (filter.Contains("/FlateDecode"))
            {
                ifi.ImageFileName += ".png";
                ExportAsPngImage(image, ifi.ImageFileName);
            }
}


i don't know, if my approach is the correct one.
if pdfdictionary.elemens["/Filter"] contains 2 values, which should i use or what should i change, to make the code working with that document?

Thanks in advance!
tetronik


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 20, 2012 4:40 pm 
Offline

Joined: Tue Feb 23, 2010 12:13 pm
Posts: 7
I have encountered this odd behaviour sometimes, too.
Basically this means that it is a jpg image (/dctdecode) that is afterwards encoded with (/flatedecode).

Not that this makes much sense because the /dctdecode already compresses the data pretty well, but some pdf tools just seem to do that anyway.

You just have to Flatedecode the data and then send the decoded data to the jpg decoder.


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 21, 2012 10:38 am 
Offline

Joined: Tue Mar 20, 2012 3:33 pm
Posts: 5
Thats a good hint, Thanks!
But if i try to Flatedecode the image first, then i got an exception at my decode-method. I now found out, that this is the reason, why i get later an "out of memory-error", because the file can't be saved correctly i think.

Code:
static void ExportAsPngImage(PdfDictionary image, string outputFileName)
{
            MemoryStream mms = new MemoryStream(image.Stream.Value);
            try
            {
                //The following line runs into an Exception: "Parameter is not valid"
                Bitmap bmp = new Bitmap(mms);
                bmp.Save(outputFileName, ImageFormat.Png);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
}


Do You have an idea, what in the image could make this problem?


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 21, 2012 1:26 pm 
Offline

Joined: Tue Mar 20, 2012 3:33 pm
Posts: 5
UPDATE 1:
if i try use at first the decoder for jpg the image is also not readable, no matter if i use the decoder for png afterwards.

UPDATE 2:
i tried some other PDF-Files and i found out, that they are all decoded with "CCITTFaxDecode" and there is no problem to extract the images. But it seems to be a generally problem to decode images with FlateDecode. Maybe my approach for extracting such images is wrong (you can see my code above).


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 21, 2012 1:28 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
Tetronik wrote:
you can see my code above
Not much code showing here.

FlateDecode is very similar to ZIP. A FlateEncoded JPG is still a JPG and not a PNG.
DCTDecode means there is a JPG encoded image in the PDF.
FlateDecode & DCTDecode means there is a "zipped" JPG encoded image in the PDF - you must "unzip" it to get the JPG.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 21, 2012 1:44 pm 
Offline

Joined: Tue Mar 20, 2012 3:33 pm
Posts: 5
Sorry, "above" means in the previous post from Wed Mar 21, 2012 11:38 am. :?
Could you have a look at it, please? I just used the GDI+ way.

But your infos are helpfully. I look now, if there is another way to "unzip" the image.


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 22, 2012 8:32 am 
Offline

Joined: Tue Feb 23, 2010 12:13 pm
Posts: 7
You cant just send a "zipped" jpg image to GDI directly , because GDI wont be able to handle that.

Have a look at the following Class in PdfSharp:

namespace PdfSharp.Pdf.Filters.FlateDecode

public override byte[] Decode(byte[] data, FilterParms parms)

Just call the decode funktion (to "unzip" the data) and then use the decoded Data for further GDI operations.


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 22, 2012 8:53 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
Tetronik wrote:
Sorry, "above" means in the previous post from Wed Mar 21, 2012 11:38 am. :?
I see two code fragments - one ExportImage and ExportAsPngImage. I call that "fragments" and "not much".
And you call ExportAsPngImage for "zipped" JPG images. Makes no sense.

You'll have to call ExportJpegImage after calling Decode. This will get you closer to the goal - and if it does not work, there'll be more code for us to review.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 29, 2012 3:00 pm 
Offline

Joined: Tue Mar 20, 2012 3:33 pm
Posts: 5
i found a solution for my problem.
First i uncompress the file with this method i found in the internet:
Code:
static byte[] FlateDecode(byte[] inp, bool strict)
{
            MemoryStream stream = new MemoryStream(inp);
            InflaterInputStream zip = new InflaterInputStream(stream);
            MemoryStream outp = new MemoryStream();
            byte[] b = new byte[strict ? 4092 : 1];
            try
            {
                int n;
                while ((n = zip.Read(b, 0, b.Length)) > 0)
                {
                    outp.Write(b, 0, n);
                }
                zip.Close();
                outp.Close();
                return outp.ToArray();
            }
            catch (Exception ex)
            {
                if (strict)
                    return null;
                return outp.ToArray();
            }
}


after that i take the result of this method and call my normal method for decode jpg-image:

Code:
static void ExportJpegImage(PdfDictionary image, string outputFileName)
{
          // Fortunately JPEG has native support in PDF and exporting an image is just writing the stream to a file.
          byte[] stream = image.Stream.Value;
          FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.Write);
          BinaryWriter bw = new BinaryWriter(fs);
          bw.Write(stream);
          bw.Close();
}


works perfect.
Thanks again. Without your help i wouldn't be able to solve this issue. Maybe in the future i will try to test your other hints, too.


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 30 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