I am saving HTML content to PDF with the following code:
public void SaveHTMLToPdf(string HTML, string FilePath)
{
Document document = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Invoice_Statement.pdf", FileMode.Create));
document.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(Server.MapPath(ImgCom.ImageUrl.ToString()));
pdfImage.ScaleToFit(150, 100);
pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(40, 770);
document.AddTitle("Invoice Details");
document.Add(pdfImage);
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
styles.LoadTagStyle("th", "color", "red");
styles.LoadTagStyle("th", "frontsize", "5");
document.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, "Style.css"));
hw.Parse(new StringReader(HTML));
document.Close();
}
But it gives this error:
The process cannot access the file 'C:\inetpub\wwwroot\abc\Invoice_Statement.pdf' because it is being used by another process.
At this line:
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath +
"\\Invoice_Statement.pdf", FileMode.Create));
It happen only on server with IIS-7 and works fine on local with visual studio.
Can anyone help..??