Thank you for the quick response Ryan.
I have read through the docs and looked at the various techniques, tried various ways, but I am still not able to save an image.
The closest I get is with PTMemoryFilter to write the PNGData for the stream.
let filter = PTMemoryFilter(buf_sz: UInt(imageData.count), is_input: false)
But when calling the ‘createIndirectStream’, the system times out ( Terminated due to signal 9)
Is there any example of how to correctly pass the PNG byte data to set the stream up?
I’ve attached my testing func in hopes that I might be missing something obvious?
Thank you for your time…
P.
static func addAnnotation(pdfAnnotation: ImageStampAnnotation, pageNumber: Int) throws {
print("START")
guard let ptDocument = document else { throw PDFAnnotationError.documentNotFound }
guard pageNumber > 0, pageNumber <= ptDocument.getPageCount() else { throw PDFAnnotationError.invalidPageNumber }
guard let ptPage = ptDocument.getPage(UInt32(pageNumber)) else { throw PDFAnnotationError.pageNotFound }
guard let stamper = PTStamper(size_type: e_ptabsolute_size, a: pdfAnnotation.bounds.width, b: pdfAnnotation.bounds.height) else {
throw PDFAnnotationError.stamperCreationFailed
}
// Use any small image - still cause error
guard let image = pdfAnnotation.image else { throw PDFAnnotationError.missingAnnotationImage }
guard let imageData = image.pngData() else { throw PDFAnnotationError.pngImageConversionError }
//let bounds = pdfAnnotation.bounds
// Set the stamper properties
stamper.setAlignment(e_pthorizontal_left, vertical_alignment: e_ptvertical_top)
// Apply the stamp to the PTPage
let pageSet = PTPageSet(one_page: Int32(pageNumber))
stamper.stampPage(ptDocument, src_page: ptPage, dest_pages: pageSet)
// Testing / Set the main image for the annotation
var imageStream: PTObj? // use for adding the image to the Key/Value pair
let colorSpace = PTColorSpace.createDeviceRGB()
guard let img = PTImage.create(withData: ptDocument.getSDFDoc(),
buf: imageData, buf_size: UInt(imageData.count),
width: Int32(image.size.width), height: Int32(image.size.height),
bpc: 8, color_space: colorSpace, encoder_hints: PTObj()) else {
throw PDFAnnotationError.imageCreationFailed
}
// Create PTPageSet for the (1) page
let page = PTPageSet(one_page: Int32(pageNumber))
stamper.stampImage(ptDocument, src_img: img, dest_pages: page)
print("Create a PTFilter")
// Create a PTFilter (what type?)
// let filter = PTFilter()
//let filter = PTMemoryFilter()
let filter = PTMemoryFilter(buf_sz: UInt(imageData.count), is_input: false)
// Then create a PTFilterWriter to write the image data into the filter
guard let filterWriter = PTFilterWriter(filter: filter) else { throw PDFAnnotationError.filterWriterError }
print("Start filterWriter")
filterWriter.writeBuffer(imageData) // ERROR! Data exists but get an error ? wrong format with filter?
print("filterWriter Written")
filterWriter.flush()
print("flush")
// Create the PTFilterReader from the filter
guard let filterReader = PTFilterReader(filter: filter) else { throw PDFAnnotationError.filterReaderError }
// Create the indirect stream object for the objects image data
print("Start createIndirectStream")
imageStream = ptDocument.getSDFDoc()?.createIndirectStream(with: filterReader, filter_chain: nil)
print("createIndirectStream - Completed? Never completes....")
stamper.setAlignment(e_pthorizontal_left, vertical_alignment: e_ptvertical_top) // Set the stamper properties
stamper.stampPage(ptDocument, src_page: ptPage, dest_pages: page) // Apply the stamp to the 'PTPage'
// Retrieve the last annotation added to the page
// AKA - The one we just added.... 'This' one to add the KeyValue Pairs
let numAnnots = ptPage.getNumAnnots()
guard let ptAnnotation = ptPage.getAnnot(UInt32(numAnnots - 1)) else { throw PDFAnnotationError.newAnnotationGetError }
let sdfObj = ptAnnotation.getSDFObj() // Set custom properties
if let imageStream = imageStream { // Load the same image (for now - for testing...)
sdfObj?.put("imageA", obj: imageStream)
}
// Extra Clean Ups / Tests
sdfObj?.put("T", value: pdfAnnotation.userName)
sdfObj?.put("Name", value: pdfAnnotation.stampName)
sdfObj?.put("AP", value: "/Helvetica 12 Tf 0 g")
ptDocument.refreshFieldAppearances() // Refresh field appearances
print("completed")
}