Signin/Signup with: 
Welcome, Guest
Username: Password: Remember me
Questions about Android development and PDF
  • Page:
  • 1
  • 2

TOPIC:

Added JPEG not appearing 2 years 9 months ago #15527

  • arlomedia
  • arlomedia's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 56
  • Thank you received: 1
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.

Please Log in or Create an account to join the conversation.

Added JPEG not appearing 2 years 9 months ago #15528

  • radaee
  • radaee's Avatar
  • Offline
  • Moderator
  • Moderator
  • Posts: 1123
  • Thank you received: 73
dear user:
PDF system always process image object as (1 * 1) size.
so, when you need display an image to PDF file, you shall create Matrix like
Matrix mat = new Matrix(disp_width, disp_height, x0, top);
or 
Matrix mat = new Matrix(disp_width, -disp_height, x0, page_height - bottom);
and then output image like:
pageContent.GSSave();
pageContent.GSSetMatrix(mat);
pageContent.DrawImage(imageResource);
pageContent.GSRestore();

Please Log in or Create an account to join the conversation.

Last edit: by radaee.

Added JPEG not appearing 2 years 9 months ago #15529

  • arlomedia
  • arlomedia's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 56
  • Thank you received: 1
Thanks for the quick reply. To use a simple Matrix example, should this work? It still shows no image:
[code]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(100f, 100f, 100f, 100f
)
val pageContent = PageContent()
pageContent.GSSave()
pageContent.GSSetMatrix(matrix)
pageContent.DrawImage(imageResource)
pageContent.GSRestore()
combinedDocumentPage.AddContent(pageContent, true)
combinedDocumentPage.Close()

combinedDocument.Save()
output = stream._data
[/code]

By the way, none of the objects created above show as null in the debugger. addImage, imageResource, combinedDocumentPage and pageContent all seem to be valid objects.

Please Log in or Create an account to join the conversation.

Added JPEG not appearing 2 years 9 months ago #15530

  • radaee
  • radaee's Avatar
  • Offline
  • Moderator
  • Moderator
  • Posts: 1123
  • Thank you received: 73
dear user:
what is "documentFilename"? is it absolute path to jpeg image file?
if you want add page as image from another PDF document, please render that PDF file to Bitmap object and save to jpeg file, 
then add jpeg file to destination PDF file.

Please Log in or Create an account to join the conversation.

Last edit: by radaee.

Added JPEG not appearing 2 years 9 months ago #15531

  • arlomedia
  • arlomedia's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 56
  • Thank you received: 1
Yes, documentFilename is the absolute path to a JPEG file. I can load it with File() and display it in a WebView, so I know the path is correct and readable.

output is a ByteArray generated from an Android PdfDocument object. I'm building the PDF data in memory and then I want to add an image that's saved as a local JPEG file.

I don't want to add an image that's inside another PDF file. I only showed my code to import a PDF file to show which parts of my code are confirmed to work.

Please Log in or Create an account to join the conversation.

Added JPEG not appearing 2 years 9 months ago #15532

  • radaee
  • radaee's Avatar
  • Offline
  • Moderator
  • Moderator
  • Posts: 1123
  • Thank you received: 73
sorry, we have found the issue later.
your codes will cause Page.AddContent() return false.
because the content need invoke PageContent.Create() to initialize buffer.
so, whole codes shall be:
Document.DocImage dimg = m_doc.NewImageJPEG("/sdcard/img.jpg");
Page page = m_doc.GetPage(0);
page.ObjsStart();
ResImage rimg = page.AddResImage(dimg);

PageContent content = new PageContent();
content.Create();
content.GSSave();
Matrix mat = new Matrix(100, -100, 100, 200);
content.GSSetMatrix(mat);
content.DrawImage(rimg);
content.GSRestore();
boolean bret = page.AddContent(content, true);
content.Destroy();
page.Close();
//m_doc.Save();

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2
Powered by Kunena Forum