我使用以下代码将编辑器(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> </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;
如果我从上面的代码中删除以下table
,则 pdf 会成功生成。但是一旦我在我的 HTMl 代码中包含该 table
,我就会收到以下错误
如何解决我的问题。
最佳答案
当前版本的 iTextSharp 不支持使用“px”指定的单元格宽度。它确实支持百分比或纯数字宽度单元格宽度。
当您尝试对 HTML 内容进行硬编码时,有两个地方有“px”:
-
<td width='750px'>
-
<td width='75px'>
删除“px”就可以了。
关于asp.net - 在 asp.net 中使用 itextsharp 将 HTML 文本转换为 pdf 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5856484/