简体   繁体   English

使用itextsharp生成html到pdf

[英]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 am using the above code for pdf generation from html text in HTMLEditor(ajax control). 我正在使用上面的代码从HTMLEditor(ajax控件)中的html文本生成pdf。 If i hardcode a table with each column of different width, into HTMLEditor text than while generating pdf the column get devided equally ie all column have fixed size on pdf even if i specify some custom width for each column. 如果我用不同宽度的每一列硬编码一个表,进入HTMLEditor文本而不是生成pdf时,列相同,即使我为每列指定一些自定义宽度,所有列在pdf上都有固定大小。

I want to generate pdf that can convert html to pdf,also divide table column with specified width. 我想生成可以将html转换为pdf的pdf,也可以将表格列除以指定的宽度。 How to do it? 怎么做?

i don't think HTMLWorker (iTextSharp) supports table widths yet. 我认为HTMLWorker(iTextSharp)还不支持表格宽度。

so you need to: 所以你需要:

  1. parse your HTML to find the columns widths - use a regular expression or something like Html Agility Pack . 解析HTML以查找列宽 - 使用正则表达式或类似Html Agility Pack

  2. call HTMLWorker.ParseToList() to iterate over iText elements and look for PdfPTable(s) 调用HTMLWorker.ParseToList()迭代iText元素并查找PdfPTable(s)

  3. manually set the PdfPTable width by calling SetWidthPercentage() 通过调用SetWidthPercentage()手动设置PdfPTable宽度

here's an example (excluding step 1) using a HTTP handler: 这是使用HTTP处理程序的示例(不包括步骤1):

<%@ 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; }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM