Create, write and display PDF file in android app -
i have android app form accept user input. input stored in database. want create pdf file information user enters , and display user can print file or save file android note tab. best way go it. have seen itext around not render file. found code online , tested understand concept of pdf creation. uses lowagie 2.1.7
package com.example.sweetiean.androidpdfdemo; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.color; import android.net.uri; import android.os.environment; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.toast; import com.lowagie.text.document; import com.lowagie.text.documentexception; import com.lowagie.text.font; import com.lowagie.text.headerfooter; import com.lowagie.text.paragraph; import com.lowagie.text.phrase; import com.lowagie.text.pdf.pdfwriter; import com.lowagie.text.image; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; public class mainactivity extends actionbaractivity { private button createpdf , openpdf; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); createpdf = (button)findviewbyid(r.id.button1); createpdf.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub createpdf(); } }); openpdf = (button)findviewbyid(r.id.button2); openpdf.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub openpdf(); } }); } public void createpdf() { document doc = new document(); try { string path = environment.getexternalstoragedirectory().getabsolutepath() + "/pdf"; file dir = new file(path); if(!dir.exists()) dir.mkdirs(); log.d("pdfcreator", "pdf path: " + path); file file = new file(dir, "demo.pdf"); fileoutputstream fout = new fileoutputstream(file); pdfwriter.getinstance(doc, fout); //open document doc.open(); /* create paragraph , s`enter code here`et font */ paragraph p1 = new paragraph("hi! generating first pdf using droidtext"); /* create set font , size */ font parafont= new font(font.helvetica); parafont.setsize(16); p1.setalignment(paragraph.align_center); p1.setfont(parafont); //add paragraph document doc.add(p1); paragraph p2 = new paragraph("this example of simple paragraph"); /* can set font , size */ font parafont2= new font(font.courier,14.0f, color.green); p2.setalignment(paragraph.align_center); p2.setfont(parafont2); doc.add(p2); /* inserting image in pdf */ /*bytearrayoutputstream stream = new bytearrayoutputstream(); bitmap bitmap = bitmapfactory.decoderesource(getbasecontext().getresources(), r.drawable.android); bitmap.compress(bitmap.compressformat.jpeg, 100 , stream); image myimg = image.getinstance(stream.tobytearray()); myimg.setalignment(image.middle); //add image document doc.add(myimg);*/ //set footer phrase footertext = new phrase("this example of footer"); headerfooter pdffooter = new headerfooter(footertext, false); doc.setfooter(pdffooter); toast.maketext(getapplicationcontext(), "created...", toast.length_long).show(); } catch (documentexception de) { log.e("pdfcreator", "documentexception:" + de); } catch (ioexception e) { log.e("pdfcreator", "ioexception:" + e); } { doc.close(); } } void openpdf() { intent intent = new intent(intent.action_view); string path = environment.getexternalstoragedirectory().getabsolutepath() + "/pdf"; file file = new file(path, "demo.pdf"); intent.setdataandtype( uri.fromfile(file), "application/pdf" ); startactivity(intent); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
and main activity.
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/textview1" android:layout_margintop="100dp" android:text="open pdf" /> <button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview1" android:layout_margintop="44dp" android:text="generate pdf" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="50dp" android:text="@string/hello_world" />
do way:
import com.cete.dynamicpdf.*; import com.cete.dynamicpdf.pageelements.label; import android.app.activity; import android.os.bundle; import android.os.environment; import android.widget.toast; public class dynamicpdfhelloworld extends activity { private static string file = environment.getexternalstoragedirectory() + "/helloworld.pdf"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); // create document , set it's properties document objdocument = new document(); objdocument.setcreator("dynamicpdfhelloworld.java"); objdocument.setauthor("your name"); objdocument.settitle("hello world"); // create page add document page objpage = new page(pagesize.letter, pageorientation.portrait, 54.0f); // create label add page string strtext = "hello world...\nfrom dynamicpdf generator " + "for java\ndynamicpdf.com"; label objlabel = new label(strtext, 0, 0, 504, 100, font.gethelvetica(), 18, textalign.center); // add label page objpage.getelements().add(objlabel); // add page document objdocument.getpages().add(objpage); try { // outputs document file objdocument.draw(file); toast.maketext(this, "file has been written :" + file, toast.length_long).show(); } catch (exception e) { toast.maketext(this, "error, unable write file\n" + e.getmessage(), toast.length_long).show(); } } }
also check these links. fulfill requirement.
Comments
Post a Comment