Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:13:20+00:00 2026-06-03T10:13:20+00:00

I’m writing a PDF parser for work, and we’re using Core Graphics to read

  • 0

I’m writing a PDF parser for work, and we’re using Core Graphics to read in all of the data with callbacks and then writing it out with Lib Haru because our client needs to write out “real” annotations and CG can’t do it.

Well, I’ve gotten to the point where I am getting images (and saving them out to a file to make sure I’m doing it right before I start to draw them) and I’ve run into an issue. I’m getting all of the Image XObjects out of the Resource dictionary and then attempting to save them out with this code

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];                

NSData *imageFileData = (NSData *)CGPDFStreamCopyData(objectStream, CGPDFDataFormatRaw);

NSString *fileName = [NSString stringWithFormat:@"%@/%s.png", documentsDir, name];
[imageFileData writeToFile:fileName atomically:YES];

where the objectStream is using the CGPDFDictionaryGetStream to extract the XObject. Well, it works fine when the Filter is “DCTDecode”, but whenever the Filter is “FlateDecode”, the image that is saved is corrupt and won’t open.

I read in this post that CGPDFStreamCopyData can decode text with FlateDecode (all the way to the bottom of the post in the comments), but there are only 3 data formats in the CGPDFDataFormats, and none of them work.

I believe I’m also having issues with text that is encoded with FlatDecode. Does anyone have any suggestions on how to go about decoding this? Surely CGPDF has something that handles this since it appears in almost every pdf that I’ve tried to open (though I haven’t been able to locate it).

Edit: I read in a few places that I could decompress it using zlib, so I tried this code that I was able to find about how to do that:

            NSData* uncompressedImageData;
            if ([imageFileData length] == 0) 
                uncompressedImageData = imageFileData;
            else
            {                
                z_stream strm;
                strm.zalloc = Z_NULL; 
                strm.zfree = Z_NULL; 
                strm.opaque = Z_NULL; 
                strm.total_out = 0; 
                strm.next_in=(Bytef*)[imageFileData bytes]; 
                strm.avail_in = [imageFileData length];

                // Compresssion Levels: // Z_NO_COMPRESSION // Z_BEST_SPEED // Z_BEST_COMPRESSION // Z_DEFAULT_COMPRESSION
                if (deflateInit(&strm, Z_DEFAULT_COMPRESSION) != Z_OK) 
                    uncompressedImageData = nil;

                NSMutableData *compressed = [NSMutableData dataWithLength:16384]; // 16K chuncks for expansion
                do 
                {
                    if (strm.total_out >= [compressed length]) 
                        [compressed increaseLengthBy: 16384];

                    strm.next_out = [compressed mutableBytes] + strm.total_out; strm.avail_out = [compressed length] - strm.total_out;
                    deflate(&strm, Z_FINISH);
                } 
                while (strm.avail_out == 0);

                deflateEnd(&strm);
                [compressed setLength: strm.total_out]; 

                uncompressedImageData = [NSData dataWithData: compressed]; 
            }

            if(uncompressedImageData != nil)
                [uncompressedImageData writeToFile:fileName atomically:YES];

The code didn’t throw any exceptions when I ran it, but the resulting images were still unreadable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:13:22+00:00Added an answer on June 3, 2026 at 10:13 am

    Your use of CGPDFStreamCopyData seems to suggest that you have a misunderstanding there: You don’t set the format that you want, the function sets this to the format it encounters in the stream. A typical use would be:

    CGPDFDataFormat format;
    CGPDFStreamCopyData(objectStream, &format);
    if (format == CGPDFDataFormatRaw) {
        //handle raw data...
    } else if (format == CGPDFDataFormatJPEGEncoded) {
        //handle jpeg data...
    } else if (format == CGPDFDataFormatJPEG2000) {
        //handle jpeg 2000 data
    }
    

    PNG images are not supported at all by the PDF standard, so you’ll never get a valid PNG file out of an image data stream. The options are JPEG, JPEG2K and raw images (see the spec for details on those).

    Quartz handles zlib compression transparently, so you’ll never get zlib-compressed data yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.