3

我是 ios 开发的新手。我正在尝试在 UIWebview 中心显示 pdf。我在 mainbundle 中有一个 pdf。这是我的 PDF 现在加载到 UIWebView 时的样子:(我将棕色渐变作为 UIWebView 的背景,这样你就可以更好地理解我的意思)。 在此处输入图像描述

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

我想将其居中,以使下边距与上边距相同。我将如何使用 PDF(本地)执行此操作?

4

2 回答 2

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];
于 2014-06-05T11:17:32.990 回答
0

不确定这是否是您问题的理想解决方案。

先获取PDF的宽高==>

float width = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.width;

float height = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.height;

其中 Pageref 是 PDF 页面参考。

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]

//然后是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];

编辑:

获取 Pageref(部分),

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);
于 2013-09-06T09:27:39.257 回答