I am trying to wrap the PDFRenderer API for Lollipop. I have the following Java code:
my B4A test code:
RenderPageToFile works fine and generates exactly what I would expect. The initialize3 call and WriteToStream generates a zero byte file. The Log for ByteCount shows 2003960.
B4X:
public Bitmap RenderPage(int PageNumber) {
PdfRenderer.Page page = renderer.openPage(PageNumber);
BA.Log(Integer.toString(page.getWidth()));
BA.Log(Integer.toString(page.getHeight()));
Bitmap bmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_4444);
page.render(bmap,null,null,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
page.close();
BA.Log(Integer.toString(bmap.getByteCount()));
return bmap;
}
/**
* Renders Bitmap to PNG File
*
*/
public void RenderPageToFile(int PageNumber,String Path, String FileName) throws FileNotFoundException {
PdfRenderer.Page page = renderer.openPage(PageNumber);
BA.Log(Integer.toString(page.getWidth()));
BA.Log(Integer.toString(page.getHeight()));
Bitmap bmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_4444);
File file = new java.io.File(Path,FileName);
page.render(bmap,null,null,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
page.close();
bmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file)) ;
}
my B4A test code:
B4X:
Sub Process_Globals
Dim sWriteDir As String
Dim PDFRenderer As PDFRender
End Sub
Sub Globals
Dim showPDF As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Dim b As Bitmap
sWriteDir = File.DirRootExternal & "/PDFTest"
PDFRenderer.Initialize(sWriteDir,"Test.pdf")
b.Initialize3(PDFRenderer.RenderPage(0))
b.WriteToStream(File.OpenOutput(sWriteDir,"TestFromBA.png",False),100,"PNG")
PDFRenderer.RenderPageToFile(0,sWriteDir,"Test.png")
End Sub
RenderPageToFile works fine and generates exactly what I would expect. The initialize3 call and WriteToStream generates a zero byte file. The Log for ByteCount shows 2003960.