java - Create a PDF according to a given format using the iText library -
i'm working on small project in java, there want fetch contents database , write them pdf file.
i tried googling , came itext library.
can guide create pdf looks enclosed image
ps: i'm pretty new java.and it's first java project.
i've done quick implementation of of use-case.
here's code:
first define small class acts single record in invoice.
static class article{ int sno; string description; int quantity; double unitprice; public article(int sno, string description, int quantity, double unitprice) { this.sno = sno; this.description = description; this.quantity = quantity; this.unitprice = unitprice; } }
then i've created method each of big blocks in invoice.
starting title:
public static void addtitle(document layoutdocument) { layoutdocument.add(new paragraph("retail invoice").setbold().setunderline().settextalignment(textalignment.center)); }
then adding little paragraph of text that's underneath title:
public static void addcustomerreference(document layoutdocument) { layoutdocument.add(new paragraph("m/s indian convent school").settextalignment(textalignment.left).setmultipliedleading(0.2f)); layoutdocument.add(new paragraph("y pocket-3, sector-24, rohini delhi-110085").setmultipliedleading(.2f)); layoutdocument.add(new paragraph("b 011-64660271").setmultipliedleading(.2f)); }
and adding table:
public void addtable(document layoutdocument, list<article> articlelist) { table table = new table(unitvalue.createpointarray(new float[]{60f, 180f, 50f, 80f, 110f})); // headers table.addcell(new paragraph("s.n.o.").setbold()); table.addcell(new paragraph("particulars").setbold()); table.addcell(new paragraph("qty").setbold()); table.addcell(new paragraph("rate").setbold()); table.addcell(new paragraph("amount in rs.").setbold()); // items for(article : articlelist) { table.addcell(new paragraph(a.sno+"")); table.addcell(new paragraph(a.description)); table.addcell(new paragraph(a.quantity+"")); table.addcell(new paragraph(a.unitprice+"")); table.addcell(new paragraph((a.quantity * a.unitprice)+"")); } layoutdocument.add(table); }
the main method looks this:
public static void main(string[] args) throws filenotfoundexception { pdfdocument pdfdocument = new pdfdocument(new pdfwriter("myfirstinvoice.pdf")); document layoutdocument = new document(pdfdocument); // title addtitle(layoutdocument); // customer reference information addcustomerreference(layoutdocument); addtable(layoutdocument, arrays.aslist( new article(1, "envelopes",2000, 1.70), new article(2, "voucher book", 50, 41))); // articles layoutdocument.close(); }
Comments
Post a Comment