2016-09-20 91 views
-1

我想用iTextSharp創建PDF文件。第一次pdf創建,但當我去編程刪除或overwrited我得到以下例外:pdf覆蓋例外c#(iTextSharp)

'/'應用程序中的服務器錯誤。
該進程無法訪問文件'C:\ Users \ g.michailou \ Desktop \ InfocomFinal \ barcodes \ 9880892102.pdf',因爲它正在被另一個進程使用。

the second time when i run code

我不知道爲什麼文件保持開放...這裏是我的示例代碼

private void CreatePdf(string first, string last, string value) 
{ 
    using (MemoryStream myMemoryStream = new MemoryStream()) 
    { 
     Document document = new Document(PageSize.A4, 100, 100, 100, 100); 
     PdfWriter myPDFWriter = PdfWriter.GetInstance(document, myMemoryStream); 

     document.Open(); 

     BaseFont MyFont = iTextSharp.text.pdf.BaseFont.CreateFont("C:\\Windows\\Fonts\\Arial.ttf", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED); 

     var titleFont = new iTextSharp.text.Font(MyFont, 16, iTextSharp.text.Font.BOLD); 
     var subTitleFont = new iTextSharp.text.Font(MyFont, 22, iTextSharp.text.Font.NORMAL); 
     var subTitleFont2 = new iTextSharp.text.Font(MyFont, 16, iTextSharp.text.Font.NORMAL); 
     var boldTableFont = new iTextSharp.text.Font(MyFont, 12, iTextSharp.text.Font.NORMAL); 
     var endingMessageFont = new iTextSharp.text.Font(MyFont, 10, iTextSharp.text.Font.ITALIC); 
     var bodyFont = new iTextSharp.text.Font(MyFont, 12, iTextSharp.text.Font.NORMAL); 

     var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/" + currentEvent + ".jpg")); 
     logo.ScaleToFit(395, 160); 

     document.Add(logo); 

     Helpers.GetEventInformation(currentEvent); 


     Paragraph name = new Paragraph(first, subTitleFont); 
     name.Alignment = 1; 
     name.SpacingBefore = 20; 
     document.Add(name); 

     Paragraph surname = new Paragraph(last, subTitleFont); 
     surname.Alignment = 1; 
     document.Add(surname); 


     Paragraph info1 = new Paragraph("Επιβεβαίωση Εγγραφής στο", subTitleFont2); 
     info1.Alignment = 1; 
     info1.SpacingBefore = 25; 
     document.Add(info1); 

     Paragraph info2 = new Paragraph(Helpers.confinfo, subTitleFont2); 
     info2.Alignment = 1; 
     document.Add(info2); 

     Paragraph info3 = new Paragraph(Helpers.maildate, titleFont); 
     info3.Alignment = 1; 
     info3.SpacingBefore = 25; 
     document.Add(info3); 

     Paragraph info4 = new Paragraph(Helpers.mailtime, subTitleFont2); 
     info4.Alignment = 1; 
     document.Add(info4); 


     document.Add(Chunk.NEWLINE); 
     document.Add(Chunk.NEWLINE); 

     var barcodeImage = iTextSharp.text.Image.GetInstance(Server.MapPath("~/barcodes/" + value + ".png")); 
     barcodeImage.Alignment = iTextSharp.text.Image.ALIGN_CENTER; 
     barcodeImage.SpacingBefore = 25; 
     document.Add(barcodeImage); 

     Paragraph barcodev = new Paragraph(value, endingMessageFont); 
     barcodev.SpacingBefore = -5; 
     barcodev.Alignment = 1; 
     document.Add(barcodev); 

     document.Add(Chunk.NEWLINE); 
     document.Add(Chunk.NEWLINE); 

     var logo2 = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/footer.jpg")); 
     logo2.SpacingBefore = 30; 
     logo2.ScaleToFit(395, 130); 
     document.Add(logo2); 


     document.Close(); 

     byte[] content = myMemoryStream.ToArray(); 

     pdfPath = Server.MapPath("~/barcodes/" + value + ".pdf"); 
     using (FileStream fs = System.IO.File.Create(pdfPath)) 
     { 
      fs.Write(content, 0, (int)content.Length); 
     } 
    } 
} 
+0

你有沒有在acrobat或任何其他pdf閱讀器中打開它? –

+0

@SamuelHuylebroeck不,我不要 –

+1

首先,iTextSharp與您使用的代碼中使用的是完全無關的問題,因爲您只能用它在內存**中創建PDF **;即使PDF格式本身也不重要;字節[]內容'可以很容易地以任何其他方式創建幷包含任何類型的字節。因此,您應該刪除標籤[tag:pdf]和[tag:itextsharp]並將其替換爲與使用'System.IO.File'和Web服務相關的標籤。 – mkl

回答

0

也許你需要關閉 「myPDFWriter」

+0

我添加myPDFWriter.Close();在document.Open()之前;但我得到同樣的例外。 –

+0

只需在document.Close()後添加它即可; – Josep

+0

'document.Close()'特別調用底層'PdfWriter'的'Close'方法。 – mkl

0

我發現解決方案。 我的下一個方法發送一封附帶pdf的電子郵件,但問題在於,pdf文件在附加時保持打開狀態。

我加了這一點:

using (System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath(bytes))) 
      { 
       mm.Attachments.Add(data); 
       ... 
      } 

,現在它的工作。