-1

After I remove/add the annotations I save the file to disk (is this even necessary to properly save the annotations?). I then have the document in a UIActivityViewController inside a UIActivityItemProvider.

The annotations are viewable in the print preview, PDF Expert, Firefox, Gmail browser, Preview, etc. - just not Acrobat (and exporting to .doc)

Using Acrobat Reader Build 19.21.20061.361316; using Xcode 11.3

The PDF I am editing or here

Example annotation:

// Open PDF etc
if annotation.fieldName == "form1[0].Page1[0].WE_FACTR[0]" {
     annotation.setValue("30.0", forAnnotationKey: .widgetValue)
     page.removeAnnotation(annotation)
     page.addAnnotation(annotation)
}
// save PDF to file
// PDF File is in the UIACtivityViewController

I was wondering if anyone had a workaround for missing annotations in PDFKit for Adobe Reader.

When I asked @steipete from PSPDFKit he told me that this is why people use his framework. There is a drop-in replacement call PDFXKit that he suggested.

It didn't work with PSPDFKit either! XFA forms don't seem to be supported.

twodayslate
  • 2,803
  • 3
  • 27
  • 43
  • Possibly related: https://stackoverflow.com/questions/58860015/saved-pdfview-with-pdfkit-ios-not-showed-properly-in-adobe-reader – twodayslate Jan 29 '20 at 20:22

2 Answers2

4

I had the same issue, I am opening a PDF with a form in the app and fill it up with data from different textField. Unfortunately as it was very well explained by mkl, all the annotations are visible on all Pdf reader expect the one that most people use...Acrobat... I have been looking for days for a solution and since that unfortunately PDFKit does not have at this time an option to flatten a PDF, I have to come up with an "ugly" solution. But at least for my needs it works. Once I have updated the PDF form with all the annotation, I convert it to an image and then convert it back to a PDF. I have now a "flatten" pdf... It is not great and I hope that Apple will add a function to flatten a pdf in their PDFkit in the future.

if there is a better solution, Let me know!

convert to image

func pdfToImage(url: URL) -> [UIImage]? {
    
    let pdfDocument = PDFDocument(url: url)!
    let numbPages = pdfDocument.pageCount
   
    var imagePdf = [UIImage]()
    
    for i in 0...numbPages-1 {
        
        let page = pdfDocument.page(at: i)!
        let bounds = page.bounds(for: .mediaBox)
        
        
        let renderer = UIGraphicsImageRenderer(bounds: bounds, format: UIGraphicsImageRendererFormat.default())
        
         let img = renderer.image { (context) in
        
         context.cgContext.saveGState()

         context.cgContext.translateBy(x: 0, y: bounds.height)
        
         context.cgContext.concatenate(CGAffineTransform.init(scaleX: 1, y: -1))
         
         page.draw(with: .mediaBox, to: context.cgContext)
         
         context.cgContext.restoreGState()
         }
        
        imagePdf.append(img)
       
   
}
     return imagePdf
}

convert back to PDF

func imageToPDF(image: [UIImage])-> PDFDocument? {
    let pdfDocument = PDFDocument()
    for (index,image) in image.enumerated() {
        let pdfPage = PDFPage(image: image)
        pdfDocument.insert(pdfPage!, at: index)
    }
    return pdfDocument
}

and then I just call those functionss

    let imageFromPdf = pdfToImage(url: pdfURL)!
   
    let flattenPDF = imageToPDF(image: imageFromPdf)
    
    flattenPDF?.write(to: pdfURL2)
Julien7377
  • 475
  • 4
  • 15
2

Your PDF has a XFA/AcroForm hybrid form definition. That means that the form is defined twice in the PDF, once using AcroForm objects, the "native" PDF form format, and once using a XFA XML stream, a meanwhile deprecated alternative form technique allowing more dynamic forms.

If your PDF is opened in a PDF processor supporting XFA (which foremost means Adobe viewers plus very few other processors), the XFA form definition is processed (e.g. displayed) and all the normal PDF content is ignored, merely values in the AcroForm form definition are updated to match the XFA form values.

If your PDF is opened in a PDF processor not supporting XFA (i.e. essentially all except Adobe products), the normal PDF content including the Acroform form definition is processed.

This explains your observation

The annotations are viewable in the print preview, PDF Expert, Firefox, Gmail browser, Preview, etc. - just not Acrobat (and exporting to .doc)

(As I only see now you found out about XFA yourself and edited a remark pointing there into your question.)

The usual solution in this situation is to remove the XFA form definition. Once it is removed, all processors (the Adobe viewers, too) will only process the normal PDF content including the AcroForm form definition.

To do so you merely have to remove XFA entry in the AcroForm dictionary of the PDF.

If a usage rights signature is present in the document (as is in yours), that removal will invalidate it. In such a case you should also remove the Perms entry in the Catalog.

Unfortunately I do not know your libraries, so I cannot show the code to implement this with them.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thank you very much for the response! This seems to be the correct answer to my problems. I'm on iOS so [PDFKit](https://developer.apple.com/documentation/pdfkit) would be the library I would use on device. I haven't found any software to remove the XFA entries. I have not tried Acrobat Pro yet since I don't own it. – twodayslate Jan 28 '20 at 13:20