2011-11-29 90 views
2

我有一個下載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更有經驗的人能幫我一個忙嗎?

+0

我不確定這是否是答案,但您可能需要關閉您創建的'FileStream',而不是'iTextSharp.text.Document'。我不在VS面前,所以我無法測試它。 – Timiz0r

回答

0

想通了,你根本不應該使用using作爲DocumentPDFWriter的對象。此代碼的工作:

/// <summary> 
    /// Saves the entire manual as PDF 
    /// </summary> 
    public static void createEntireManual(string PDFPath) 
    { 
     Document d = new Document(); 
     PdfWriter.GetInstance(d, new FileStream(PDFPath, FileMode.Create)); 
     d.Open(); 
     d.SetPageSize(iTextSharp.text.PageSize.A4); 
     Image Cover = Image.GetInstance(Settings.ManualBinLocation + "cover.png"); 
     Cover.SetAbsolutePosition(0, 0); 
     d.Add(Cover); 
     d.Close();    

    } 
+0

靜態方法?沒有鎖? asp.net?文件系統?併發災難的配方。 – x0n

+0

@ z0n最好的辦法是什麼?我需要它來構建PDF,然後提供它。 –

+0

想想如果兩個人在大致相同的時間請求相同但尚未創建的PDF,會發生什麼情況。這裏有一個很好的併發和線程教程,我總是推薦:http://www.albahari.com/threading/ – x0n

2

需要保存PDF文件系統?否則,直接使用對象的OutputStream要容易得多。這裏有一個簡單的例子:

<%@ WebHandler Language="C#" Class="handlerExample" %> 
using System; 
using System.Web; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

public class handlerExample : IHttpHandler { 
    public void ProcessRequest (HttpContext context) { 
    HttpResponse Response = context.Response; 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader(
     "Content-disposition", 
     "attachment; filename=Construct2-Manual-Full.pdf" 
    ); 
    using (Document document = new Document()) { 
     PdfWriter.GetInstance(
     document, Response.OutputStream 
    ); 
     document.Open(); 
     document.Add(new Paragraph("Hello World!")); 
    } 
    } 
    public bool IsReusable { get { return false; } } 
} 

所以說你不應該使用using說法是不正確。上面的例子簡化了,但可以很容易地擴展;從你上面命名你的方法的方式來看,也許你正在從許多不同的小PDF文檔中構建PDF手冊?

相關問題