blank pdf page is created when im using itext in android -
screen shot m adding images in pdf file generate pdf document problem automatically adding 1st page blank page , remaining pages contain images fine. how should overcome problem.
pdfpath = dir + "/" + txtfilename.gettext().tostring() + ".pdf"; string pdffilename = txtfilename.gettext().tostring() + ".pdf"; pdfwriter writer; file file = new file(dir, pdffilename); document document = new document(); try { writer = pdfwriter.getinstance(document, new fileoutputstream(file)); headerfooterpageevent event = new headerfooterpageevent(); writer.setpageevent(event); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (documentexception e) { e.printstacktrace(); } document.open(); image image = null; (int = 0; < listofimage.size(); i++) { string imgfilename = listofimage.get(i).tostring(); try { image = image.getinstance(imgfilename); } catch (badelementexception e) { e.printstacktrace(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } float scaler = ((document.getpagesize().getwidth() - document.leftmargin() - document.rightmargin()) / image .getwidth()) * 90; log.i("scale percentage", scaler + ""); image.scalepercent(scaler); try { document.newpage(); document.add(image); document.addauthor("genetech solutions"); } catch (documentexception e) { e.printstacktrace(); } } document.close();
headerfooterpageevent
consists of:
public void onendpage(pdfwriter writer, document document) { columntext.showtextaligned(writer.getdirectcontent(), element.align_center, new phrase("generated snap scanner powered genetech solutions"), 180, 30, 0); columntext.showtextaligned(writer.getdirectcontent(), element.align_center, new phrase("page " + document.getpagenumber()), 550, 30, 0); }
i burnt down code following test case, removing try-catch
constructs keep short , inlining page event listener:
@test public void testyasirahmedkhanoriginal() throws ioexception, documentexception { list<string> listofimage = collections.ncopies(3, "src/test/resources/mkl/testarea/itext5/layer/willi-1.jpg"); // collections.singletonlist("src/test/resources/mkl/testarea/itext5/layer/willi-1.jpg"); file file = new file(result_folder, "yasirahmedkhanoriginal.pdf"); document document = new document(); pdfwriter writer = pdfwriter.getinstance(document, new fileoutputstream(file)); writer.setpageevent(new pdfpageeventhelper() { public void onendpage(pdfwriter writer, document document) { columntext.showtextaligned(writer.getdirectcontent(), element.align_center, new phrase("generated snap scanner powered genetech solutions"), 180, 30, 0); columntext.showtextaligned(writer.getdirectcontent(), element.align_center, new phrase("page " + document.getpagenumber()), 550, 30, 0); } }); document.open(); (int = 0; < listofimage.size(); i++) { string imgfilename = listofimage.get(i).tostring(); image image = image.getinstance(imgfilename); float scaler = ((document.getpagesize().getwidth() - document.leftmargin() - document.rightmargin()) / image.getwidth()) * 90; image.scalepercent(scaler); document.newpage(); document.add(image); document.addauthor("genetech solutions"); } document.close(); }
the result:
no empty first page, should be...
thus, either op additional causes empty page or uses old itext version may have had bug in respect.
Comments
Post a Comment