- Posts: 56
- Thank you received: 1
Microsoft Windows Phone 8.1 support ends (13 Jul 2017)
Microsoft has ended support for Windows Phone 8.1
Questions about Android development and PDF
Added JPEG not appearing
IP: 71.36.119.126
4 years 8 months ago #15527
by arlomedia
Added JPEG not appearing was created by arlomedia
I can successfully import an existing PDF document into a new PDF that I'm building in memory, like this:
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:
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:
But an image annotation doesn't appear:
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.
Code:
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:
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(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:
Code:
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:
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 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.
IP: 111.196.244.79
4 years 8 months ago - 4 years 8 months ago #15528
by radaee
Replied by radaee on topic Added JPEG not appearing
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:
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:
Code:
pageContent.GSSave();
pageContent.GSSetMatrix(mat);
pageContent.DrawImage(imageResource);
pageContent.GSRestore();
Last edit: 4 years 8 months ago by radaee.
IP: 71.36.119.126
4 years 8 months ago #15529
by arlomedia
Replied by arlomedia on topic Added JPEG not appearing
Thanks for the quick reply. To use a simple Matrix example, should this work? It still shows no image:
[/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.
Code:
[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
Code:
)
val pageContent = PageContent()
pageContent.GSSave()
pageContent.GSSetMatrix(matrix)
pageContent.DrawImage(imageResource)
pageContent.GSRestore()
combinedDocumentPage.AddContent(pageContent, true)
combinedDocumentPage.Close()
combinedDocument.Save()
output = stream._data
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.
IP: 111.196.244.79
4 years 8 months ago - 4 years 8 months ago #15530
by radaee
Replied by radaee on topic Added JPEG not appearing
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.
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.
Last edit: 4 years 8 months ago by radaee.
IP: 71.36.119.126
4 years 8 months ago #15531
by arlomedia
Replied by arlomedia on topic Added JPEG not appearing
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.
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.
IP: 111.196.244.79
4 years 8 months ago #15532
by radaee
Replied by radaee on topic Added JPEG not appearing
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:
your codes will cause Page.AddContent() return false.
because the content need invoke PageContent.Create() to initialize buffer.
so, whole codes shall be:
Code:
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();
Time to create page: 0.433 seconds