iOS开发采用pdfkit ,可以很方便的读取pdf的内容。以下是简易的显示pdf内容的代码
import UIKit
import PDFKitclass ViewController: UIViewController, UIDocumentPickerDelegate {var pdfView: PDFView! //创建一个控件显示内容let selectPDFButton = UIButton(type: .system) //点击按钮选择打开pdf文件override func viewDidLoad() {super.viewDidLoad()view.backgroundColor = .whiteselectPDFButton.setTitle("Select PDF", for: .normal)selectPDFButton.addTarget(self, action: #selector(selectPDF), for: .touchUpInside)selectPDFButton.translatesAutoresizingMaskIntoConstraints = falseview.addSubview(selectPDFButton)pdfView = PDFView()pdfView.backgroundColor = .redpdfView.translatesAutoresizingMaskIntoConstraints = falseview.addSubview(pdfView)selectPDFButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 120).isActive = trueselectPDFButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = trueselectPDFButton.widthAnchor.constraint(equalToConstant: 100).isActive = trueselectPDFButton.heightAnchor.constraint(equalToConstant: 50).isActive = true// 设置 pdfView 的约束pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = truepdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = truepdfView.topAnchor.constraint(equalTo: selectPDFButton.bottomAnchor, constant: 120).isActive = truepdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true}@objc func selectPDF() {let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.pdf])documentPicker.delegate = selfpresent(documentPicker, animated: true, completion: nil)}func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {guard let url = urls.first else { return }selectedPDFURL = url// 请求安全范围内的资源访问,这个startAccessingSecurityScopedResource很关键,否则可能打不开文件guard url.startAccessingSecurityScopedResource() else {print("Failed to start accessing security-scoped resource.")showErrorMessage("Failed to access the selected file.")return}defer {url.stopAccessingSecurityScopedResource()}// 尝试加载 PDF 文档guard let pdfDocument = PDFDocument(url: url) else {// 如果无法加载 PDF 文档,打印错误信息print("Failed to load PDF document at URL: \(url)")showErrorMessage("Failed to load PDF document.")return}print("load PDF document success ")pdfView.document = pdfDocumentpdfView.autoScales = truepdfView.layoutSubviews()print("load PDF document success ")// 可以在这里显示一些提示,告知用户文件已选择let alertController = UIAlertController(title: "Info", message: "PDF file selected successfully.", preferredStyle: .alert)alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))present(alertController, animated: true, completion: nil)// 显示 PDF 文件}private func showErrorMessage(_ message: String) {let alertController = UIAlertController(title: "Error", message: message, preferredStyle: .alert)alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))present(alertController, animated: true, completion: nil)}private func showSuccessMessage(_ message: String) {let alertController = UIAlertController(title: "Success", message: message, preferredStyle: .alert)alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))present(alertController, animated: true, completion: nil)}}