Print multiple PDF pages using Android 4.4 printing framework -
i want ask help. want print/create pdf document. can create 1 pdf page , write data page via canvas. problem dont know how can create pdf page , continue writing second page. if has experiences full spend many time this.
i use this: https://developer.android.com/reference/android/print/pdf/printedpdfdocument.html
part of code there:
private void doprint(int _docnumber){ docnumber = _docnumber; //get printmanager instance printmanager printmanager = (printmanager)this.getsystemservice(context.print_service); // set job name, displayed in print queue string jobname = getstring(r.string.app_name) + " dokument"; // start print job, passing in printdocumentadapter implementation // handle generation of print document printmanager.print(jobname, new myprintdocadapter(), null); toast.maketext(getbasecontext(), "príprava na tlač...", toast.length_short).show(); } public class myprintdocadapter extends printdocumentadapter { context context; private int pageheight; private int pagewidth; public pdfdocument mypdfdocument; @override public void onlayout(printattributes oldattributes, printattributes newattributes, cancellationsignal cancellationsignal, layoutresultcallback callback, bundle extras) { mypdfdocument = new printedpdfdocument(context, newattributes); pageheight = newattributes.getmediasize().getheightmils()/1000 * 72; pagewidth = newattributes.getmediasize().getwidthmils()/1000 * 72; if (cancellationsignal.iscanceled() ) { callback.onlayoutcancelled(); return; } if (totalpages > 0) { printdocumentinfo.builder builder = new printdocumentinfo .builder("dokument_"+ docnumber + ".pdf") .setcontenttype(printdocumentinfo.content_type_document) .setpagecount(totalpages); printdocumentinfo info = builder.build(); callback.onlayoutfinished(info, true); } else { callback.onlayoutfailed("počet strán je nula."); } } @override public void onwrite(final pagerange[] pageranges, final parcelfiledescriptor destination, final cancellationsignal cancellationsignal, final writeresultcallback callback) { (int = 0; < totalpages; i++) { if (pageinrange(pageranges, i)) { pdfdocument.pageinfo newpage = new pdfdocument.pageinfo.builder(pagewidth, pageheight, i).create(); pdfdocument.page page = mypdfdocument.startpage(newpage); if (cancellationsignal.iscanceled()) { callback.onwritecancelled(); mypdfdocument.close(); mypdfdocument = null; return; } switch (docnumber) { case 1: drawobjednavka(page, i); break; case 2: drawvykaz(page, i); break; default: toast.maketext(mainactivity.this, "bad file format",toast.length_short).show(); } mypdfdocument.finishpage(page); } } try { mypdfdocument.writeto(new fileoutputstream(destination.getfiledescriptor())); } catch (ioexception e) { callback.onwritefailed(e.tostring()); return; } { mypdfdocument.close(); mypdfdocument = null; } callback.onwritefinished(pageranges); } private boolean pageinrange(pagerange[] pageranges, int page) { (int = 0; i<pageranges.length; i++) { if ((page >= pageranges[i].getstart()) && (page <= pageranges[i].getend())) return true; } return false; } private void drawobjednavka(pdfdocument.page page, int pagenumber) { int verticalposy = 0; canvas = page.getcanvas(); pagenumber++; // make sure page numbers start @ 1 typeface tf = typeface.create(typeface.default, typeface.normal); // there can change type of font family, if needed pf pdfdocument.pageinfo pageinfo = page.getinfo(); paint paint = new paint(); paint.settypeface(tf); paint.setcolor(color.black); paint.setstrokewidth(1f); //set line thickness paint.settextsize(20); paint.settypeface(typeface.default_bold); canvas.drawtext("objednávka", default_left_margin_x, 30, paint);...
since painting directly canvas, can calculate totalpages
need. when new media size selected user, should redraw pages according page dimensions , how want display page.
your code ok, need calculate totalpages
according media size selected user. have pagewidth
, pageheight
, , can margins via newattributes.getminmargins()
. values need in newattributes
.
Comments
Post a Comment