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);
Related
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];
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;
I am already using the MediaPlayer framework but the media player can only be initialized through a URL. I have my video saved in my project, so how can I play it?
URLs can be files paths too (file://):
NSURL *movieURL = [[NSBundle mainBundle] URLForResource:#"movie" withExtension:#"mp4"];
You can also play your video in UIWebview.
CGRect webFrame = CGRectMake(0.0, 0.0, 1, 1);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
webView.scalesPageToFit =YES;
webView.autoresizingMask =YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test123" ofType:#"mp4"]isDirectory:NO]]];
[self.view insertSubview:webView atIndex:0];
I am trying to play video inside a UIView, so my first step was to add a class for that view and start playing a movie in it using this code:
- (IBAction)movie:(id)sender{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Movie" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
}
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];
I am calling an infoPage.html as below with an image from Resources and referenced as img src="eye.png" in the html, the text displays correctly but I cannot load the image (only a ? appears), any suggestions...
- (void)viewDidLoad {
[super viewDidLoad];
CGRect webRect = CGRectMake(15,40,300,450);
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:webRect];
myWebView.delegate = self;
myWebView.scalesPageToFit = NO;
NSString *htmlPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"infoPage.html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath];
[myWebView loadHTMLString:htmlContent baseURL:nil];
//stop webview from scrolling
[[[myWebView subviews] lastObject] setScrollingEnabled:NO];
[myWebView loadHTMLString:htmlContent baseURL:nil];
[self.view addSubview:myWebView];
[myWebView release];
}
You need to use [myWebView loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: htmlPath]]], rather than -loadHTML so that it knows where to extract images from. Alternatively, you could pass the path in as the baseURL: argument.
Instead of baseURL:nil, try baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]]