【问题标题】:how to load pdf file in UIWebview center如何在 UIWebview 中心加载 pdf 文件
【发布时间】:2013-09-10 07:48:54
【问题描述】:

我是 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(本地)执行此操作?

【问题讨论】:

  • UIWebView 不适合那样使用。寻找 pdf 查看器/解析器,类似于 github.com/vfr/Reader
  • @NimrodGutman 是否可以在 html 代码的帮助下加载 pdf 文件 uiwebview center 使用
  • 已经在某处看到过截图……你已经问过同样的问题了吗?
  • @iOSCoder yes but not same.previously 问如何加载pdf文件uiwebview全屏
  • @Ravindhiran...如何拥有 (1) 一个灰色背景的 view1 (2) 然后在 view1 的中心添加 webview?

标签: iphone ios objective-c pdf uiwebview


【解决方案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】:

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

    先获取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);
    

    【讨论】:

    • 我不想动态更改 UIWebview 框架。它是固定框架,为什么因为如果用户想要缩放 pdf 文件,它就是扩展全屏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多