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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 01-26-2009, 02:21 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
Default Zooming a PDF file in webview

Hi guys,
I have a question regarding the zooming/scaling of a pdf file that is being displayed within a webview. Many of the tutorials and forum postings regarding pinch zooming using UIScrollView have dealt with scaling UIImage objects, but I have yet to see anything dealing with how to properly scale something like a pdf file.

Here's what I'm trying to do. My code starts by creating an new NSObject from an archived pdf saved from a previous session. It then creates a ScrollView and add a webView as a subview. The webView is created using the loadData: method to display the unarchived pdf file.

Code:
[webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
This works fine. The scroll view allows for pinch zooms and the pdf file scales accordingly. The problem is that the pdf file doesn't redraw as one would expect when the zooming ends. It remains in it's blurry up-scaled state. I know that I need to implement such a redraw function myself, but I'm using a webView to display the pdf file and not a QuartzView, in which case I could just redraw the pdf data using a new size.

So the question is, does anyone know how to correctly transform an pdf file that is being displayed within a webView? Is this a lost cause and should I just rewrite the code to use QuartzView instead?

Here's the code in question:

Code:
- (void)loadView {
	
	
	// Create a custom scroll view hierarchy.

	myScrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
	self.view = myScrollView;

	myScrollView.maximumZoomScale = 4.0;
	myScrollView.minimumZoomScale = 0.75;
	myScrollView.clipsToBounds = YES;
	myScrollView.delegate = self;
	
	CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 480.0);
	webView = [[UIWebView alloc] initWithFrame:webFrame];
	[webView setBackgroundColor:[UIColor whiteColor]];
	webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
	webView.multipleTouchEnabled=YES;	

		
	// Find the pdf directory
	NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
	NSString * documentsDir = [paths objectAtIndex:0];
	NSString * PDFArXivePath = [documentsDir stringByAppendingPathComponent:Input];	
		
	pdfData = [NSKeyedUnarchiver unarchiveObjectWithFile:PDFArXivePath];

	[webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

	[myScrollView addSubview:webView];
	[myScrollView release];
	
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
	return webView;
}
borg359 is offline   Reply With Quote
Old 01-26-2009, 03:52 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 96
Default

You may be making this to complex. I have an app that displays PDFs in a web view. All I did was create the webview and loaded the PDF with a file URL. No need to create a scroll view, a web view has all the zooming and scrolling built in. It sharpens the PDF just fine.

I am not at my mac right now, but if you want, tonight I can post the code I used to create the web view and to open the PDF.
shan is offline   Reply With Quote
Old 01-26-2009, 04:10 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
Default Zooming within a webView

Hi shan,
My code originally had the pdf file displayed within just a webView and that worked fine for scrolling around the pdf, but I had no zoom capabilities. From what I can tell, webView doesn't inherit any of the UIScrollView methods and webView.multipleTouchEnabled=YES didn't seem to do anything. What did you have to do to enable zoom capabilities within a webView?

In any case, I'd love to see the code that you reference. Thanks again!


-dan
borg359 is offline   Reply With Quote
Old 01-26-2009, 06:23 PM   #4 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 96
Default

To create the web view:
Code:
	UIWebView *tempWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];  
	tempWebView.backgroundColor = [UIColor whiteColor];  
	tempWebView.scalesPageToFit = YES;
	tempWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	tempWebView.delegate = self;
	[self.view addSubview:tempWebView]; 
	[self setWebView:tempWebView];
	[tempWebView release];

To load the PDF:
Code:
		[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:PDFFilePath isDirectory:NO]]];
shan is offline   Reply With Quote
Old 01-26-2009, 07:04 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
Default

Thanks Shan,
Your code did the trick. For those that are curious, the reason the code I posted above didn't work was because I never assigned a webView delegate, so there was nothing handling the screen touches. Simply setting webView.delegate = self; fixes the problem. Thanks again!


-dan
borg359 is offline   Reply With Quote
Old 11-11-2010, 12:51 PM   #6 (permalink)
yoz
Registered Member
 
Join Date: Jul 2010
Posts: 16
Default

I know Apple has come out with sample code for ZoomingPDFViewer and there's an amazing open source project that is working on mastering paging and zooming through PDFs @
http://www.vfr.org

Here's another way to open a pdf inside the bundle.
http://www.iphonedevsdk.com/forum/ip...uiwebview.html

I'm just trying to click on a button and open the pdf inside that's inside the bundle. If anyone could provide code on how to do this, I'm sure a lot of people would appreciate it.

Last edited by yoz; 11-11-2010 at 01:05 PM.
yoz is offline   Reply With Quote
Old 11-11-2010, 01:54 PM   #7 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
Default Opening a PDF using a button

Would you like to open the pdf file in your app or in iBooks? If you'd like to open it inside your app, then create a webview as detailed above. Then push in this new view when the user clicks on the button. Here's the code on how to setup the button and push in the new view.


Code:
OpenPDFButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
					style:UIBarButtonItemStyleDone 
					target:self
					action:@selector(SaveAction)];

self.navigationItem.rightBarButtonItem = OpenPDFButton;
Code:
if(aWebViewController == nil)
	webViewController = [[myWebViewController alloc] initWithNibName:@"myWebViewController" bundle:[NSBundle mainBundle]];
	[self.navigationController pushViewController:aWebViewController animated:YES];
	[aWebViewController release];
If you'd like to open the pdf in iBooks, then check out this link:

http://www.iphonedevsdk.com/forum/ip...on-ibooks.html
borg359 is offline   Reply With Quote
Old 11-11-2010, 02:28 PM   #8 (permalink)
yoz
Registered Member
 
Join Date: Jul 2010
Posts: 16
Default

Thanks!

I'm building a universal app, and on the iPad, I don't have a navigation controller. Eventually, I'm going to use popovers on a series of
-(IBActions) I have as a custom menu at the bottom that will utilize a variation of this code. Awesome.

Now, I used this code in viewDidLoad to open in a web view, and the 4 page pdf pulled up blank pages and a black column on the right.

NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"pdf"];

NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];

Can you add some autosizing to the code to fit and handle rotation?

As for the blank pages, the pdfs are high res, mostly image based, and I'm wondering if the way that Acrobat creates pdfs is the reason the pages are blank.

Any thoughts?

Here's the Apple forum link to the open source project on handling PDFs without webviews. https://devforums.apple.com/thread/6...art=0&tstart=0


Could someone instruct us on how to use this code to in the same way; by opening a pdf with a simple -(IBAction) that uses this code to open the PDF??

Last edited by yoz; 11-11-2010 at 03:04 PM.
yoz is offline   Reply With Quote
Reply

Bookmarks

Tags
pdf, scrollview, transform, webview, zoom

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: 264
14 members and 250 guests
2WeeksToGo, AdamL, ADY, BrianSlick, Dani77, Dattee, headkaze, mer10, prchn4christ, smithdale87, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,879
Threads: 89,228
Posts: 380,747
Top Poster: BrianSlick (7,129)
Welcome to our newest member, mgon987
Powered by vBadvanced CMPS v3.1.0

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