android - Conversion of PDF files to BMP and print -
i need working android code print pdf files bmp. code had able print text blurry , shrinked difficult read.the device used print small paper width of 384. printed text should clear read.
public class pdftoimage { private int viewsize = 384; private string pdferrorcode = null; private string pdfimageerrorcode = null; private string pdfimagesaveerrorcode = null; private string returnerror = null; public string pdftoimage(file pdffilepath) { pdfimage.sshowimages = true; pdfpaint.s_doantialias = true; hardreference.skeepcaches = true; try { randomaccessfile pdfaccessfile = new randomaccessfile(pdffilepath, "r"); byte[] pdfdata = new byte[(int) pdfaccessfile.length()]; pdfaccessfile.readfully(pdfdata); returnerror = pdfloadimages(pdfdata); pdferrorcode = "success"; } catch (exception ignored) { pdferrorcode = "pdf file not found"; } if (returnerror.equals("pdf bmp conversion success")) { return pdferrorcode; } else { return "failed"; } } @suppresslint("newapi") private string pdfloadimages(final byte[] data) { try { bytebuffer bytebuffer = bytebuffer.new(data); pdffile pdffile = new pdffile(bytebuffer); pdfpage pdfpage = pdffile.getpage(1, true); final float scaleimage = viewsize / pdfpage.getwidth() * 0.95f; bitmap bitmappdfpage = pdfpage.getimage( (int) (pdfpage.getwidth() * scaleimage), (int) (pdfpage.getheight() * scaleimage), null, true, true); saveimage(bitmappdfpage, 1); bytearrayoutputstream stream = new bytearrayoutputstream(); bitmappdfpage.compress(bitmap.compressformat.png, 100, stream); stream.reset(); (int = 2; <= pdffile.getnumpages(); i++) { pdfpage = pdffile.getpage(i, true); bitmappdfpage = pdfpage.getimage( (int) (pdfpage.getwidth() * scaleimage), (int) (pdfpage.getheight() * scaleimage), null, true, true); bitmappdfpage.compress(bitmap.compressformat.png, 100, stream); saveimage(bitmappdfpage, i); pdfimageerrorcode = "pdf bmp conversion success"; } stream.close(); } catch (exception e) { log.d("error", e.tostring()); pdfimageerrorcode = "pdf bmp conversion failed"; } system.gc(); return pdfimageerrorcode; } private string saveimage(bitmap pdfbitmap, int pagenumber) { string sdcardpath = environment.getexternalstoragedirectory() .tostring(); file pdfdir = new file(sdcardpath + "/pdftobmp"); pdfdir.mkdirs(); string pdftoimagefilename = "pdf-" + pagenumber + ".png"; file imagefile = new file(pdfdir, pdftoimagefilename); if (imagefile.exists()) imagefile.delete(); try { fileoutputstream outputstream = new fileoutputstream(imagefile); pdfbitmap.compress(bitmap.compressformat.png, 90, outputstream); outputstream.flush(); outputstream.close(); pdfimagesaveerrorcode = "image saved"; } catch (exception e) { e.printstacktrace(); pdfimagesaveerrorcode = "image not saved"; } return pdfimagesaveerrorcode; } }
here, you're compressing image 1/10 of original quality:
bitmappdfpage.compress(bitmap.compressformat.png, 10, stream);
why not try either removing line, or adjusting compression higher value, 90?
bitmappdfpage.compress(bitmap.compressformat.png, 90, stream);
edit
if that's not case can give advice debug it.
i'm wondering whether it's loading or saving code that's causing small, blurry sizes, if you, place debug breakpoint on line reads
saveimage(bitmappdfpage, i);
then, in list of variables, you'll able find bitmappdfpage
(in android studio), , view bitmap itself. if bitmap small , blurry here, maybe update question knowledge it's loading code incorrect. if it's large , can read text converse true , know it's bitmap saving code that's @ fault.
Comments
Post a Comment