PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Oct 16, 2019 12:33 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Aug 14, 2019 3:44 pm 
Offline

Joined: Wed Aug 14, 2019 3:11 pm
Posts: 5
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 15, 2019 9:01 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2980
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 15, 2019 6:38 pm 
Offline

Joined: Wed Aug 14, 2019 3:11 pm
Posts: 5
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


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 16, 2019 6:59 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 481
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 16, 2019 7:26 pm 
Offline

Joined: Wed Aug 14, 2019 3:11 pm
Posts: 5
Excellent, thankyou for the advice and information.

Simon


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 426 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