2017-02-01 116 views
0

我有一個從HTML頁面接受數據,並將該數據標記到PDF並將PDF保存到第三方成像系統的過程。iTextSharp - 填寫PDF多次併合併爲一個pdf

該HTML頁面接受最多8個用戶的信息。但是,PDF只能容納4個用戶。因此,如果輸入的信息超過4個,我需要填寫PDF兩次並將這兩個PDF合併到一個文件中以提交給成像系統。

我試過嵌套流,pdfReaders,pdfStampers,但最終的PDF總是損壞。任何幫助將不勝感激。提前致謝。

下面的代碼我目前是處理衝壓PDF如果有1 - 4用戶進入:

var pdfTemplate = ConfigurationManager.AppSettings["PdfFileLocation"] + formName + ".pdf"; 
var pdfReader = new PdfReader(pdfTemplate); 

using (var stream = new MemoryStream()) 
{ 
    using (var pdfStamper = form == null 
    ? new PdfStamper(pdfReader, stream, '\0', false) 
    : processType == 1 // printing form 
     ? new PdfStamper(pdfReader, new FileStream(Path.GetTempPath() + "UserForm_" + UserNumber + ".pdf", FileMode.Create)) // temporarily save form 
     : new PdfStamper(pdfReader, stream, '\0', false)) // use a stream if submitting 
    { 
     var overContent1 = pdfStamper.GetOverContent(1); 
     var overContent2 = pdfStamper.GetOverContent(2); 
     var overContent3 = pdfStamper.GetOverContent(3); 
     var overContent4 = pdfStamper.GetOverContent(4); 
     var pdfFormFields = pdfStamper.AcroFields; 

     ... Code to fill out other pages of the PDF goes here ... 

     #region User Form Page #3 

     foreach (var user in users) 
     { 
      var eligibility = _sharedFormService.GetEligibility(user.Eligibility); 
      pdfFormFields.SetField("User" + nameof(user.Name) + user.Id, user.Name); 
      pdfFormFields.SetField("User" + nameof(user.Eligibility) + user.Id, eligibility); 
      pdfFormFields.SetField("User" + nameof(user.Title) + user.Id, user.Title); 
      pdfFormFields.SetField("User" + nameof(user.Type) + user.Id, user.Type); 
      pdfFormFields.SetField("User" + nameof(user.FamilyMemberName) + user.Id, user.FamilyMemberName); 

      try 
      { 
       if (user.Signature != null) 
       { 
        var sigImage = _imageHelpers.LoadImage(user.Signature); 
        var image = Image.GetInstance(sigImage, System.Drawing.Imaging.ImageFormat.Gif); 
        var sigPostitions = pdfFormFields.GetFieldPositions("User" + nameof(user.Signature) + user.Id)[0].position; 

        image.Transparency = new int[] { 255, 255 }; 
        image.SetAbsolutePosition(sigPostitions.Left, sigPostitions.Bottom); 
        image.ScalePercent(15); 

        overContent3.AddImage(image); 
       } 
      } 
      catch (DocumentException dex) 
      { 
       response.Success = false; 
       response.ErrorMessage += "StampPdf - UserSignature" + user.Id + ": " + (dex.InnerException != null 
        ? dex.InnerException.Message 
        : dex.Message) + ", "; 
      } 
      catch (IOException ioex) 
      { 
       response.Success = false; 
       response.ErrorMessage += "StampPdf - UserSignature" + user.Id + ": " + (ioex.InnerException != null 
        ? ioex.InnerException.Message 
        : ioex.Message) + ", "; 
      } 
     } 

     #endregion 

     pdfStamper.FormFlattening = true; 
    } 

    response.Stream = stream.ToArray(); 
} 

回答

1

谷歌搜索的幾天後,我終於找到了我從尋找答案iText的常見問題網站,「合併(有相同的字段)相同的形式」下(http://developers.itextpdf.com/question/how-merge-forms-different-files-one-pdf

這裏是我最後使用的代碼:

public static byte[] ManipulatePdf(string src, string dest) 
    { 
     var ms = new MemoryStream(); 
     Document document = new Document(); 
     PdfCopy copy = new PdfSmartCopy(document, ms); 
     copy.SetMergeFields(); 
     document.Open(); 
     List<PdfReader> readers = new List<PdfReader>(); 
     for (int i = 0; i < 2;) 
     { 
      PdfReader reader = new PdfReader(RenameFields(src, ++i)); 
      readers.Add(reader); 
      copy.AddDocument(reader); 
     } 
     document.Close(); 
     foreach (var reader in readers) 
     { 
      reader.Close(); 
     } 

     return ms.ToArray(); 
    } 

    public static byte[] RenameFields(String src, int i) 
    { 
     MemoryStream baos = new MemoryStream(); 
     PdfReader reader = new PdfReader(src); 
     PdfStamper stamper = new PdfStamper(reader, baos); 
     AcroFields form = stamper.AcroFields; 
     var keys = new HashSet<string>(form.Fields.Keys); 
     foreach (var key in keys) 
     { 
      form.RenameField(key, string.Format("{0}_{1}", key, i)); 
     } 
     stamper.Close(); 
     reader.Close(); 
     return baos.ToArray(); 
    }