↧
Display file pdf in android application
We did thetest program
open filepdf
The program hasseveral steps:
1. Convert
byte array to file pdf -> docname.pdf
2. Save
this file on storage -> getApplicationContext().getFilesDir()
3. Open
file in webwiew
There code:
WebView mDocDisplay_wv;
….//init of webview
int ViewSize=
mDocDisplay_wv.getWidth();
// select a document and
get bytes
File file = new File(getApplicationContext().getFilesDir()
+ "/docname.pdf" );
RandomAccessFile raf = new
RandomAccessFile(file, "r");
FileChannel channel =
raf.getChannel();
ByteBuffer bb =
ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()));
raf.close();
// create a pdf doc
PDFFile pdf = new
PDFFile(bb);
String html = "<!DOCTYPE
html><html><body bgcolor=\"#ffffff\">"
//loop through the rest
of the pages and repeat the above
for(int i = 1; i <=
pdf.getNumPages(); i++)
{
PDFPage PDFpage =
pdf.getPage(i, true);
float scale =
ViewSize / PDFpage.getWidth() * 0.6f;
Bitmap page =
PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true,
true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
page.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
byte[] byteArray =
stream.toByteArray();
String base64 =
Base64.encodeToString(byteArray, Base64.DEFAULT);
html +=
"<img src=\"data:image/png;base64,"+base64+"\"
hspace=0 vspace=0><br>";
}
html +=
"</body></html>";
//load the html in the
webview à mDocDisplay_wv
mDocDisplay_wv.loadDataWithBaseURL("", html,
"text/html","UTF-8", "");
Questopns:
1. How
open pdf file from byte array without convert byte array to file and without
save on storage?
2. We
do not wanttoscrollhorizontallywas
↧