简体   繁体   中英

Cannot add image and save to pdf file using PDFKit

I have a class to edit pdf by inserting an image into the pdf and save a new pdf with the inserted image.

Below code is the way I use to achieve the scenario, by creating a custom PDFAnnotation . I specify type as .widget instead of .stamp to avoid having black line drawing across diagonal.

final private class PDFImageAnnotation: PDFAnnotation {

    var image: UIImage?

    convenience init(_ image: UIImage?, bounds: CGRect, properties: [AnyHashable: Any]?) {
        self.init(bounds: bounds, forType: PDFAnnotationSubtype.widget, withProperties: properties)
        self.image = image
    }

    override func draw(with box: PDFDisplayBox, in context: CGContext) {
        super.draw(with: box, in: context)

        // Drawing the image within the annotation's bounds.
        guard let cgImage = image?.cgImage else { return }
        context.draw(cgImage, in: bounds)
    }
}

Below is how I display pdf and select image to save in pdf

private func setupPDF() {
    guard let url = url else { return }
    pdfView.document = PDFDocument(url: url)
    pdfView.autoScales = true
}

private func addImageAndSave(_ image: UIImage) {
    guard let page = pdfView.currentPage else { return }

    let pageBounds = page.bounds(for: .cropBox)
    let imageBounds = CGRect(x: pageBounds.midX, y: pageBounds.midY, width: image.size.width, height: image.size.height)
    let imageStamp = PDFImageAnnotation(image, bounds: imageBounds, properties: nil)
    imageStamp.shouldDisplay = true
    imageStamp.shouldPrint = true
    page.addAnnotation(imageStamp)

    // Save PDF with image
    let fileName = url.lastPathComponent
    let saveUrl = FileManager.default.temporaryDirectory.appendingPathComponent(fileName, isDirectory: false)
    pdfView.document?.write(to: saveUrl)
}

However the result pdf file is below

The preview on right column does display the picture but when open the pdf in Preview app or any browser the picture is not there.

在此处输入图像描述

How can I make the picture appear in the final pdf file?

Thanks.

I have to also draw that image on PDFPage to make them appear in the saved pdf file

final private class ImagePDFPage: PDFPage {

    /// A flag indicates whether to draw image on a page
    /// 
    /// - Note: Set this to `true` before write pdf to file otherwise the image will not be appeared in pdf file
    var saveImageToPDF: Bool = false

    private var imageAnnotation: EkoPDFImageAnnotation? = nil


    func addImageAnnotation(_ annotation: EkoPDFImageAnnotation) {
        imageAnnotation = annotation
        addAnnotation(annotation)
    }

    func removeImageAnnotation() {
        guard let imageAnnotation = imageAnnotation else { return }
        self.imageAnnotation = nil
        removeAnnotation(imageAnnotation)
    }

    override func draw(with box: PDFDisplayBox, to context: CGContext) {
        super.draw(with: box, to: context)

        guard saveImageToPDF,
              let annotation = imageAnnotation,
              let cgImage = annotation.image?.cgImage else { return }
        context.draw(cgImage, in: annotation.bounds)
    }
}

and update PDFDocument.delegate to tell to use ImagePDFPage as a class for pdf page

extension PDFEditorViewController: PDFDocumentDelegate {
    func classForPage() -> AnyClass {
        return ImagePDFPage.self
    }
}

The result

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM