我有一個下載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