0

I want to use PDFView framework in Swift 4. (https://developer.apple.com/documentation/pdfkit/pdfview)

The following function receives a path to a PDF document. If the path is valid, the PDF file is successful shown. A problem occurs, when I call openMe(path: String) twice. In this case, the old content is still there and the new content is added. I just want to change the old content with the new content.

private var pdfData: NSData? = nil

func openMe(path: String) {
   let fileManager = FileManager.default

   if fileManager.fileExists(atPath: path){
       let url = NSURL.fileURL(withPath: path)
       pdfData = NSData(contentsOfFile: path)
       let pdfView = PDFView(frame: self.view.frame)
       pdfViewController?.pdfViewControllerInformsMeasurementDataViewController = self
       pdfView.document = PDFDocument(url: url)
       pdfView.autoScales = true
       pdfView.maxScaleFactor = 0.5
       pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
       pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

       self.pdfViewController?.view.addSubview(pdfView)
       self.show(self.pdfViewController!, sender: nil)
    }
}

EDIT

Refer to excitedmicrobe's answer: I just changed the code like shown in the answer but the distance between the navigation controller and the PDFView differs.

Fist openMe call:

enter image description here

Second openMe call:

enter image description here

PascalS
  • 975
  • 1
  • 16
  • 40

2 Answers2

1

In that case, you would need to make your pdfView global:

Before viewDidLoad() add the following:

var pdfView = PDFView()

override func viewDidLoad() {
    super.viewDidLoad()

    // ....
}

And edit your code openMe() to:

func openMe(path: String) {
     if self.pdfViewController?.view.subviews.contains(pdfView) {
          self.pdfView.removeFromSuperview() // Remove it
      } else {
          // Do Nothing
      }

     let fileManager = FileManager.default

   if fileManager.fileExists(atPath: path){
       let url = NSURL.fileURL(withPath: path)
       pdfData = NSData(contentsOfFile: path)
       self.pdfView = PDFView(frame: self.view.frame)
       pdfViewController?.pdfViewControllerInformsMeasurementDataViewController = self
       pdfView.document = PDFDocument(url: url)
       pdfView.autoScales = true
       pdfView.maxScaleFactor = 0.5
       pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
       pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

       self.pdfViewController?.view.addSubview(pdfView)
       self.show(self.pdfViewController!, sender: nil)
     }
}
excitedmicrobe
  • 2,338
  • 1
  • 14
  • 30
  • Thanks for your reply! +1 Now it works, but after the first function call, the PDF file is shown a bit lower. – PascalS Nov 26 '18 at 10:07
  • What do you mean lower? My code has nothing to do with the layout. So it might be your code – excitedmicrobe Nov 26 '18 at 10:07
  • pdf view is like a a book page, in the first openMe. if you click debug hierarchy. You would see that it’s the same. Just like the iBooks app. If you swipe left or right, the other one becomes visible. So in the first one we had 2 views. First became the 1st page of the book, second became visible and first got in the back of it – excitedmicrobe Nov 26 '18 at 10:25
  • Ok, it might be standard but it doesn't fit to my purpose. My workaround is to set `pdfView = PDFView(frame: self.view.frame)` in viewDidLoad(). Now the appearance is always the same. – PascalS Nov 26 '18 at 10:41
  • I’m afraid that’s the only way. Unless you add a white background to it. Cause the view of pdfview depends on the pdf itself – excitedmicrobe Nov 26 '18 at 10:47
1

In this situation, you have to achieve document using try? Data(contentsOf: url).

 if let url = URL.init(string: path), let data = try? Data(contentsOf: url), let pdfDocument = PDFDocument.init(data: data){
            
            
            pdfView.displayMode = .singlePageContinuous
            pdfView.autoScales = true
            pdfView.displayDirection = .vertical
            pdfView.document = pdfDocument
            //                        
        }
sachin
  • 11
  • 1