在 asp.net 中使用 itextsharp 将 HTML 文本转换为 pdf 文件时出错

发布于 2024-11-04 17:35:11 字数 3078 浏览 0 评论 0原文

我正在使用以下代码将编辑器(Ajax 控件)中的内容转换为 pdf,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
/// <summary>
/// Summary description for pdfgeneration
/// </summary>
public class pdfgeneration
{
    public pdfgeneration()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void pdfgenerator(String name1, AjaxControlToolkit.HTMLEditor.Editor Editor1)
    {

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/pdf";
        // Create PDF document
        Document pdfDocument = new Document(PageSize.A4, 70, 55, 40, 25);

        PdfWriter wri = PdfWriter.GetInstance(pdfDocument, new FileStream("e://" +name1 + ".pdf", FileMode.Create));

        PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);

        pdfDocument.Open();
        string htmlText = Editor1.Content;
        System.Collections.Generic.List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);

        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            pdfDocument.Add((IElement)htmlarraylist[k]);
        }

        pdfDocument.Close();
        HttpContext.Current.Response.End();
    }


}

我最初在编辑器(Ajax 控件)中对以下 HTML 文本进行硬编码,

String editorcontent = "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>" +
                 "<br/>" + "<P align='center'><b>" + courtname + "</b></P>"
                + "<br/><P align='center'>(Before" + jname + "," + desname + ")"
                + "<br/>" + "<P align='right'><b><u>" + DropDownList1.SelectedItem + " no. " + TextBox1.Text + "/" + TextBox2.Text + "</u></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P>"
                + "<br />"
  **//this table is the problem//**    + "<table><tr><td width='750px'><p align='left'>" + petitioner + "</p>" + "</td><td>" + "<p align='right'>" + "..Applicant" + "</p>" + "</td></tr>" + "<tr><td><p align='center'>" + "V/s" + "</p>" + "</td><td></td>" + "</tr>" + "<tr><td width='75px'><p align='left'>" + respondent + "</p>" + "</td><td>" + "<p align='right'>" + "..Respondent" + "</p>" + "</td></tr></table>"
                + "<br/><P align='center'><b><u>ORDER</u></b>";
            Editor1.Content = editorcontent;

如果我从上面的代码比pdf生成成功。但是一旦我在 HTMl 代码中包含该 table ,我就会收到以下错误

在此处输入图像描述

如何解决我的问题。

I am using the following code to convert contents in Editor(Ajax control) to pdf,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
/// <summary>
/// Summary description for pdfgeneration
/// </summary>
public class pdfgeneration
{
    public pdfgeneration()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void pdfgenerator(String name1, AjaxControlToolkit.HTMLEditor.Editor Editor1)
    {

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/pdf";
        // Create PDF document
        Document pdfDocument = new Document(PageSize.A4, 70, 55, 40, 25);

        PdfWriter wri = PdfWriter.GetInstance(pdfDocument, new FileStream("e://" +name1 + ".pdf", FileMode.Create));

        PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);

        pdfDocument.Open();
        string htmlText = Editor1.Content;
        System.Collections.Generic.List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null);

        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            pdfDocument.Add((IElement)htmlarraylist[k]);
        }

        pdfDocument.Close();
        HttpContext.Current.Response.End();
    }


}

I am initially hard coding the following HTML text in the Editor(Ajax control),

String editorcontent = "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>" +
                 "<br/>" + "<P align='center'><b>" + courtname + "</b></P>"
                + "<br/><P align='center'>(Before" + jname + "," + desname + ")"
                + "<br/>" + "<P align='right'><b><u>" + DropDownList1.SelectedItem + " no. " + TextBox1.Text + "/" + TextBox2.Text + "</u></b>     </P>"
                + "<br />"
  **//this table is the problem//**    + "<table><tr><td width='750px'><p align='left'>" + petitioner + "</p>" + "</td><td>" + "<p align='right'>" + "..Applicant" + "</p>" + "</td></tr>" + "<tr><td><p align='center'>" + "V/s" + "</p>" + "</td><td></td>" + "</tr>" + "<tr><td width='75px'><p align='left'>" + respondent + "</p>" + "</td><td>" + "<p align='right'>" + "..Respondent" + "</p>" + "</td></tr></table>"
                + "<br/><P align='center'><b><u>ORDER</u></b>";
            Editor1.Content = editorcontent;

If i remove the following table from the above code than the pdf is generated successfully. but once i include that table in my HTMl code i get the following Error

enter image description here

How to resolve my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2024-11-11 17:35:11

当前版本的 iTextSharp 支持用“px”指定的单元格宽度。它确实支持百分比或纯数字宽度单元格宽度。

当您尝试对 HTML 内容进行硬编码时,有两个地方有“px”:

  1. < /code>

删除 'px' 就可以了。

the current version of iTextSharp does not support cell width specified with 'px'. it does support percentage or numeric-only width cell widths.

when you tried hard-coding the HTML content, there was 'px' in two places:

  1. <td width='750px'>
  2. <td width='75px'>

remove 'px' and you'll be OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文