由1个PDF模板创建的多页PDF

这是我的情况,我有一个数据模型列表,它将给定的PDF模板填充到一个大的pdf中,并显示在网页上供客户端打印文件 . (没有本地副本) . 我使用kuujinbo的例子https://stackoverflow.com/a/8525007/5533303但由于某种原因我可以't let the memory stream to bind when i am going to display the pdf on page. (the error is can't读取封闭的内存流 . )我的系统环境是带有最新itextsharp的MVC 5 .

20151107更新:找到原因 . 总体代码是正确的但在循环结束时我应该把byteInfo = Masterstream.ToArray();在smartcopy之外使用但在文档内部使用 . 然后创建一个新的内存字符串以捕获byteinfo并返回到视图 .

List<Datamodel> PrintList = ( List<Datamodel> )Session["GetDAta"];
         BaseFont baseChineseFont = BaseFont.CreateFont(@"~~~~~~~~~",
            BaseFont.IDENTITY_H,  BaseFont.NOT_EMBEDDED);
         MemoryStream Masterstream = new MemoryStream();
         byte[] byteInfo;
         using (Document document = new Document())
         {                
             using (PdfSmartCopy copy = new PdfSmartCopy(document, Masterstream))
             {
                 document.Open();
                 foreach (Datamodel childnode in PrintList)
                 {
                     PdfReader pdfReader = new PdfReader(Request.MapPath("~/Content/try 1.pdf"));
                     using (MemoryStream ms = new MemoryStream())
                     {
                         using (PdfStamper stamper = new PdfStamper(pdfReader, ms))
                         {
                             // do stuff      //
                               AcroFields pdfForm = stamper.AcroFields;
                             #region[input data]

                             pdfForm.AddSubstitutionFont(baseChineseFont);//加font

                             pdfForm.SetField("XXXXXXXX", childnode.XXXXXX);

                             #endregion                                                     
                             stamper.FormFlattening = true;
                         }
                         pdfReader = new PdfReader(ms.ToArray());
                         copy.AddPage(copy.GetImportedPage(pdfReader, 1));

                     }

                 }
                 byteInfo = Masterstream.ToArray();
             }                
         }                         
         Masterstream.Write(byteInfo, 0, byteInfo.Length);
         Masterstream.Position = 0;
         return new FileStreamResult(Masterstream, "application/pdf");

正确的结果

}///smartcopy end using
byteInfo = Masterstream.ToArray();
         }         //document   end using 
        MemoryStream finalyresult= new  MemoryStream(byteInfo);
        finalyresult.Write(byteInfo, 0, byteInfo.Length);
        finalyresult.Position = 0;
         return new FileStreamResult(finalyresult, "application/pdf");

回答(1)

2 years ago

using (Document document = new Document())
     {                
         using (PdfSmartCopy copy = new PdfSmartCopy(document, Masterstream))
         {
             document.Open();
             foreach (Datamodel childnode in PrintList)
             {
                 PdfReader pdfReader = new PdfReader(Request.MapPath("~/Content/try 1.pdf"));
                 using (MemoryStream ms = new MemoryStream())
                 {
                     using (PdfStamper stamper = new PdfStamper(pdfReader, ms))
                     {
                         // do stuff      //
                           AcroFields pdfForm = stamper.AcroFields;
                         #region[input data]

                         pdfForm.AddSubstitutionFont(baseChineseFont);//加font

                         pdfForm.SetField("XXXXXXXX", childnode.XXXXXX);

                         #endregion                                                     
                         stamper.FormFlattening = true;
                     }
                     pdfReader = new PdfReader(ms.ToArray());
                     copy.AddPage(copy.GetImportedPage(pdfReader, 1));

                 }

             }
               }///smartcopy end using
           byteInfo = Masterstream.ToArray();
     }         //document   end using 
    MemoryStream finalyresult= new  MemoryStream(byteInfo);
    finalyresult.Write(byteInfo, 0, byteInfo.Length);
    finalyresult.Position = 0;
     return new FileStreamResult(finalyresult, "application/pdf");