I can successfully import an existing PDF document into a new PDF that I'm building in memory, like this:
val combinedDocument = Document()
val stream = PDFMemStream(output)
combinedDocument.OpenStream(stream, null)
combinedDocument.SetCache(Global.tmp_path + "/ttt.dat")
val addDocument = Document()
addDocument.Open(documentFilename, null)
val context = combinedDocument.ImportStart(addDocument)
combinedDocument.ImportPage(context, thisPage, addPage + thisPage)
context.Destroy()
addDocument.Close()
combinedDocument.Save()
output = stream._data
Now I'm trying to add an existing JPEG file instead. From reading the source code and converting a few iOS examples, I've come up with this:
val combinedDocument = Document()
val stream = PDFMemStream(output)
combinedDocument.OpenStream(stream, null)
combinedDocument.SetCache(Global.tmp_path + "/ttt.dat")
val addImage = combinedDocument.NewImageJPEG(documentFilename)
val combinedDocumentPage = combinedDocument.GetPage(addPage)
val imageResource = combinedDocumentPage.AddResImage(addImage)
val matrix = Matrix(1f, -1f, 0f, combinedDocument.GetPageHeight(addPage))
val pageContent = PageContent()
pageContent.GSSave()
pageContent.GSSetMatrix(matrix)
pageContent.DrawImage(imageResource)
pageContent.GSRestore()
combinedDocumentPage.AddContent(pageContent, true)
combinedDocumentPage.Close()
combinedDocument.Save()
output = stream._data
But the image doesn't appear. I tried a few different image files from different sources, and I think the image file is good because if I load it into a bitmap object, and click View Bitmap in the debugger, the image appears.
To test the rest of the code, I tried adding a line annotation, and that appeared:
val combinedDocument = Document()
val stream = PDFMemStream(output)
combinedDocument.OpenStream(stream, null)
combinedDocument.SetCache(Global.tmp_path + "/ttt.dat")
val startPoint = FloatArray(2)
startPoint[0] = 50f
startPoint[1] = 50f
val endPoint = FloatArray(2)
endPoint[0] = 100f
endPoint[1] = 100f
combinedDocumentPage.AddAnnotLine(startPoint, endPoint, 0, 0, 10f, Color.RED, 0)
combinedDocumentPage.Close()
combinedDocument.Save()
output = stream._data
But an image annotation doesn't appear:
val combinedDocument = Document()
val stream = PDFMemStream(output)
combinedDocument.OpenStream(stream, null)
combinedDocument.SetCache(Global.tmp_path + "/ttt.dat")
val addImage = combinedDocument.NewImageJPEG(documentFilename)
val frame = FloatArray(4)
frame[0] = 100f
frame[1] = 200f
frame[2] = 200f
frame[3] = 100f
combinedDocumentPage.AddAnnotBitmap(addImage, frame)
combinedDocumentPage.Close()
combinedDocument.Save()
output = stream._data
I found several forum posts saying "...then rerender the page," but I can't figure out how to do that. The only examples I've found work from a PDFView or PDFVPage, and I just have a Page object.
If the rerender is needed, how do I do that in this situation? Or am I missing something else?
I have a premium license.