【发布时间】:2011-11-29 19:47:41
【问题描述】:
我有一个下载 PDF 处理程序:
// Build the PDF
Manual.Functions.createEntireManual(ThisDownload.FileLocation);
// Download file
context.Response.Clear();
context.Response.Buffer = false;
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-disposition", "attachment; filename=Construct2-Manual-Full.pdf");
context.Response.AddHeader("Content-Length", FileSize.ToString());
context.Response.TransmitFile(Settings.ManualBinLocation + ThisDownload.ID + ".pdf");
context.Response.Close();
手动创建函数如下所示:
public static void createEntireManual(string PDFPath)
{
iTextSharp.text.Document d = new iTextSharp.text.Document();
d.Open();
using (iTextSharp.text.pdf.PdfWriter p = iTextSharp.text.pdf.PdfWriter.GetInstance(d, new FileStream(PDFPath, FileMode.Create)))
{
d.Add(new Paragraph("Hello world!"));
}
}
这会引发错误:
Exception Details: System.IO.IOException: The process cannot access the file 'C:\inetpub\wwwroot\manuals\26.pdf' because it is being used by another process.
好的,所以我在我的函数中添加d.Close():
d.Add(new Paragraph("Hello world!"));
}
d.Close();
}
但这会引发:
Exception Details: System.Exception: The document is not open.
在d.Close() 线上。我尝试将新的 Document 对象添加为 using 语句,但它不喜欢这样,抛出无意义的错误:
Exception Details: System.Exception: The document is not open.
关于使用右括号。
有谁对 iTextSharp 更有经验吗?
【问题讨论】:
-
我不确定这是否是答案,但您可能需要关闭您创建的
FileStream,而不是iTextSharp.text.Document。我不在VS前面,所以无法测试。
标签: c# asp.net itextsharp