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 931547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:31:15+00:00 2026-05-15T20:31:15+00:00

I am stuck with parsing a PDF file. Please guide me how to do

  • 0

I am stuck with parsing a PDF file. Please guide me how to do this.

Header file.

//PDFViewer.h
@interface PDFViewer : UIView 
{
 CGPDFDocumentRef pdf;
}

-(void)drawInContext:(CGContextRef)context;

@end

Implementation file

//PDFViewer.m
@implementation PDFViewer


- (id)initWithFrame:(CGRect)frame 
{

 if ((self = [super initWithFrame:frame])) 
 {
        // Initialization code
  if(self != nil)
  {
   CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("WR1MayJun1S08.pdf"), NULL, NULL);
   pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
   CFRelease(pdfURL);
  }
    }
    return self;
}


-(void)drawInContext:(CGContextRef)context
{
 // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
 // before we start drawing.
 CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
 CGContextScaleCTM(context, 1.0, -1.0);

 // Grab the first PDF page
 CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
 // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
 CGContextSaveGState(context);
 // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
 // base rotations necessary to display the PDF page correctly. 
 CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
 // And apply the transform.
 CGContextConcatCTM(context, pdfTransform);
 // Finally, we draw the page and restore the graphics state for further manipulations!
 CGContextDrawPDFPage(context, page);
 CGContextRestoreGState(context);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc 
{
    CGPDFDocumentRelease(pdf);
 [super dealloc];
}


@end

Now I am adding this class (PDFViewer.h) to my MainViewController.

//MainViewController.m

CGRect frame = CGRectMake(0, 200, 300, 500);

PDFViewer *pdfViewer = [[PDFViewer alloc] initWithFrame:frame];
CGContextRef context = UIGraphicsGetCurrentContext();
[pdfViewer drawInContext:context];
[self.view addSubview:pdfViewer];

It shows nothing. I get the following errors/warnings:

local MultiView[2850] <Error>: CGContextTranslateCTM: invalid context
local MultiView[2850] <Error>: CGContextScaleCTM: invalid context
local MultiView[2850] <Error>: CGContextSaveGState: invalid context
local MultiView[2850] <Error>: CGContextConcatCTM: invalid context
local MultiView[2850] <Error>: CGContextRestoreGState: invalid context

What am I missing?

Regards.

  • 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-05-15T20:31:16+00:00Added an answer on May 15, 2026 at 8:31 pm

    UIGraphicsGetCurrentContext does not return a context if there isn’t one, obviously.

    You try to get the context at view initialization, at that time there is no context available. A valid context gets pushed onto the stack just before -[UIView drawRect:] is being called. This should work:

    //PDFViewer.m
    @implementation PDFViewer
    
    - (void)drawRect:(CGRect)rect {
        [self drawInContext:UIGraphicsGetCurrentContext()];
    }
    
    @end
    

    EDIT
    Eventhough I don’t like to give anyone copy-and-paste-ready-code, I don’t think there is another option left if you didn’t understand my latest comment. I don’t know what you’ve tried, but if you try to understand what I’m really saying, this is the only thing you can come up with:

    //PDFViewer.m
    @implementation PDFViewer
    
    - (id)initWithFrame:(CGRect)frame 
    {
    
        if (self = [super initWithFrame:frame]) 
        {
            CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("WR1MayJun1S08.pdf"), NULL, NULL);
            pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
            CFRelease(pdfURL);
        }
        return self;
    }
    
    -(void)drawInContext:(CGContextRef)context
    {
        // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
        // before we start drawing.
        CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
    
        // Grab the first PDF page
        CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
        // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
        CGContextSaveGState(context);
        // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
        // base rotations necessary to display the PDF page correctly. 
        CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
        // And apply the transform.
        CGContextConcatCTM(context, pdfTransform);
        // Finally, we draw the page and restore the graphics state for further manipulations!
        CGContextDrawPDFPage(context, page);
        CGContextRestoreGState(context);
    }
    
    - (void)drawRect:(CGRect)rect {
        [self drawInContext:UIGraphicsGetCurrentContext()];
    }
    
    - (void)dealloc 
    {
        CGPDFDocumentRelease(pdf);
        [super dealloc];
    }
    
    @end
    

    —

    //MainViewController.m
    
    CGRect frame = CGRectMake(0, 200, 300, 500);
    
    PDFViewer *pdfViewer = [[PDFViewer alloc] initWithFrame:frame];
    [self.view addSubview:pdfViewer];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Really stuck on trying to write code to unzip a file or directory on
I'm a bit stuck on this. Basically I want to do something like the
I'm kinda stuck with this one so I hoped someone could help me. I
Hello i have trouble with parsing a pdf when the iterator reaches page 11
I'm using simplehtmldom to parse html and I'm stuck in parsing plaintext located outside
I am parsing a XML file in Java using the W3C DOM. I am
I'm stuck on this and have been all day.. I'm still pretty new to
I'm already parsing xml successfully but i'm stuck at getting an attribute of childrens.
I'm parsing a XML file with Commons Digester and I don't understand what's wrong
I am stuck using an AJAX library from about 5 years ago in this

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.