Reputation: 2973
I am reading a text file and trying to parse it as an html and make a pdf of it with iTextSharp
.
Here I load the text file:
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);
The HTML.txt
contains something like the following:
<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>...
And here I try to parse it as html and print it in a 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();
}
And I tried this too:
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();
}
The pdf file is created but its empty. I see the following error in line 6 in the first code and in line 8 in the second code. The error is:
Could not find file 'C:\Program Files\IIS Express#'.
Upvotes: 1
Views: 3469
Reputation: 2993
It seems to be parsing the <img src='#'>
part of your HTML, and subsequently attempting to load the image. Try using a real image path, or removing the img
tag.
Upvotes: 2