c# - Why is my table not being generated on my PDF file using iTextSharp? -
i'm trying add table pdf file i'm generating. can add stuff "directly" want put various paragraphs or phrases in table cells align nicely.
the following code should add 3 paragraphs "free-form" first, same 3 in table. however, first 3 display - table seen. here's code:
try { using (var ms = new memorystream()) { using (var doc = new document(pagesize.a4, 50, 50, 25, 25)) { using (var writer = pdfwriter.getinstance(doc, ms)) { doc.open(); var titlefont = fontfactory.getfont(fontfactory.courier_bold, 11, basecolor.black); var doctitle = new paragraph("ucsc direct - direct payment form", titlefont); doc.add(doctitle); var subtitlefont = fontfactory.getfont("times roman", 9, basecolor.black); var subtitle = new paragraph("(not used reimbursement of services)", subtitlefont); doc.add(subtitle); var importantnoticefont = fontfactory.getfont("courier", 9, basecolor.red); var importantnotice = new paragraph("important: form must filled out in adobe reader or acrobat professional 8.1 or above. save completed forms, acrobat professional required. technical , accessibility assistance, contact campus controller's office.", importantnoticefont); importantnotice.leading = 0; importantnotice.multipliedleading = 0.9f; // reduce width between lines in paragraph these 2 settings importantnotice.extraparagraphspace = 4; // ? test this.... doc.add(importantnotice); // add table pdfptable table = new pdfptable(10); // arg number of columns // row 1 pdfpcell cell = new pdfpcell(doctitle); cell.colspan = 3; cell.borderwidth = 0; //cell.bordercolor = basecolor.white; <= works me, if background other white, wouldn't cell.horizontalalignment = 0; //0=left, 1=centre, 2=right table.addcell(cell); // row 2 pdfpcell cellcaveat = new pdfpcell(subtitle); cellcaveat.colspan = 2; cellcaveat.borderwidth = 0; table.addcell(cellcaveat); // row 3 pdfpcell cellimportantnote = new pdfpcell(importantnotice); cellimportantnote.colspan = 5; cellimportantnote.borderwidth = 0; table.addcell(importantnotice); doc.add(table); doc.close(); } var bytes = ms.toarray(); string pdftestoutputfilename = string.format("itextsharp_{0}.pdf", datetime.now.toshorttimestring()); pdftestoutputfilename = pdftestoutputfilename.replace(":", "_"); var testfile = path.combine(environment.getfolderpath(environment.specialfolder.desktop), pdftestoutputfilename); file.writeallbytes(testfile, bytes); messagebox.show(string.format("{0} written", pdftestoutputfilename)); } } } catch (documentexception dex) { throw (dex); } catch (ioexception ioex) { throw (ioex); } catch (exception ex) { string exmsg = ex.message; messagebox.show(string.format("boo-boo!: {0}", ex.message)); }
why borderless table invisible or nonexistant?
update
based on bruno's answer, , applying output need, works:
using (var ms = new memorystream()) { using (var doc = new document(pagesize.a4, 50, 50, 25, 25)) { //create writer that's bound our pdf abstraction , our stream using (var writer = pdfwriter.getinstance(doc, ms)) { //open document writing doc.open(); // mimic appearance of direct_payment.pdf var courierbold11font = fontfactory.getfont(fontfactory.courier_bold, 11, basecolor.black); var doctitle = new paragraph("ucsc - direct payment form", courierbold11font); doc.add(doctitle); var timesroman9font = fontfactory.getfont("times roman", 9, basecolor.black); var subtitle = new paragraph("(not used reimbursement of services)", timesroman9font); doc.add(subtitle); var courier9redfont = fontfactory.getfont("courier", 9, basecolor.red); var importantnotice = new paragraph("important: form must filled out in adobe reader or acrobat professional 8.1 or above. save completed forms, acrobat professional required. technical , accessibility assistance, contact campus controller's office.", courier9redfont); importantnotice.leading = 0; importantnotice.multipliedleading = 0.9f; // reduce width between lines in paragraph these 2 settings // add table pdfptable table = new pdfptable(1); pdfpcell cellimportantnote = new pdfpcell(importantnotice); cellimportantnote.borderwidth = pdfpcell.no_border; table.widthpercentage = 50; table.horizontalalignment = element.align_left; table.addcell(cellimportantnote); doc.add(table); doc.close(); } var bytes = ms.toarray(); string pdftestoutputfilename = string.format("itextsharp_{0}.pdf", datetime.now.toshorttimestring()); pdftestoutputfilename = pdftestoutputfilename.replace(":", "_"); var testfile = path.combine(environment.getfolderpath(environment.specialfolder.desktop), pdftestoutputfilename); file.writeallbytes(testfile, bytes); messagebox.show(string.format("{0} written", pdftestoutputfilename)); } }
please take @ simpletable7 example , compare java code [?] code:
this part relevant:
pdfpcell cellimportantnote = new pdfpcell(importantnotice); cellimportantnote.setcolspan(5); cellimportantnote.setborder(pdfpcell.no_border); table.addcell(cellimportantnote);
if convert [?] code java, you'd have:
pdfpcell cellimportantnote = new pdfpcell(importantnotice); cellimportantnote.setcolspan(5); cellimportantnote.setborder(pdfpcell.no_border); table.addcell(importantnotice);
do see difference? create cellimportantnote
instance, aren't using anywhere. instead of adding cellimportantnote
table, add importantnotice
paragraph.
this means creating table 10 columns has a single row of only 6 cells taken (because have 1 cell colspan 3, 1 cell colspan 2 , 1 cell colspan 1).
by default, itext doesn't render rows aren't complete, , since table doesn't have complete row, table isn't being rendered.
if @ resulting pdf, simple_table7.pdf, you'll notice added second table. table has 3 columns, looks identical table constructed. instead of improper use of colspan functionality, defined relative widths 3 columns:
table = new pdfptable(3); table.setwidths(new int[]{3, 2, 5}); cell.setcolspan(1); table.addcell(cell); cellcaveat.setcolspan(1); table.addcell(cellcaveat); cellimportantnote.setcolspan(1); table.addcell(cellimportantnote); document.add(table);
note avoid use meaningless numbers. instance, instead of:
cell.sethorizontalalignment(0); // 0 means left
i use:
cell.sethorizontalalignment(element.align_left);
this way, people can read line means without having depend on comments.
furtermore, replaced:
cell.setborderwidth(0);
with:
cell.setborder(pdfpcell.no_border);
that too, matter of taste, think it's important if want keep code maintainable.