【问题标题】:Display PDF downloaded from URL according to individual pages根据各个页面显示从 URL 下载的 PDF
【发布时间】:2026-01-16 21:55:02
【问题描述】:

在我的应用程序中,我有像根据页面显示 PDF 的要求。一次只有一页,底部有下一页和上一页按钮。点击“下一页”将加载下一页等等...

我知道使用 UIWebview 我们可以显示 PDF,但是整个 PDF 文档是一次显示的。每次只需要显示一个页面

NSURL* url = [NSURL URLWithString:@"http://ExMonthly.pdf"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
wbView.scalesPageToFit=YES;
[wbView loadRequest:request];
[self.view addSubview:wbView];

此代码显示该 pdf 的所有可用页面。如何获取单个页面

任何想法/帮助将不胜感激

【问题讨论】:

    标签: objective-c xcode swift ipad pdf


    【解决方案1】:

    Apple 有 QLPreviewController,但如果我没记错的话,还有整个 pfd。 您可以保存 pdf,下一个拆分 pdf 到单页 (for iOS see this answer),使用 UIWebView 和 2 个按钮 Next & Prev 制作自定义 UIViewController。接下来只需重新加载 UIWebView 以读取每个单独的页面。

    类似这样的:

    - (void)savePdfDataLocally {
        NSData *dataPdf = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://ExMonthly.pdf"]];
    
        //Get path directory
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        //Create PDF_Documents directory
        documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"PDF_Documents"];
        [[NSFileManager defaultManager] documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"ExMonthly.pdf"];
    
        [dataPdf writeToFile:filePath atomically:YES];
    }
    
    - (void)loadPage:(int)page {
        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath_from_previous_function]];
        webView.scalesPageToFit=YES;
        [webView loadRequest:request];
    } 
    
    - (IBAction)nextTap:(id)sender {
       currentPage++;
       [self loadPage:currentPage];
    }
    

    【讨论】: