Android How to turn a XML layout into a PDF? -
i have form when filled out sent dedicated email address on submission. have opportunity offer other options. saving pdf file. form completed in main.xml layout. possible turn xml layouts pdf's? if point me in direction of example.
many thanks
you can take on link:
https://github.com/hendrixstring/android-pdfmyxml
instructions
- create xml layouts
first create xml layouts. give dimensions in pixels (and it's sub views) , proportions according landscape or portrait according ratio 1:1.41.
page1.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="2115px" android:layout_height="1500px" android:background="@color/white"> <textview android:id="@+id/tv_hello" android:textcolor="@color/black" android:textsize="27px" android:textstyle="bold" android:padding="6px"/> </relativelayout> you can create many pages/templates need.
- implement view renderer
implement view renderer extending abstractviewrenderer or anonymously instantiating , injecting layout id. initview(view view) supply inflated view automatically. there other options wont cover now.
abstractviewrenderer page = new abstractviewrenderer(context, r.layout.page1) { private string _text; public void settext(string text) { _text = text; } @override protected void initview(view view) { textview tv_hello = (textview)view.findviewbyid(r.id.tv_hello); tv_hello.settext(_text); } }; // can reuse bitmap if want page.setreusebitmap(true); - build pdf document
use pdfdocument or pdfdocument.builder add pages , render , run @ background progress bar.
pdfdocument doc = new pdfdocument(ctx); // add many pages have doc.addpage(page); doc.setrenderwidth(2115); doc.setrenderheight(1500); doc.setorientation(pdfdocument.a4_mode.landscape); doc.setprogresstitle(r.string.gen_please_wait); doc.setprogressmessage(r.string.gen_pdf_file); doc.setfilename("test"); doc.setinflateonmainthread(false); doc.setlistener(new pdfdocument.callback() { @override public void oncomplete(file file) { log.i(pdfdocument.tag_pdf_my_xml, "complete"); } @override public void onerror(exception e) { log.i(pdfdocument.tag_pdf_my_xml, "error"); } }); doc.createpdf(ctx); or use pdfdocument.builder
new pdfdocument.builder(ctx).addpage(page).filename("test").orientation(pdfdocument.a4_mode.landscape) .progressmessage(r.string.gen_pdf_file).progresstitle(r.string.gen_please_wait).renderwidth(2115).renderheight(1500) .listener(new pdfdocument.callback() { @override public void oncomplete(file file) { log.i(pdfdocument.tag_pdf_my_xml, "complete"); } @override public void onerror(exception e) { log.i(pdfdocument.tag_pdf_my_xml, "error"); } }).create().createpdf(this);
Comments
Post a Comment