c# - 使用 itextsharp 将 html 生成为 pdf

标签 c# asp.net pdf itext pdf-generation

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();
}

我正在使用上面的代码从 HTMLEditor(ajax 控件)中的 html 文本生成 pdf。如果我将每列宽度不同的表格硬编码到 HTMLEditor 文本中,而不是在生成 pdf 时,列将被平均分配,即所有列在 pdf 上都具有固定大小,即使我为每列指定了一些自定义宽度。

我想生成可以将 html 转换为 pdf 的 pdf,还可以将表格列划分为指定的宽度。怎么做?

最佳答案

我认为 HTMLWorker (iTextSharp) 还不支持表格宽度。

所以你需要:

  1. 解析您的 HTML 以查找列宽 - 使用正则表达式或类似 Html Agility Pack 的表达式.

  2. 调用 HTMLWorker.ParseToList()遍历 iText 元素并查找 PdfPTable(s)

  3. 通过调用 SetWidthPercentage() 手动设置 PdfPTable 宽度

这是一个使用 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; }
  }
}

关于c# - 使用 itextsharp 将 html 生成为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5038031/

相关文章:

pdf - 在 Windows 上将 PDF 转换为 PCL5?

c# - Windows Phone 8 网络浏览器 : session drops out on resume

c# - 插入后如何处理 Entity Framework 记录对象?

c# - 在 ASP、C# 和 CSS 中更改图像大小

jquery - asp.net 母版页头部内容中的 Javascript 似乎不起作用

asp.net - 带有FormsAuthentication的跨域Cookie

c# - C# 和 Java 中的垃圾回收

c# - 如何覆盖 TextBox 文本属性

pdf - PDF 根对象中的错误

react-native - 使用 React Native 的 Expo PDF 查看器