c# - Generating html to pdf using itextsharp -


public void pdfgenforffd(textbox textbox3, hiddenfield hiddenfield1, hiddenfield hiddenfield4, ajaxcontroltoolkit.htmleditor.editor editor1) {      httpcontext.current.response.clear();     httpcontext.current.response.contenttype = "application/pdf";     // create pdf document     document pdfdocument = new document(pagesize.a4, 50, 25, 15, 10);      pdfwriter wri = pdfwriter.getinstance(pdfdocument, new filestream("d://" + hiddenfield1.value + "_" + hiddenfield4.value + ".pdf", filemode.create));      pdfwriter.getinstance(pdfdocument, httpcontext.current.response.outputstream);      pdfdocument.open();     string htmltext = editor1.content;     //string htmltext = htmltext1.replace(environment.newline, "<br/>");      htmlworker htmlworker = new htmlworker(pdfdocument);      htmlworker.parse(new stringreader(htmltext));       pdfdocument.close();     httpcontext.current.response.end(); } 

i using above code pdf generation html text in htmleditor(ajax control). if hardcode table each column of different width, htmleditor text while generating pdf column devided equally i.e column have fixed size on pdf if specify custom width each column.

i want generate pdf can convert html pdf,also divide table column specified width. how it?

i don't think htmlworker (itextsharp) supports table widths yet.

so need to:

  1. parse html find columns widths - use regular expression or html agility pack.

  2. call htmlworker.parsetolist() iterate on itext elements , pdfptable(s)

  3. manually set pdfptable width calling setwidthpercentage()

here's example (excluding step 1) using http handler:

<%@ webhandler language='c#' class='tablecolumnwidths' %> using system; using system.collections.generic; using system.io; using system.web; using itextsharp.text; using itextsharp.text.html.simpleparser; using itextsharp.text.pdf;  public class tablecolumnwidths : ihttphandler {   public void processrequest (httpcontext context) {     context.response.contenttype = "application/pdf";     string html = @" <html><head></head><body>  <p>a paragraph</p>    <table border='1'> <tr><td>row1-column1</td><td>row1-column2</td><td>row1-column3</td></tr> <tr><td>row2-column1</td><td>row2-column2</td><td>row2-column3</td></tr> </table> </body></html>     "; /*  * need rectangle later when set column widths  */     rectangle rect = pagesize.letter;     document document = new document(rect);     pdfwriter.getinstance(document, context.response.outputstream);     document.open(); /*   * iterate on itext elements  */     list<ielement> ie = htmlworker.parsetolist(       new stringreader(html), null     ); /*  * page width  */     float pagewidth = rect.width; /*  * pdfptable(s)  */     foreach (ielement element in ie) {       pdfptable table = element pdfptable; /*  * set column widths  */       if (table != null) {         table.setwidthpercentage(           new float[] {             (float).25 * pagewidth,              (float).50 * pagewidth,              (float).25 * pagewidth           },           rect         );       }       document.add(element);      }      document.close();     }   public bool isreusable {     { return false; }   } } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -