我正在阅读一个文本文件并尝试将其解析为html并使用iTextSharp
制作一个pdf文件。
我在这里加载文本文件:
string HTML = System.IO.File.ReadAllText(Server.MapPath("~/Misc/Html.txt"));
HTML = HTML.Replace("[Date]", Paristan.Broker.UI.Components.PersianDate.GetDate(DateTime.Now));
HTML = HTML.Replace("[Title]", person.Title);
HTML = HTML.Replace("[Person]", person.Name);
HTML.txt
包含以下内容:
<body id='prt-body'><div class='prt-container'><header id='prt-header'><div id='prt-header-logo'><img src='#'></div><ul id='prt-header-information'><li><span>تاریخ: </span><label>[Date]</label>...
在这里,我尝试将其解析为html并以pdf格式打印:
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/Files/Pdf/test.pdf"), FileMode.Create));
document.Open();
try
{
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(HTML), null);
for (int k = 0; k < htmlarraylist.Count; k++)
{
document.Add((IElement)htmlarraylist[k]);
}
document.Close();
}
catch
{
document.Close();
}
我也尝试了这个:
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/Files/Pdf/test.pdf"), FileMode.Create));
document.Open();
try
{
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.StartDocument();
hw.Parse(new StringReader(HTML));
hw.EndDocument();
hw.Close();
document.Close();
}
catch
{
document.Close();
}
创建了pdf文件,但它是空的。我在第一个代码的第6行和第二个代码的第8行中看到以下错误。错误是:
无法找到文件'C:\ Program Files \ IIS Express#'。
答案 0 :(得分:2)
它似乎正在解析HTML的<img src='#'>
部分,然后尝试加载图像。尝试使用实际图像路径,或删除img
标记。