2013-09-25 45 views
0

我有,每頁60個標籤一個PDF模板文件。我的目標是根據需要進行模板的副本,填寫表格數據,然後將文件合併成一個單一的PDF(或提供鏈接到個人文件...無論工程)PDF模板,第二個文件已經損壞的保存2份 - iTextSharp的

的問題是,第二PDF不管日期如何,副本都會腐敗。

工作流是用戶選擇的日期。當天的午餐訂單被收集到一個通用列表中,然後用於填寫模板上的表單字段。在60,將文件保存爲一個臨時文件和模板的新副本用於接下來的60名,等等

2013年9月23日通過09/25有數據。 25日只有38個訂單,所以按預期工作。在2013年9月24日有超過60個訂單,第一個頁面可以工作,但第二個頁面已經損壞。

private List<string> CreateLabels(DateTime orderDate) 
{ 

    // create file name to save 
    string fName = ConvertDateToStringName(orderDate) + ".pdf"; // example 09242013.pdf 

    // to hold Temp File Names 
    List<string> tempFNames = new List<string>(); 

    // Get path to template/save directory 
    string path = Server.MapPath("~/admin/labels/"); 
    string pdfPath = path + "8195a.pdf"; // template file 

    // Get the students and their lunch orders 
    List<StudentLabel> labels = DalStudentLabel.GetStudentLabels(orderDate); 


    // Get number of template pages needed 
    decimal recCount = Convert.ToDecimal(labels.Count); 
    decimal pages = Decimal.Divide(recCount, 60); 
    int pagesNeeded = Convert.ToInt32(Math.Ceiling(pages)); 


    // Make the temp names 
    for (int c = 0; c < pagesNeeded; c++) 
    { 
     tempFNames.Add(c.ToString() + fName); //just prepend a digit to the date string 
    } 

    //Create copies of the empty templates 
    foreach (string tName in tempFNames) 
    { 
     try 
     { File.Delete(path + tName); } 
     catch { } 

     File.Copy(pdfPath, path + tName); 
    } 

    // we know we need X pages and there is 60 per page 
    int x = 0; 

    // foreach page needed 
    for (int pCount = 0; pCount < pagesNeeded; pCount++) 
    { 
     // Make a new page 
     PdfReader newReader = new PdfReader(pdfPath); 

     // pCount.ToString replicates temp names 
     using (FileStream stream = new FileStream(path + pCount.ToString() + fName, FileMode.Open)) 
     { 
      PdfStamper stamper = new PdfStamper(newReader, stream); 

      var form = stamper.AcroFields; 
      var fieldKeys = form.Fields.Keys; 

      StudentLabel lbl = null; 

      string lblInfo = ""; 

      // fill in acro fields with lunch data 
      foreach (string fieldKey in fieldKeys) 
      { 
       try 
       { 
        lbl = labels[x]; 
       } 
       catch 
       { 
        break; 
       } // if we're out of labels, then we're done 

       lblInfo = lbl.StudentName + "\n"; 
       lblInfo += lbl.Teacher + "\n"; 
       lblInfo += lbl.MenuItem; 

       form.SetField(fieldKey, lblInfo); 

       x++; 

       if (x % 60 == 0) // reached 60, time for new page 
       { 
        break; 
       } 
      } 

      stamper.Writer.CloseStream = false; 
      stamper.FormFlattening = true; 
      stamper.Close(); 
      newReader.Close(); 

      stream.Flush(); 
      stream.Close(); 
     }   
    } 

    return tempFNames; 
} 
+1

我不明白你的代碼:你爲什麼不容許壓模關閉流? –

回答

1

你爲什麼要預分配的文件嗎?我的猜測是這是你的問題。你輸入和相同的PDF的完全相同的副本PdfStamperPdfReader綁定到FileStream對象輸出。 PdfStamper將爲您生成輸出文件,您無需提供幫助。你正在嘗試將新數據追加到現有文件中,我不太確定在這種情況下會發生什麼(因爲我從未見過任何人這樣做)。

因此,請刪除您的整個File.Copy預分配和改變你的FileStream聲明:

using (FileStream stream = new FileStream(path + pCount.ToString() + fName, FileMode.Create, FileAccess.Write, FileShare.None)) 

你顯然還需要調整你的回報數組被填充方式,太。

+0

好吧,看起來這是預先分配的文件是以某種方式導致問題。刪除File.Copy行以創建模板的副本並使用您建議的「使用(FileStream ...」),它就像一個魅力。 – user2229424