Creating html in 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 use the above code to generate pdf from html text in HTMLEditor (ajax control). If I hard-code the table with each column of different widths into HTMLEditor text than when generating pdf, the column will be split equally. All columns have a fixed size on pdf, even if I specify some custom width for each column.

I want to create a pdf that can convert html to pdf, also split the table column with the specified width. How to do it?

+3
source share
1 answer

I don't think HTMLWorker (iTextSharp) still supports table width.

so you need to:

( 1) HTTP:

<%@ 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 the Rectangle for later when we set the column widths
 */
    Rectangle rect = PageSize.LETTER;
    Document document = new Document(rect);
    PdfWriter.GetInstance(document, context.Response.OutputStream);
    document.Open();
/* 
 * iterate over iText elements
 */
    List<IElement> ie = HTMLWorker.ParseToList(
      new StringReader(html), null
    );
/*
 * page width
 */
    float pageWidth = rect.Width;
/*
 * look for PdfPTable(s)
 */
    foreach (IElement element in ie) {
      PdfPTable table = element as PdfPTable;
/*
 * set the 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 {
    get { return false; }
  }
}
+8

Source: https://habr.com/ru/post/1792972/


All Articles