how to load pdf file in UIWebview center

删除回忆录丶 提交于 2020-01-14 15:01:20

问题


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)?


回答1:


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];



回答2:


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);


来源:https://stackoverflow.com/questions/18653907/how-to-load-pdf-file-in-uiwebview-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!