In previous tutorial PDF Creator using iTextG Library we discussed hoe to create PDF using iTextG library.
In this tutorial we will see how to design a PDF in Table format
Before going to this tutorial make sure how to create a PDF using iTextG Library PDF Creator using iTextG Library
The following sample will give you clear picture to create PDF in Table format
Create a New project
Add dependencies and permissions for Writing Local Storage
Add the to your apps Gradle file:
build.gradle
To create a Pdf file, our app will need the some permissions, open your project’s AndroidManifest.xml and add the following permission.
AndroidManifest.xml
In this tutorial we will see how to design a PDF in Table format
Before going to this tutorial make sure how to create a PDF using iTextG Library PDF Creator using iTextG Library
The following sample will give you clear picture to create PDF in Table format
Create a New project
Add dependencies and permissions for Writing Local Storage
Add the to your apps Gradle file:
build.gradle
compile 'com.itextpdf:itextg:5.5.10'
To create a Pdf file, our app will need the some permissions, open your project’s AndroidManifest.xml and add the following permission.
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <Button android:id="@+id/button_create" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:text="Create PDF"/> </LinearLayout>
Design looks like
PDFCreateActivity.java
public class MainActivity extends AppCompatActivity { private static final String TAG = "PdfCreatorActivity"; private Button mCreateButton; private File pdfFile; final private int REQUEST_CODE_ASK_PERMISSIONS = 111; Context context; int total=0; Font mainheadone = new Font(Font.FontFamily.TIMES_ROMAN, 15.0f, Font.BOLD); Font mainhead = new Font(Font.FontFamily.TIMES_ROMAN, 12.0f, Font.BOLD); Font subhead = new Font(Font.FontFamily.TIMES_ROMAN, 11.0f, Font.NORMAL); Font listvalues = new Font(Font.FontFamily.TIMES_ROMAN, 10.0f, Font.NORMAL); ArrayList<HashMap<String,String>> listArray; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; mCreateButton = (Button) findViewById(R.id.button_create); listArray = new ArrayList<>(); /*Hardcoded list to create list in PDF*/ for(int i=0; i<2;i++){ HashMap<String,String> map = new HashMap<>(); switch(i){ case 0: map.put("sl",""+1); map.put("item","Veg Sandwich"); map.put("rate","45"); map.put("qty","2"); map.put("amt",""+90); break; case 1: map.put("sl",""+2); map.put("item","Veg Pizza"); map.put("rate","140"); map.put("qty","1"); map.put("amt",""+140); break; case 3: map.put("sl",""+3); map.put("item","Bovonto-1l"); map.put("rate","60"); map.put("qty","1"); map.put("amt",""+60); break; } listArray.add(map); } mCreateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { createPdfWrapper(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }); } private void createPdfWrapper() throws FileNotFoundException,DocumentException{ //refer the codes to create wrapper in previous tutorial if (hasWriteStoragePermission != PackageManager.PERMISSION_GRANTED) { }else{ createPdf(); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { //refer the codes to create wrapper in previous tutorial } private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { //refer the codes to create wrapper in previous tutorial } private void createPdf() throws FileNotFoundException, DocumentException { File docsFolder = new File(Environment.getExternalStorageDirectory() + "/Documents"); if (!docsFolder.exists()) { docsFolder.mkdir(); Log.i(TAG, "Created a new directory for PDF"); } pdfFile = new File(docsFolder.getAbsolutePath(),"BillPayment.pdf"); OutputStream output = new FileOutputStream(pdfFile); Document document = new Document(); document.setMargins(10, 10, 12, 12); PdfWriter.getInstance(document, output); document.open(); try{ /*Table_Heading*/ PdfPTable headtab = new PdfPTable(1); headtab.setWidthPercentage(100); headtab.setHorizontalAlignment(Paragraph.ALIGN_CENTER); PdfPCell headcell; headcell = new PdfPCell(new Paragraph("Bill Payment", mainheadone)); headcell.setHorizontalAlignment(Paragraph.ALIGN_CENTER); headcell.setBorderWidthLeft(1); headcell.setBorderWidthRight(1); headcell.setBorderWidthTop(1); headcell.setBorderWidthBottom(1); headcell.setPadding(5); headtab.addCell(headcell); document.add(headtab); PdfPTable patab = new PdfPTable(12); patab.setWidthPercentage(100); PdfPCell pacell; pacell = new PdfPCell(new Paragraph("Sl.No", mainhead)); pacell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); pacell.setBorder(0); pacell.setPadding(5); pacell.setColspan(1); pacell.setBorderWidthLeft(1); pacell.setBorderWidthBottom(1); patab.addCell(pacell); pacell = new PdfPCell(new Paragraph("Item", mainhead)); pacell.setHorizontalAlignment(Paragraph.ALIGN_CENTER); pacell.setBorder(0); pacell.setPadding(5); pacell.setColspan(5); pacell.setBorderWidthLeft(1); pacell.setBorderWidthBottom(1); patab.addCell(pacell); pacell = new PdfPCell(new Paragraph("Rate(Rs)", mainhead)); pacell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); pacell.setBorder(0); pacell.setPadding(5); pacell.setColspan(2); pacell.setBorderWidthLeft(1); pacell.setBorderWidthBottom(1); patab.addCell(pacell); pacell = new PdfPCell(new Paragraph("Qty", mainhead)); pacell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); pacell.setBorder(0); pacell.setPadding(5); pacell.setColspan(2); pacell.setBorderWidthLeft(1); pacell.setBorderWidthBottom(1); patab.addCell(pacell); pacell = new PdfPCell(new Paragraph("Amt(Rs)", mainhead));//₹-"\u20B9" pacell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); pacell.setBorder(0); pacell.setPadding(5); pacell.setBorderWidthLeft(1); pacell.setBorderWidthRight(1); pacell.setBorderWidthBottom(1); pacell.setColspan(2); patab.addCell(pacell); document.add(patab); /*Set the item details*/ PdfPTable contab = new PdfPTable(12); contab.setWidthPercentage(100); PdfPCell concell; for(int i=0; listArray.size()>i; i++){ if(i==0){ total=0; } HashMap map = listArray.get(i); String sl = map.get("sl").toString(); String item = map.get("item").toString(); String rate = map.get("rate").toString(); String qty = map.get("qty").toString(); String amt = map.get("amt").toString(); int intAmt = Integer.parseInt(amt); total += intAmt; concell = new PdfPCell(new Paragraph("" + sl, listvalues)); concell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); concell.setPadding(5); concell.setBorder(0); concell.setColspan(1); concell.setBorderWidthLeft(1); concell.setBorderWidthRight(1); contab.addCell(concell); concell = new PdfPCell(new Paragraph("" + item, listvalues)); concell.setHorizontalAlignment(Paragraph.ALIGN_CENTER); concell.setBorder(0); concell.setPadding(5); concell.setColspan(5); concell.setBorderWidthRight(1); contab.addCell(concell); concell = new PdfPCell(new Paragraph("" + rate, listvalues)); concell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); concell.setBorder(0); concell.setPadding(5); concell.setColspan(2); concell.setBorderWidthRight(1); contab.addCell(concell); concell = new PdfPCell(new Paragraph("" + qty, listvalues)); concell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); concell.setBorder(0); concell.setPadding(5); concell.setColspan(2); concell.setBorderWidthRight(1); contab.addCell(concell); concell = new PdfPCell(new Paragraph("" + amt, listvalues)); concell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); concell.setBorder(0); concell.setPadding(5); concell.setColspan(2); concell.setBorderWidthRight(1); contab.addCell(concell); } document.add(contab); /*total_amount*/ PdfPTable tottab = new PdfPTable(12); tottab.setWidthPercentage(100); PdfPCell totacell; totacell = new PdfPCell(new Paragraph("Total Amount : ", mainhead)); totacell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); totacell.setBorder(0); totacell.setPadding(5); totacell.setColspan(10); totacell.setBorderWidthLeft(1); totacell.setBorderWidthTop(1); totacell.setBorderWidthBottom(1); tottab.addCell(totacell); totacell = new PdfPCell(new Paragraph(""+total, mainhead)); totacell.setHorizontalAlignment(Paragraph.ALIGN_RIGHT); totacell.setBorder(0); totacell.setPadding(5); totacell.setColspan(2); totacell.setBorderWidthRight(1); totacell.setBorderWidthTop(1); totacell.setBorderWidthBottom(1); tottab.addCell(totacell); document.add(tottab); }catch (DocumentException de) { Log.e("PDFCreator", "DocumentException:" + de); } finally { document.close(); } previewPdf(); } private void previewPdf() { //refer the codes to create wrapper in previous tutorial } }
For missing coding refer previous tutorial PDF Creator using iTextG Library
Now run the coding and click the button to create a PDF and view using a PDF viewer, output as
___________________________________________________________Sourcecode PDFwithTableDesign.zip
Happy Coding...
Hello bro, why if i change dynamic data? line 10 record or higher, becauce i try error null. thanks bro :)
ReplyDelete