1

I am Using QLPreviewController for editing the PDF. Everything is working properly but if I add text annotation from the QLPreviewController and click on the done button it is working as expected I am getting the URL with edited PDF and if I do other changes for example add image annotation from outside of QLPreviewController and save that pdf using the code

   self.pdfDocument.write(to: self.pdfUrl)

and if I open that pdf in the QLPReviewController and tap on the previously added text then the text annotation background becomes Black and the font becomes small. I have attached the video please check.

**Video Link : ** https://www.dropbox.com/s/m0ql1csr8pars3t/Issue_video.mov?dl=0

I have used the below code for pdf mark up

    func openPreview(_ url : URL){
        let previewController = QLPreviewController()
        previewController.isEditing = true
        previewController.delegate = self
        self.pdfUrl = url
        previewController.dataSource = self
        previewController.modalPresentationStyle = .overFullScreen
        self.present(previewController, animated: true, completion: nil)
    }

    extension ViewController : QLPreviewControllerDataSource,QLPreviewControllerDelegate{
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }
    
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return self.pdfUrl as QLPreviewItem
    }
    
    func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
        return .updateContents
    }
    
    func previewControllerWillDismiss(_ controller: QLPreviewController) {
        
    }
    
    func previewController(_ controller: QLPreviewController, didUpdateContentsOf previewItem: QLPreviewItem) {
        DispatchQueue.main.async {
            self.pdfUrl = previewItem.previewItemURL
            self.setPDFFile()
            self.pdfDocument.write(to: self.pdfUrl)
        }
    }
}

func setPDFFile(){
    if let doc = PDFDocument(url: self.pdfUrl){
        self.pdfDocument = doc
        self.pdfView.document = doc
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
MahammedAsif
  • 111
  • 1
  • 6
  • 1
    Just make sure to override the original file. I have an app which allows editing as well and if I save it it doesn't happen. AFAIR I don't use `updateContents` mode. I always use `.createCopy` and let the user decide if he wants to overwrite or create a new copy. – Leo Dabus Aug 10 '21 at 17:43
  • @LeoDabus Oh that is a great idea. I think you can help us but we need to give you more details about project . How to be sure that it is overriding the original file? and also what if user wants to override? – neda Derakhshesh Aug 10 '21 at 20:03
  • 1
    @nedaDerakhshesh AFAIR I did it using `func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {`. The confirmation (UIAlertController) was done overriding `func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {` – Leo Dabus Aug 10 '21 at 20:07
  • @LeoDabus I should add this to my code or replace it with some of my functions? I really appreciate your help. Thank you very much – neda Derakhshesh Aug 10 '21 at 20:20
  • @LeoDabus I am trying to replace this with the function that includes updatecontent. – neda Derakhshesh Aug 10 '21 at 20:35
  • I did subclass QLPreviewController. My didUpdateContentsOf method does nothing. Don't forget to return `.createCopy` instead of `.updateContents` – Leo Dabus Aug 10 '21 at 20:36
  • @Leo Dabus I have tried the update contents also. Everything is working fine if i use only QLPreviewController for editing the pdf using update contents or create copy. but whenever i do any changes in the PDF without using the QLPreviewController and add line pdfDocument.writeTo(url). than this text issue is occurring. – MahammedAsif Aug 11 '21 at 03:38

0 Answers0