我尝试将两个 pdf url 文件合并到一个 PDFView。
import Foundation
import PDFKit
protocol PdfViewDelegate: class {
func heightDidReccived(height:CGFloat)
}
class PdfView: YITStoryboardCustomXibView {
weak var delegate: PdfViewDelegate?
@IBOutlet weak var pageNumbersLabel: UILabel!
@IBOutlet weak var showThumbnailButton: UIButton!
@IBOutlet weak var pdfViewContainer: UIView!
@IBOutlet weak var thumbnailViewContainer: UIView!
@IBOutlet weak var pageNumbersView: UIView!
@IBOutlet weak var pdfViewContainerHeight: NSLayoutConstraint!
@IBOutlet weak var thumbnailViewHeightConstraint: NSLayoutConstraint!
var pdfView = PDFView()
var subData = Data()
let pageCount = 1
var pageReturnCount = 0
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
pdfScaleFactor()
}
override func xibName() -> String {
return "PdfView"
}
func openPdf(url: String) {
let webService = WebService()
webService.delegate = self
webService.getRequestFor(url: "http://www.orimi.com/pdf-test.pdf")
let webService1 = WebService()
webService1.delegate = self
webService1.getRequestFor(url: "http://www.pdf995.com/samples/pdf.pdf")
}
func getUrl(name:String) -> URL{
let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
return resourceDocPath.appendingPathComponent("\(name).pdf")
}
func configurPdfView(data:Data){
self.pdfView = PDFView()
if let document = PDFDocument(data: subData) {
pdfView.autoresizesSubviews = true
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
pdfView.displayDirection = .horizontal
pdfView.autoScales = true
pdfView.displaysRTL = true
pdfView.displayMode = .singlePageContinuous
pdfView.document = document
pdfView.backgroundColor = .clear
pdfViewContainer.addViewWithConstraint(view: pdfView, toView: pdfViewContainer)
pdfView.usePageViewController(true, withViewOptions:["interPageSpacing": -50])
pdfViewContainer.layer.masksToBounds = false
pdfViewContainer.layer.shadowOffset = CGSize(width: 0, height: 7)
pdfViewContainer.layer.shadowRadius = 10
pdfViewContainer.layer.shadowOpacity = 0.16
}
}
@IBAction func showThumbnailView(_ sender: Any) {
}
private func pdfScaleFactor(){
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
}
@objc func updatePageChange() {
if let pageNumber = pdfView.currentPage?.pageRef?.pageNumber,let total = pdfView.document?.pageCount {
pageNumbersLabel.text = "\(pageNumber) מתוך \(total)"
}
}
}
extension PdfView : WebServiceDelegate {
func didReceiveData(data: Data) {
print("subData 1\(data.count)\n")
print("subData 2\(subData.count)\n")
subData.append(data)
if pageCount != pageReturnCount {
pageReturnCount = pageReturnCount + 1
}else{
DispatchQueue.main.async {
self.configurPdfView(data: self.subData)
}
}
}
}
类网络服务
class WebService: NSObject {
weak var delegate: WebServiceDelegate?
func getRequestFor(url:String) {
print("getRequestFor : \(url) \n")
if WebService.isConectedToInternet(){
AF.request(url,method:.get)
.response { response in
if let data = response.data {
DispatchQueue.global(qos: .background).async {
self.delegate?.didReceiveData(data: data)
}
}
}
}
}
首先我尝试:
但总是 PDFView 加载最后一个 pdf 文件而不是合并 file1 和 file2,我看到合并 data1 和 data2 后数据更大。
第二 :
我尝试合并两个 PDFDocument 但仍然无法正常工作。
我做错了什么?有可能做这样的事情吗?