asp.net - iTextSharp to print GridView - output header width -
i'm using itextsharp print gridview, facing problem when trying determine appropriate width of columns. in gridview don't set it, rather let asp.net engine resize widths (it dynamically based on text's style).
by default, columns of same size bad in cases.
how can configure header's pdfpcell object width suitable text in it?
column widths calculated @ pdfptable
level you'll need address there. depending on parser using there's couple of ways start end result need parse html itextsharp objects , manually apply own logic before committing them pdf. code below based on using (preferred) xmlworker , itextsharp 5.4.0 following sample html:
<html> <head> <title>this test</title> </head> <body> <table> <thead> <tr> <td>first name</td> <td>last name</td> </tr> </thead> <tbody> <tr> <td>bob</td> <td>dole</td> </tr> </tbody> </table> </body> </html>
first, you'll need implement own ielementhandler
class. allow capture elements before writing them pdf.
public class customelementhandler implements ielementhandler ''//list of elements public elements new list(of ielement) ''//will called each top-level elements public sub add(w iwritable) implements ielementhandler.add ''//sanity check if (typeof w writableelement) ''//add element (which might have sub-elements) elements.addrange(directcast(w, writableelement).elements) end if end sub end class
then need use handler above instead of writing directly document:
''//todo: populate html dim html = "" ''//where we're write our pdf dim file1 = path.combine(environment.getfolderpath(environment.specialfolder.desktop), "file1.pdf") ''//create our pdf, nothing special here using fs new filestream(file1, filemode.create, fileaccess.write, fileshare.none) using doc new document() using writer = pdfwriter.getinstance(doc, fs) doc.open() ''//create instance of our handler dim handler new customelementhandler() ''//bind stringreader our text using sr new stringreader(html) ''//have xmlworker read our html , push our handler xmlworkerhelper.getinstance().parsexhtml(handler, sr) end using ''//loop through each element parser found each el in handler.elements ''//if element table if typeof el pdfptable ''//below illustration, change needed ''//set absolute width of table directcast(el, pdfptable).totalwidth = 500 ''//set first column 25% , second column 75% directcast(el, pdfptable).setwidths({0.25, 0.75}) ''//also, illustration, turn borders on each cell can see actual widths each r in directcast(el, pdfptable).rows each c in r.getcells() c.borderwidthleft = 1 c.borderwidthright = 1 c.borderwidthbottom = 1 c.borderwidthtop = 1 next next end if ''//add element document doc.add(el) next doc.close() end using end using end using
Comments
Post a Comment