Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-11-2011, 05:33 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 70
andrewtheeditor is on a distinguished road
Default Viewing a PDF without UIWebView

I am working on an app that will mark up a PDF and export it. I need the user to be able to draw on top of a PDF, and then I will analyze their actions and apply some type of annotation. Ultimately, it will be lines, circles, and text added to the file (so annotations should be sufficient). I'm not certain how I will tackle all that just yet; I am just trying to get the PDF to show up.

I'd love to work off of this tutorial: http://www.iphonedevsdk.com/forum/ip...-tutorial.html

I am worried about using UIWebView though. I figured the drawing + applying will all be in Quartz, so the PDF viewing should be too. I'd love to get a professional opinion from anyone out there. Will it be okay to view the PDF in UIWebView, and then load it with Quartz to apply annotations? I remember reading another way somewhere, but I cannot seem to figure out how its done.
andrewtheeditor is offline   Reply With Quote
Old 03-12-2011, 05:08 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 70
andrewtheeditor is on a distinguished road
Default

There is another way to do it. I wrote up a quick tutorial based on another tutorial I found. If anyone else is trying to do this, I recommend checking this out:

Loading a PDF into an iOS app n00b Dev
andrewtheeditor is offline   Reply With Quote
Old 03-21-2011, 05:58 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 70
andrewtheeditor is on a distinguished road
Default

I'm having an issue with it only displaying one defined page. UIWebView works great for displaying pages, but I need more functionality than UIWebView offers.

I'm going to update that tutorial when I figure this out, but I could really use some help. Here is what I have:

Code:
//  PDFView.h
@interface PDFView : UIScrollView {
    CGPDFDocumentRef pdf;
}

-(void)drawInContext:(CGContextRef)context;

@end
Code:
//  PDFView.m
#import "PDFView.h"
@implementation PDFView

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = [UIColor clearColor];
        CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("MyPDF.pdf"), NULL, NULL);
        pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
        CFRelease(pdfURL);
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [self drawInContext:UIGraphicsGetCurrentContext()];
}

-(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)dealloc {
    CGPDFDocumentRelease(pdf);
    [super dealloc];
}

@end
Code:
//  myViewController.m
#import "myViewController.h"
@implementation myViewController

- (void)viewDidLoad { 
    CGRect frame = CGRectMake(70, 204, 628, 850);
    PDFView *pdfView = [[PDFView alloc] initWithFrame:frame];
    [self.view addSubview:pdfView];
    [super viewDidLoad];
}
This line seems to be creating the issue, as it only asks for the first page:
Code:
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
How would I go about having it load in all the pages?

Last edited by andrewtheeditor; 03-21-2011 at 06:00 PM.
andrewtheeditor is offline   Reply With Quote
Old 03-21-2011, 07:16 PM   #4 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

I'm pretty sure you have to load each page one at a time. CGPDFDocumentGetNumberOfPages will give you the number of pages, and CGPDFPageGetBoxRect will get you the drawing space for each page. But I think it's going to take a lot of experimentation to get it to look right, where you will have to calculate which page should be on the screen at any time and only load that one.
__________________
My development blog: http://jrinn.com

Last edited by JasonR; 03-21-2011 at 07:21 PM. Reason: Re-read the question.
JasonR is offline   Reply With Quote
Old 03-21-2011, 08:10 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 70
andrewtheeditor is on a distinguished road
Default

Thanks Jason! I just started playing around with that idea. Glad to know I'm on the right track. I'll post back when I figure it out.
andrewtheeditor is offline   Reply With Quote
Old 03-21-2011, 11:57 PM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 70
andrewtheeditor is on a distinguished road
Default

I'm changing up how I am trying to do this. I just realized that I will not want the PDF to be in a scrolling view as people will be drawing on top of it (and therefore, it should not be moving around). I will now display the current page of the PDF with an up and down button that can swap the page with a new one. I suppose it is the same concept though.

I want to load a PDF page into a UIView. The code I posted above works for that. I cannot seem to find a way to define the page number outside of UIView though. I have seriously spent hours trying to pass an integer from the view controller to the view. I suppose I could add the up and down buttons to the view and create an action within the view that changes the page number. That does not sound like good practice to me though.

Is there anyway to pass int currentPage into this from the view controller?

Code:
-(void)drawInContext:(CGContextRef)context {
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);
    
    CGContextSaveGState(context);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
    CGContextConcatCTM(context, pdfTransform);
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);
}
I feel really bad about asking such specific questions, sorry guys. I am just really stumped.

Last edited by andrewtheeditor; 03-21-2011 at 11:59 PM.
andrewtheeditor is offline   Reply With Quote
Old 03-22-2011, 01:08 AM   #7 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

Add the int to your PDFView.h, and use it for the page number in your drawing code. When the user pushes the up or down arrow, change the int and call
Code:
[pdfview setNeedsDisplay];
to make it draw the new page. I think you'll have to add pdfview to your myViewController.h as well to make it work.
__________________
My development blog: http://jrinn.com
JasonR is offline   Reply With Quote
Old 03-22-2011, 04:07 AM   #8 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 70
andrewtheeditor is on a distinguished road
Default

Wow, that would have taken me so long to figure out. Updating the previous tutorial now. Thanks Jason. To think, I spent the whole day on such a silly thing.
andrewtheeditor is offline   Reply With Quote
Reply

Bookmarks

Tags
pdf, pdfview, quartz, quartzcore, uiwebview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 365
8 members and 357 guests
Absentia, akphyo, apatsufas, BinHex, fredidf, Kirkout, MarkC, mottdog
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:24 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0