how to load pdf file in UIWebview center

2019-07-03 22:18发布

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). enter image description here

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

2条回答
太酷不给撩
2楼-- · 2019-07-03 22:56
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];
查看更多
来,给爷笑一个
3楼-- · 2019-07-03 23:11

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);
查看更多
登录 后发表回答