首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用itextsharp生成html到pdf

使用itextsharp生成html到pdf
EN

Stack Overflow用户
提问于 2011-02-18 13:41:43
回答 1查看 26.3K关注 0票数 3
代码语言:javascript
复制
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。如果我硬编码一个表格,每个列的宽度都不同于生成pdf时的HTMLEditor文本,那么列将被均匀地划分,即所有列在pdf上都有固定的大小,即使我为每列指定了一些自定义的宽度。

我要生成的pdf,可以转换为pdf的html,也划分表列与指定的宽度。该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-18 20:43:17

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

因此,您需要:

使用正则表达式或类似于Html Agility Pack.

  • call

  • 的方法遍历HTMLWorker.ParseToList()元素并查找iText,通过调用PdfPTable(s)

  • manually设置iText宽度设置PdfPTable宽度

下面是一个使用HTTP处理程序的示例(不包括步骤1):

代码语言:javascript
复制
<%@ 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5038031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档