iPhone - Showing PDF in UIWebView - iphone

I want to show the PDF file in UIWebView an i am doing that with the code below
NSString *path = [[NSBundle mainBundle] pathForResource:#"About Downloads" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webViewContractViewer loadRequest:request];
All i want is that my UIWebView Farm height and width should adjust dynamically with the height and width of PDF file so that there should be no vertical or Horizontal scrolling in my UIWebView.
I am getting my PDF file Width and height with code
NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:#"About Downloads.pdf" withExtension:nil];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfURL);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
CGPDFPageRetain(page);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
Can any one help. Is there is any other good approach to do this.

webViewContractViewer.scalesPageToFit = YES;

Related

how to load pdf file in UIWebview center

I am new in ios developement.I am trying to display a pdf in a UIWebview center.i have a pdf in mainbundle.Here is what my PDF looks like now when loaded in a UIWebView: (I put in a brown gradient as the UIWebView's background so you can see what I mean better).
- (void)viewDidLoad
{
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:[self.view bounds]];
[theWebView setContentMode:UIViewContentModeScaleAspectFit];
[theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[theWebView setScalesPageToFit:YES];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
[theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]];
[self.view addSubview:theWebView];
[super viewDidLoad];
}
I would like to centre that so that the bottom margin is the same as the top margin. How would I do this with a PDF (local)?
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"pdf" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
Not sure whether this is the ideal solution for your question.
First Get the width and height of PDF==>
float width = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.width;
float height = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.height;
where Pageref is the PDF page reference.
CGSize pdfSize = CGSizeMake(width, height);
//Create one superView to webview
UIView *supView = [[UIView alloc] initWithFrame:[self.view bounds]];
supView.backgroundColor=[UIColor colorWithRed:0.663 green:0.855 blue:0.341 alpha:1]
//Then webview
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, pdfSize.width, pdfSize.height)]];
[theWebView setContentMode:UIViewContentModeScaleAspectFit];
[theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[theWebView setScalesPageToFit:YES];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
[theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]];
[supView addSubview: theWebView];
[self.view addSubview: supView];
EDIT:
Get Pageref(part),
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
CFURLRef pdfURL = (__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:filePath];
//file ref
CGPDFDocumentRef pdfDocRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
CGPDFPageRef PageRef = CGPDFDocumentGetPage(pdfDocRef, 1);
float width = CGPDFPageGetBoxRect(page1, kCGPDFMediaBox).size.width;
float height = CGPDFPageGetBoxRect(page1, kCGPDFMediaBox).size.height;
NSLog(#"%f %f",height,width);

Image does not load in UIWebView in iphone

I have UIWebView i want to load image in that but it does not show image in it i am using following code
pdfView.hidden=NO;
webView.hidden=NO;
pdfView.backgroundColor=[UIColor clearColor];
doneButton = [[UIBarButtonItem alloc]initWithTitle:#" Done " style:UIBarButtonItemStyleBordered target:self action:#selector(doneButtonClick)];
self.navigationItem.leftBarButtonItem =doneButton;
webView.backgroundColor=[UIColor clearColor];
NSLog(#"File Name is %#",fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"png"];
NSURL *url = [NSURL fileURLWithPath:fileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
is there any way to load this image which is in fileName which comes from sever in this webView thanks
The other way i can go for is to html-page dynamically and load it into UIWebView:
NSString *imageUrlString = #"your url ";
NSString *yourText = #"Some text here";
NSString *htmlString = [NSString stringWithFormat:#"<img src='%#'/><br><b>%#</b>", imageUrlString, yourText];
[myWebView loadHTMLString:htmlString baseURL:nil];
I think you can tighten this up a bit. Instead of getting a string path and creating a file URL from it, just ask the bundle for a URL.
My best guess is your filename already has the file extension in it and you're not actually getting back a valid file path/url. Step through in the debugger and see.
// get the file resource URL
NSURL *url = [[NSBundle mainBundle] URLForResource: filename withExtension: #"png"];
// sanity check - did we get a valid URL?
NSAssert( url != nil && [url checkResourceIsReachableAndReturnError: NULL], #"oops - no resource!");
// load it
[webView loadRequest: [NSURLRequest requestWithURL:url]];
Why can't you just use a UIImageView? This is done by hand but something like;
NSURL *url = [NSURL fileURLWithPath:fileName];
UIImage *tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
imageView.image = tempImage;

Can’t Display A Microsoft Word.doc document on UIView Controller

I’m using the code below to display pdfs or filename.doc Microsoft Word documents. It works fine when I use a ofType:#:”pdf”, but my code crashes when I use ofType:#:”doc” or ofType:#:”docx”. The Word document is mostly text and a few url links. Any help appreciated.
NSString *path = [[NSBundle mainBundle] pathForResource:#"C9" ofType:#"doc"];
pdfUrl = [NSURL fileURLWithPath:path];}
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
As per your code, it looks like you're already using a UIWebView.
Here is some code I've used in the past.
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView {
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
In viewDidLoad (change webView frame to whatever you need)..
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:webView];
[self loadDocument:#"file.doc" inView:webView];
Take a look at using QLPreviewController to display these document types.
For the crash, are you sure the file exists at the specified location? Check it's actually being copied to the bundle and that there is a file at that path / URL before you use it.

Display local pdf file in UIWebView in iphone

I am display local pdf file in UIWebView but it shows exception in path here is my code
NSString*fileName=content_Source;
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request]
You must be using the file name extension while setting the file name. So make sure the extension will not be used in file name if it's setting along with Type (like ofType:#"pdf").
Here's my code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= #"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];
There's a better way to show PDFs:
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
Make sure use the delegate along with that code to work it properly.
(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
first validate that string and the assign to URL like bellow..
NSString *strURLPath = [self trimString: path];
NSURL *url = [NSURL fileURLWithPath:strURLPath];
use my bellow method...
-(NSString*) trimString:(NSString *)theString {
if (theString != [NSNull null])
theString = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return theString;
}
It is working fine with the given code problem was that in my file Name i was giving also the type like ali.pdf they type pdf so it get exception it worked fine now when i use filename like following
NSString*fileName=#"ali";
Instead of
NSString*fileName=#"ali.pdf";
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"pdf" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];

How to show a PDF page by page using horizontal swipe in iphone?

I would like to add to my app a pdf viewer which can scroll horizontally. Anyone know how to do it? I'm making several attempts but without success. Thanks. Matteo
Use UIWebViewController in landscape orientation and this piece of code can give you an idea about how to display pdfs:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake( 0, 0, 320, 480 )];
NSString *path = [[NSBundle mainBundle] pathForResource:#"about" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
[webView release];