positioning text using itextSharp in C3 -
i trying generate pdf on button click. having challenge design page. if can on, how position text in particular position.
say want address in top right corner , heading in center.
here code working on:
private void pdf_btn_click(object sender, eventargs e) { savefiledialog svg = new savefiledialog(); svg.showdialog(); using (filestream stream = new filestream(svg.filename + ".pdf", filemode.create)) { // create document object var document = new document(pagesize.a4, 50, 50, 25, 25); // create new pdfwrite object, writing output memorystream // var output = new memorystream(); var writer = pdfwriter.getinstance(document, stream); document.open(); // first, create our fonts... (for more on working w/fonts in itextsharp, see: http://www.mikesdotnetting.com/article/81/itextsharp-working-with-fonts var titlefont = fontfactory.getfont("arial", 18, convert.toint32(font.bold)); var addressfont = fontfactory.getfont("arial", 7, convert.toint32(font.bold)); var boldtablefont = fontfactory.getfont("arial", 12, convert.toint32(font.bold)); var endingmessagefont = fontfactory.getfont("arial", 10, convert.toint32(font.italic)); var bodyfont = fontfactory.getfont("arial", 12); // add "" title document.add(new paragraph("certificate of analysis", titlefont)); // add address var text = document.add(new paragraph("abc company", addressfont)); document.add(new paragraph("tel:(xx) xxx-xxxx, (xxx) xxx-xxx", addressfont)); document.add(new paragraph("fax: (xxx) xxx-xxx, , addressfont)); document.close(); stream.close(); } }
there different ways achieve want.
currently, telling itext layout. if want center text "certificate of analysis"
, change alignment of paragraph
this:
paragraph header = new paragraph("certificate of analysis", titlefont); header.alignment = element.align_center; document.add(header);
if want text "abc company"
right-aligned, can change alignment this:
paragraph company = new paragraph("abc company", addressfont); company.alignment = element.align_right; document.add(company);
of course: when add company
, start on new line under header
. maybe want text on same line.
in case, why not use pdfptable
?
pdfptable mytable = new pdfptable(3); mytable.widthpercentage = 100; pdfpcell mydate = new pdfpcell(new phrase("april 22, 2015", myfont)); mydate.border = rectangle.no_border; mytable.addcell(mydate); pdfpcell header = new pdfpcell(new phrase("certificate of analysis", titlefont)); header.border = rectangle.no_border; header.horizontalalignment = element.align_center; mytable.addcell(header); pdfpcell address = new pdfpcell(new phrase("company address", addressfont)); address.border = rectangle.no_border; address.horizontalalignment = element.align_right; mytable.addcell(address); doc.add(mytable);
now available width of page divided in three, date aligned left, header centered, , address aligned right. obviously, can add more data pdfpcell
, more rows pdfptable
. simple proof of concept.
another option, add text @ absolute position (using coordinates). can done using columntext
object. there plenty of examples on in the best itext questions on stackoverflow
Comments
Post a Comment