ios 如何在iOS上显示pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10878744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display pdf on iOS
提问by Yang Jie Domodomo
I want to know if there is any way for a offline build for xcode iOS
such that we can display pdf file from local file.
我想知道是否有任何方法可以进行离线构建,xcode iOS
以便我们可以从本地文件显示 pdf 文件。
The method I'm using now is via UIWebView
to display a custom HTML which contain the PDF URL (online) , but I want to make an offline version if possible.
我现在使用的方法是通过UIWebView
显示包含 PDF URL (online) 的自定义 HTML,但如果可能,我想制作一个离线版本。
But from online research, it seems to me the only way to display PDF is via UIWebView, if that is really the case, is it possible to pull the file from local resource (e.g. add files to my xcode resource folder) rather than using the PDF URL.
但是从在线研究来看,在我看来,显示 PDF 的唯一方法是通过 UIWebView,如果确实如此,是否可以从本地资源中提取文件(例如,将文件添加到我的 xcode 资源文件夹)而不是使用PDF 网址。
OFF-TOPIC: Which would be a better choice? To pull local file or to use the online URL for PDF file?
题外话:哪个是更好的选择?拉取本地文件还是使用在线 URL 获取 PDF 文件?
As UIWebView
require connection, it would seem to be a better choice to just pull the online URL as it will be the most current and updated.
作为UIWebView
需要连接,只提取在线 URL 似乎是一个更好的选择,因为它将是最新的和更新的。
Feedback is much appreciated.
非常感谢反馈。
EDIT: quick question, if I use the UIDocumentInteractionController
method or the QuickLook frameworkto make it offline, this would mean I have to release update patch each time there is new PDF article added in (correct me if I'm wrong)
编辑:快速问题,如果我使用该UIDocumentInteractionController
方法或QuickLook 框架使其离线,这意味着每次添加新的 PDF 文章时我都必须发布更新补丁(如果我错了,请纠正我)
Taking this point into consideration, I've come up with a counter-argument to my own question on the spot (Sorry if any of you feel you are wasting time on me >.<, I am a person who like to question possibilities and the different approach one could take to solve a problem) In the event to save time from the constant updating and providing a real time base solution, which is to use UIWebView (I know this is contradicting my purpose of this SO thread), as this proj is base on a online website (getting my content and sources from there), if I were to load their website for the article section, would it be bold of me to say that by adding a NSString variable, I can add in the NSRange function to the viewDidLoad together with the UIWebView such that the URL will never leave the news section and PDF URL. e.g. using combination of if and NSRange function to counter check that the URL only contain the news section and PDF URL)
考虑到这一点,我当场就我自己的问题提出了反驳(对不起,如果你们中的任何人觉得你在我身上浪费时间>.<,我是一个喜欢质疑可能性和解决问题的不同方法)如果要从不断更新中节省时间并提供实时基础解决方案,即使用 UIWebView(我知道这与我的这个 SO 线程的目的相矛盾),因为这proj 基于在线网站(从那里获取我的内容和来源),如果我要为文章部分加载他们的网站,我敢说通过添加 NSString 变量,我可以添加到 NSRange viewDidLoad 与 UIWebView 一起使用,这样 URL 永远不会离开新闻部分和 PDF URL。例如
I did once before a NSRange function for point on calculator so I thought it would be a good time to put it in use for this framework app.
我在 NSRange 函数之前做过一次计算器上的点,所以我认为现在是将它用于这个框架应用程序的好时机。
P.S - This app is a organization base project and I'm a intern providing them with the base skeleton framework.
PS - 这个应用程序是一个组织基础项目,我是一名实习生,为他们提供基础框架框架。
回答by calimarkus
Many options, here are 3:
很多选择,这里有3个:
1) The easiest way to load and display a local pdf file is to use a UIWebview like that:
1)加载和显示本地pdf文件的最简单方法是使用这样的UIWebview:
NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
2) You can also use a UIDocumentInteractionController
/QLPreviewController
to display PDF Files natively.
2) 您还可以使用UIDocumentInteractionController
/QLPreviewController
来本地显示 PDF 文件。
3) Another way would be to build a custom PDF Viewer, as in apples ZoomingPDFViewer example code. (using UIPageViewController + CATiledLayer + UIScrollView)
3)另一种方法是构建自定义PDF查看器,如苹果ZoomingPDFViewer示例代码。(使用 UIPageViewController + CATiledLayer + UIScrollView)
回答by self
You can use a UIWebView to get the PDF from your bundle directly, no need to call an online web service.
您可以使用 UIWebView 直接从您的包中获取 PDF,无需调用在线 Web 服务。
Local File:
本地文件:
NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
Remote File:
远程文件:
- (void) loadRemotePdf
{
CGRect rect = [[UIScreen mainScreen] bounds];
? ?CGSize screenSize = rect.size;
? ? ? ?
? ? UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
? ? ? ? webView.autoresizesSubviews = YES;
? ? ? ? webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
? ?NSURL *myUrl = [NSURL URLWithString:@"http://www.mysite.com/test.pdf"];
? ?NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
? ? ? ?
? ? [webView loadRequest:myRequest];
? ?[window addSubview: myWebView];
? ?[myWebView release];
? ? ? ?
}
回答by Rui Peres
I advise you using the QuickLook frameworkwhile handling PDF files.
我建议您在处理 PDF 文件时使用QuickLook 框架。
回答by Argus
SWIFT 4.x
斯威夫特 4.x
UIWebview approach in swift:
快速的 UIWebview 方法:
//let url = URL.init(string: "https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf")! // for file in web
let url = Bundle.main.url(forResource: "filename", withExtension: ".pdf")! // for file in bundle
let webView = UIWebView.init(frame: view.frame)
webView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
webView.loadRequest(URLRequest.init(url: url))
view.addSubview(webView)
回答by Rizan Zaky
回答by user2616647
I found this site and is a very easy method to open pdf's with any available pdf capable app that is loaded on the iPhone. (I prefer iBooks)
我找到了这个网站,它是一种非常简单的方法,可以使用 iPhone 上加载的任何可用的支持 pdf 的应用程序打开 pdf。(我更喜欢 iBooks)
回答by Ved Rauniyar
NSString *path = [[NSBundle mainBundle] pathForResource:@"Yourpdf" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
[[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]];
[webView loadRequest:request];
[self.view addSubview:webView];
回答by Shaik Riyaz
It's late but there is some limitation with UIWebview on loading pdf files, It won't load editable pdf properly. so load any type of pdf files use following code
为时已晚,但 UIWebview 在加载 pdf 文件方面存在一些限制,它无法正确加载可编辑的 pdf。所以加载任何类型的pdf文件使用以下代码
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"pdfFileName" ofType:@"pdf"]];
PDFView *view = [[PDFView alloc] initWithFrame:self.view.frame];
view.document = [[PDFDocument alloc] initWithURL:url];
[self.view addSubview:view];
回答by Ali A. Jalil
Apple's PDFKit framework provides a huge range of code to help us work with PDFs, and one of the most useful is PDFView – it renders PDFs to the screen and lets users interact with them.
Apple 的 PDFKit 框架提供了大量代码来帮助我们处理 PDF,其中最有用的一个是 PDFView——它将 PDF 呈现到屏幕上并让用户与它们进行交互。
To try it out, start by importing the PDFKit framework:
要尝试一下,首先导入 PDFKit 框架:
import PDFKit
Next, add this code to your viewDidLoad() method to create a PDFView and make it fill all available space:
接下来,将此代码添加到您的 viewDidLoad() 方法以创建一个 PDFView 并使其填充所有可用空间:
let pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
Finally, create a URL pointing to a PDF you have in your bundle somewhere (or one in your documents directory), then create a PDFDocument object from that and pass it to the PDF view:
最后,创建一个 URL,指向您的包中某处(或您的文档目录中的一个)的 PDF,然后从中创建一个 PDFDocument 对象并将其传递给 PDF 视图:
guard let path = Bundle.main.url(forResource: "example", withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
}
Done!
完毕!
回答by Tracker6
In my case, I have a tableView that segues to a PDFView based on either a selected row or detail indicator (there are two different ViewControllers for each in different parts of my app). I have an @IBOutlet for the PDFView and use this code to open the desired PDF, which is saved in the app:
在我的例子中,我有一个 tableView,它根据选定的行或详细信息指示器(在我的应用程序的不同部分有两个不同的 ViewControllers)转为一个 PDFView。我有一个用于 PDFView 的 @IBOutlet 并使用此代码打开所需的 PDF,该 PDF 保存在应用程序中:
@IBOutlet weak var myPDFView: PDFView!
var mySelection = String()
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: mySelection, ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let myDocument = PDFDocument(url: url) {
myPDFView.document = myDocument
myPDFView.autoScales = true
myPDFView.displayMode = .singlePageContinuous
myPDFView.displayDirection = .vertical
myPDFView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
}
This all works great. The correct PDFs load easily and fill the screen like I want. However, the PDF is always scrolled up just a little, causing the top portion to be hidden under the navigation bar. Notice in the screen shot below that the top of the document is not visible but the bottom is, just above the tab bar at the bottom. I think this has to do with PDF coordinates starting at the bottom of the page but cannot figure out how to get it to load the very top of the document, so the background at the top is visible.
这一切都很好。正确的 PDF 可以轻松加载并像我想要的那样填满屏幕。但是,PDF 总是向上滚动一点,导致顶部隐藏在导航栏下方。请注意,在下面的屏幕截图中,文档的顶部不可见,但底部可见,就在底部选项卡栏的上方。我认为这与从页面底部开始的 PDF 坐标有关,但无法弄清楚如何让它加载文档的最顶部,因此顶部的背景是可见的。
Perhaps this has to do with autoScales?
也许这与 autoScales 有关?