5
我在使用Windows Form程序的 中的HTMLWorker.Parse從iTextSharp出現問題。每當我執行代碼時,它都會從HTMLWorker.Parse開始,它會給出objectDisposedException。 該例外說明它無法訪問已關閉的文件。但我多次檢查了 ,無法找到已關閉的文件。下面是代碼:Itextsharp HTMLWorker.Parse錯誤
class HtmlToPdfConverter
{
private iTextSharp.text.Document doc = new iTextSharp.text.Document();
public HtmlToPdfConverter()
{
this.doc.SetPageSize(PageSize.A4);
}
public string Run(string html, string pdfName)
{
try
{
using (doc)
{
StyleSheet styles = new StyleSheet();
using (PdfWriter writer = PdfWriter.GetInstance(this.doc, new FileStream(@"Z:\programs\" + pdfName + ".pdf", FileMode.Create)))
{
this.doc.Open();
this.doc.OpenDocument();
this.doc.NewPage();
if (this.doc.IsOpen() == true)
{
StringReader reader = new StringReader(html);
//XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, reader);
this.doc.Add(new Paragraph(" "));
HTMLWorker worker = new HTMLWorker(this.doc);
worker.Open();
worker.StartDocument();
worker.NewPage();
worker.Parse(reader);
worker.SetStyleSheet(styles);
List<IElement> ie = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(reader, null);
foreach (IElement element in ie)
{
this.doc.Add((IElement)element);
}
worker.EndDocument();
worker.Close();
}
}
}
return string.Empty;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
這是例外:
System.ObjectDisposedException was caught
Message=Cannot access a closed file.
Source=mscorlib
ObjectName=""
StackTrace:
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
at iTextSharp.text.pdf.OutputStreamCounter.Write(Byte[] buffer, Int32 offset, Int32 count)
at iTextSharp.text.pdf.PdfIndirectObject.WriteTo(Stream os)
at iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, Int32 refNumber, Boolean inObjStm)
at iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, Int32 refNumber)
at iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, PdfIndirectReference refa)
at iTextSharp.text.pdf.PdfWriter.AddToBody(PdfObject objecta, PdfIndirectReference refa)
at iTextSharp.text.pdf.Type1Font.WriteFont(PdfWriter writer, PdfIndirectReference piref, Object[] parms)
at iTextSharp.text.pdf.FontDetails.WriteFont(PdfWriter writer)
at iTextSharp.text.pdf.PdfWriter.AddSharedObjectsToBody()
at iTextSharp.text.pdf.PdfWriter.Close()
at iTextSharp.text.DocWriter.Dispose()
at WebPageExtraction.HtmlToPdfConverter.Run(String html, String pdfName)
InnerException:
我加入那些doc.close和.closeDocument作爲額外的看,如果說是去上班。我試過你的解決方案,但它仍然無法工作。感謝您的幫助。 – Emon
是的,找到了真正的原因。看我的編輯。關鍵的變化是添加'writer.CloseStream = false;' –
現在它給出了另一個例外。這是webexception。它說它找不到網絡路徑。這個版本也停在worker.parse上,你知道iTextSharp中的方法有什麼問題嗎?它不再給出其他例外。感謝你們對我的幫助。 – Emon