c# - Table border does not automatically fit into contents -
i have invoice being constructed via itextsharp (4.1.2).
my problem lies on part of invoice wherein display terms , conditions , payment terms. following image below shows problem.
however, when dynamic rows not present (i mean no selected discounts, no shipping costs , no tax, subtotal , grand total), appearance similar image. bottom border falls down far bottom instead of within cell.
constraints:
- upgrading not yet option there printing functionalities require itextsharp 4.1.2.
- using html pdf not yet option because of time constraints.
i hope can me this.
edit: revised question reflect output , code replication.
protected void btnviewpdf_click(object sender, eventargs e) { // create document in memory default settings. var document = new document(); string path = server.mappath("pdfs"); // commit document disk. pdfwriter.getinstance(document, new filestream(path + "/paymentdetails.pdf", filemode.create)); // open th document. document.open(); #region pdf document logic here... // create pdfptable contain 2 columns. used layout. pdfptable outertable = new pdfptable(2); outertable.totalwidth = 510f; outertable.lockedwidth = true; outertable.defaultcell.border = rectangle.box; #region first column // add pdfptable in first column. pdfptable outertablefirstcolumn = new pdfptable(1); outertablefirstcolumn.totalwidth = 450f; outertablefirstcolumn.defaultcell.bordercolor = color.black; // add cells first column. pdfpcell outertablefirstcolumnheader = new pdfpcell() { border = rectangle.box, bordercolor = color.black, phrase = new phrase("terms , conditions", fontfactory.getfont("consolas", 8, (int) font.bold, color.black)), padding = 5f }; outertablefirstcolumn.addcell(outertablefirstcolumnheader); string mytermsandconditions = "acceptance , cancellation of orders. invoice accepted customer may not cancelled customer , constitutes binding agreement between customer , company. in event of cancellation or other withdrawal of order customer reason , without limiting other remedy company may have result of such cancellation or other withdrawal, reasonable cancellation charges, shall include expenses incurred , commitments made company, shall paid customer company."; pdfpcell outertablefirstcolumncontent = new pdfpcell() { border = rectangle.box, bordercolor = color.black, phrase = new phrase(mytermsandconditions, fontfactory.getfont("consolas", 8, (int)font.normal, color.black)), padding = 5f }; outertablefirstcolumn.addcell(outertablefirstcolumncontent); #endregion #region second column // add pdfptable in second column. pdfptable outertablesecondcolumn = new pdfptable(1); outertablesecondcolumn.totalwidth = 70f; outertablesecondcolumn.defaultcell.border = rectangle.box; pdfptable outertablesecondcolumncontent = new pdfptable(3); outertablesecondcolumncontent.setwidths(new float[] { 3f, 0.5f, 1.5f }); outertablesecondcolumncontent.defaultcell.border = rectangle.box; outertablesecondcolumncontent.addcell(generatepaymentdetailcell("subtotal:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("$")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("999.99")); #region conditional adding of cells // initialize details can controlled produce results. var subtotal = 999.99m; var selecteddiscounttype = discounttype.nodiscount; var flatdiscountamount = 30m; var discountrate = 23.18m; var discountpercentamount = 0.00m; var selectedtaxtype = taxtype.notax; var flattaxamount = 20.00m; var taxrate = 12.25m; var taxpercentamount = 0.00m; var includeshipping = false; var shippingcost = 20.50m; if (selecteddiscounttype != 0) { switch (selecteddiscounttype) { case discounttype.flatdiscount: { // discount amount outertablesecondcolumncontent.addcell(generatepaymentdetailcell("flat discount:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("$")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(decimal.parse(flatdiscountamount.tostring()).tostring("n"))); } break; case discounttype.percentdiscount: { flatdiscountamount = 0; // discount rate outertablesecondcolumncontent.addcell(generatepaymentdetailcell("% discount:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(" ")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(string.format(discountrate.tostring(), "{0:0.0}"))); discountpercentamount = subtotal * (discountrate / 100); // total discount outertablesecondcolumncontent.addcell(generatepaymentdetailcell("total discount:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("$")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(decimal.parse(discountpercentamount.tostring()).tostring("n"))); } break; default: break; } } if (includeshipping) { // shipping cost outertablesecondcolumncontent.addcell(generatepaymentdetailcell("shipping cost:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("$")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(decimal.parse(shippingcost.tostring()).tostring("n"))); } if (selectedtaxtype != 0) { switch (selectedtaxtype) { case taxtype.flattax: { // tax amount outertablesecondcolumncontent.addcell(generatepaymentdetailcell("flat tax:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("$")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(decimal.parse(flattaxamount.tostring()).tostring("n"))); } break; case taxtype.percenttax: { flattaxamount = 0; // tax percent outertablesecondcolumncontent.addcell(generatepaymentdetailcell("% tax:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(" ")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(string.format(taxrate.tostring(), "{0:0.0}"))); taxpercentamount = (subtotal - (flatdiscountamount + discountpercentamount) + (shippingcost)) * (taxrate / 100); // total tax outertablesecondcolumncontent.addcell(generatepaymentdetailcell("total tax:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("$")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell(decimal.parse(taxpercentamount.tostring()).tostring("n"))); } break; default: break; } } #endregion var grandtotalcost = (subtotal - (flatdiscountamount + discountpercentamount) + shippingcost) + (flattaxamount + taxpercentamount); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("grand total:")); outertablesecondcolumncontent.addcell(generatepaymentdetailcell("$")); outertablesecondcolumncontent.addcell(generategrandtotalcostcell(decimal.parse(grandtotalcost.tostring()).tostring("n"))); outertablesecondcolumn.addcell(outertablesecondcolumncontent); #endregion // add columns table. outertable.addcell(outertablefirstcolumn); outertable.addcell(outertablesecondcolumn); // add table document. document.add(outertable); #endregion // close document. document.close(); } public pdfpcell generatepaymentdetailcell(string content) { pdfpcell cell = new pdfpcell() { phrase = new phrase(content, fontfactory.getfont("consolas", 8, font.bold, color.black)), horizontalalignment = element.align_right, verticalalignment = element.align_top, border = rectangle.box }; return cell; } public pdfpcell generategrandtotalcostcell(string grandtotalcost) { pdfpcell cell = new pdfpcell() { phrase = new phrase(grandtotalcost, fontfactory.getfont("consolas", 8, font.bold, color.black)), horizontalalignment = element.align_right, verticalalignment = element.align_top, border = 1, borderwidthtop = 1, borderwidthbottom = 2 }; return cell; } enum discounttype { flatdiscount = 100, percentdiscount = 200, nodiscount = 300 } enum taxtype { flattax = 100, percenttax = 200, notax = 300 }
got answer replication. adding blank row (in form of addcells()) in outertablesecondcolumncontent, cell shrinks down fit contents.
Comments
Post a Comment