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

PDF image using /Filter [/FlateDecode/DCTDecode]
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4003
Page 1 of 1

Author:  bonds007 [ Wed Aug 14, 2019 3:44 pm ]
Post subject:  PDF image using /Filter [/FlateDecode/DCTDecode]

Hi,

I've been using the provided example http://www.pdfsharp.net/wiki/ExportImages-sample.ashx to extract images from each page in a PDF.
The provided example shows how to determine if an image is /DCTDecode or /FlateDecode, I've provided a fragment of the code sample from your Wiki below (for reference):

Code:
string filter = objImage.Elements.GetName("/Filter");

switch (filter) {
  case "/DCTDecode":
    ExportJpegImage(objImage, ref count);
    break;
 
  case "/FlateDecode":
    ExportFlateImage(objImage, ref count);
    break;
}



But some of my PDFs seems to have images with two encodings and this causes the GetName() method to fail with a null exception. I've provided an example PDF fragment below:

Code:
6 0 obj
<<
/Type /XObject
/Subtype /Image
/Name /Img1
/Filter [/FlateDecode/DCTDecode]
/Width 2480
/Height 3508
/BitsPerComponent 8
/ColorSpace /DeviceRGB
/Length 7 0 R
>>
stream



My understanding is that /Filter [/FlateDecode/DCTDecode] means that the Image was encoding using Flate and then DCT and I should decode in reverse order. Is that correct?
But I don't know how to detect this condition in code since GetName("/Filter") returns null in this scenario.

So how do I detect this scenario in code?
And in the above /FlateDecode/DCTDecode example, what should I be doing to decode it and save to disk?

Thanks.

Simon Bond

Author:  Thomas Hoevel [ Thu Aug 15, 2019 9:01 am ]
Post subject:  Re: PDF image using /Filter [/FlateDecode/DCTDecode]

bonds007 wrote:
So how do I detect this scenario in code?

If "/Filter" has an array then access the members of that array.

bonds007 wrote:
And in the above /FlateDecode/DCTDecode example, what should I be doing to decode it and save to disk?
Use the "Decode" method of the "FlateDecode" filter class to obtain the embedded JPEG file.

Author:  bonds007 [ Thu Aug 15, 2019 6:38 pm ]
Post subject:  Re: PDF image using /Filter [/FlateDecode/DCTDecode]

Quote:
If "/Filter" has an array then access the members of that array.


Thankyou for the reply.

Apologies for being dense, but would it be possible to provide a tiny snippet of C# which shows how to detect that "/Filter" is an array?
Maybe I've missed something in the documentation, but I can't see an example which shows how to do this.

I don't see how I can make use of IsArray() in C# when I can't even get a handle to /Filter

If the following code causes an exception then how do I get a handle to /Filter to determine that it is an array:

Code:
objImage.Elements.GetName("/Filter");


Thanks,

Simon

Author:  TH-Soft [ Fri Aug 16, 2019 6:59 am ]
Post subject:  Re: PDF image using /Filter [/FlateDecode/DCTDecode]

bonds007 wrote:
Apologies for being dense, but would it be possible to provide a tiny snippet of C# which shows how to detect that "/Filter" is an array?
You can easily set a breakpoint and inspect the properties.
I first have to find a PDF that uses both filters before I can start trying anything...

Maybe you have to use "GetName(0)" and "GetName(1)" after checking the Count property.

A web search found this topic:
viewtopic.php?p=5587#p5587
Using "Contains" does not ensure the order of filters, but only "/FlateDecode /DCTDecode" makes sense.

Author:  bonds007 [ Fri Aug 16, 2019 7:26 pm ]
Post subject:  Re: PDF image using /Filter [/FlateDecode/DCTDecode]

Excellent, thankyou for the advice and information.

Simon

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