我有一个下载PDF处理程序:该进程无法访问该文件,因为它正在使用由另一个进程(iTextSharp的C#)
// 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对象作为使用语句,但它不喜欢这样,抛出无意义的错误:
Exception Details: System.Exception: The document is not open.
关于使用右括号。
iTextSharp更有经验的人能帮我一个忙吗?
我不确定这是否是答案,但您可能需要关闭您创建的'FileStream',而不是'iTextSharp.text.Document'。我不在VS面前,所以我无法测试它。 – Timiz0r