【发布时间】:2011-06-13 10:07:41
【问题描述】:
我正在使用以下代码使用 iTextSharp.text 将 html 写入 pdf。
但我在 pdf 文件中的 html 格式与它们在 html 中显示的不同。设计不正确。
我应该怎么做才能在 pdf 中显示正确的设计?
Document document = new Document(PageSize.A4_LANDSCAPE, 0, 0, 30, 65);
PdfWriter.GetInstance(document, new FileStream(Server.MapPath("/") + "Temp/" + "parsetest1.pdf", FileMode.Create));
document.Open();
String htmlText = "<div style='margin: 20px auto; width: 300px; font-family: Arial, Helvetica, sans-serif;'><div style='background: rgb(216, 216, 216); padding: 7px;'><div style='background: rgb(255, 255, 255); padding: 10px; width: 57px; float: left;'><img alt='barcode' src='images/abc.png'></div><div style='width: 7px; height: 7px; float: left;'></div> <div style='width: 166px; float: right;'><div style='background: rgb(255, 255, 255); padding: 10px;' align='center'><img alt='img' src='images/photpath.png'><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; margin-top: 7px;' align='center'><div style='color: rgb(157, 157, 157); font-size: 12px; float: left;'>Name</div><div style='width: 146px; text-align: center; color: rgb(0, 0, 0); clear: both; font-size: 14px; margin-top: 10px; float: left;' align='center'><strong>dalvirsaini</strong></div><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; margin-top: 7px;' align='center'><div style='color: rgb(157, 157, 157); font-size: 12px; float: left;'>Description</div><div style='width: 146px; text-align: center; color: rgb(0, 0, 0); clear: both; font-size: 14px; margin-top: 10px; float: left;' align='center'><strong>Payment Status</strong></div><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; height: 89px; margin-top: 7px;' align='center'><img alt='ScanCode' src='images/abc2.png'><div style='clear: both;'></div></div><div style='clear: both;'></div></div><div style='clear: both;'></div></div><div style='clear: both;'></div></div>";
StringReader abc = new StringReader(htmlText);
List<iTextSharp.text.IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(abc, null);
foreach (object item in elements)
{
document.Add((IElement)item);
}
document.Close();
【问题讨论】:
-
您的 HTML 不是有效的全格式 HTML - 有效的 HTML 必须以 HTML 标记开头。尝试使用简单、有效的 HTML 作为初学者。
-
你找到解决方法了吗?
标签: asp.net html itextsharp