<tfoot id='5BQSC'></tfoot>

      <small id='5BQSC'></small><noframes id='5BQSC'>

    1. <legend id='5BQSC'><style id='5BQSC'><dir id='5BQSC'><q id='5BQSC'></q></dir></style></legend>
      1. <i id='5BQSC'><tr id='5BQSC'><dt id='5BQSC'><q id='5BQSC'><span id='5BQSC'><b id='5BQSC'><form id='5BQSC'><ins id='5BQSC'></ins><ul id='5BQSC'></ul><sub id='5BQSC'></sub></form><legend id='5BQSC'></legend><bdo id='5BQSC'><pre id='5BQSC'><center id='5BQSC'></center></pre></bdo></b><th id='5BQSC'></th></span></q></dt></tr></i><div id='5BQSC'><tfoot id='5BQSC'></tfoot><dl id='5BQSC'><fieldset id='5BQSC'></fieldset></dl></div>
        • <bdo id='5BQSC'></bdo><ul id='5BQSC'></ul>
      2. 合并内存流以在 c# 中创建 http PDF 响应

        Merging Memory Streams to create a http PDF response in c#(合并内存流以在 c# 中创建 http PDF 响应)
          <tbody id='YLR0J'></tbody>
        <legend id='YLR0J'><style id='YLR0J'><dir id='YLR0J'><q id='YLR0J'></q></dir></style></legend>

            <tfoot id='YLR0J'></tfoot>

            <i id='YLR0J'><tr id='YLR0J'><dt id='YLR0J'><q id='YLR0J'><span id='YLR0J'><b id='YLR0J'><form id='YLR0J'><ins id='YLR0J'></ins><ul id='YLR0J'></ul><sub id='YLR0J'></sub></form><legend id='YLR0J'></legend><bdo id='YLR0J'><pre id='YLR0J'><center id='YLR0J'></center></pre></bdo></b><th id='YLR0J'></th></span></q></dt></tr></i><div id='YLR0J'><tfoot id='YLR0J'></tfoot><dl id='YLR0J'><fieldset id='YLR0J'></fieldset></dl></div>

                <small id='YLR0J'></small><noframes id='YLR0J'>

                  <bdo id='YLR0J'></bdo><ul id='YLR0J'></ul>
                  本文介绍了合并内存流以在 c# 中创建 http PDF 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将 2 个水晶报告合并到单个 pdf 文件中,并且我正在使用 Itextsharp v5.1.1.但它说文件无法打开.它可能已损坏.没有构建错误.但是pdf格式不正确,无法打开.这是我选择完成此操作的顺序.

                  I am trying to merge 2 crystal reports into single pdf file and I'm using Itextsharp v5.1.1. But it says the document cannot be opened. It might be corrupted. There are no build errors. but the pdf is malformed and cant be opened. Here is the order I chose to accomplish this.

                  1. 将 Crystal 报表以 pdf 格式导出到 MemoryStream1
                  2. 将第二个报告导出到 MemoryStream2.
                  3. 合并内存流
                  4. 将流以 PDF 格式发送到 Http 输出响应.

                  这是订单中每个步骤的代码.

                  Here is the Code for each step in the order.

                     /// Get the Dataset from Stored Procedure for the CSSource Report
                     dsCS = CSData.GetUSSourceXML(ds_input);
                     /// Create the Report of type CSsource
                     rptCS = ReportFactory.GetReport(typeof(CSsource));
                     rptCS .SetDataSource(dsCS);
                     /// Set the Parameters to the CS report
                     rptCS .ParameterFields["Parameterid"].CurrentValues.Clear();
                     rptCS .SetParameterValue("Parameterid", PID);
                     //// Serialize the Object as PDF                    
                     msCS=(MemoryStream)rptCS .ExportToStream(ExportFormatType.PortableDocFormat);
                  

                  对于第 2 步

                     /// Get the Dataset from Stored Procedure for the Aden Report
                     dsAd = CSData.GetAdden(ds_input);
                     /// Create the Report of type Aden
                     rptAd = ReportFactory.GetReport(typeof(Aden));
                     rptAd.SetDataSource(dsAd );
                     /// Set the Parameters to the Aden report
                     rptAd.ParameterFields["Parameterid"].CurrentValues.Clear();
                     rptAd.SetParameterValue("Parameterid", PID);
                     //// Serialize the Object as PDF                    
                     msAD = (MemoryStream)rptAd.ExportToStream(ExportFormatType.PortableDocFormat);
                  

                  第三步

                    System.Collections.Generic.List<byte[]> sourceFiles = new List<byte[]>();
                    sourceFiles.Add(msCS.ToArray());
                    sourceFiles.Add(msAD.ToArray());
                    PdfMerger mpdfs = new PdfMerger();
                    /// ms is the Memory stream to which both the streams are added
                    ms=mpdfs.MergeFiles(sourceFiles);
                  

                  MergeFiles方法如下

                  MergeFiles method is as follows

                   public MemoryStream MergeFiles(Generic.List<byte[]> sourceFiles)
                      {
                          Document document = new Document();
                          MemoryStream output = new MemoryStream();
                  
                          try
                          {
                              // Initialize pdf writer
                              PdfWriter writer = PdfWriter.GetInstance(document, output);
                              //writer.PageEvent = new PdfPageEvents();
                  
                              // Open document to write
                              document.Open();
                              PdfContentByte content = writer.DirectContent;
                  
                              // Iterate through all pdf documents
                              for (int fileCounter = 0; fileCounter < sourceFiles.Count; 
                                     fileCounter++)
                              {
                                  // Create pdf reader
                                  PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
                                  int numberOfPages = reader.NumberOfPages;
                  
                                  // Iterate through all pages
                                  for (int currentPageIndex = 1; currentPageIndex <=
                                                     numberOfPages; currentPageIndex++)
                                  {
                                      // Determine page size for the current page
                                      document.SetPageSize(
                                         reader.GetPageSizeWithRotation(currentPageIndex));
                  
                                      // Create page
                                      document.NewPage();
                                      PdfImportedPage importedPage =
                                        writer.GetImportedPage(reader, currentPageIndex);
                  
                  
                                      // Determine page orientation
                                      int pageOrientation = 
                                        reader.GetPageRotation(currentPageIndex);
                                      if ((pageOrientation == 90) || (pageOrientation == 270))
                                      {
                                       content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0,
                                       reader.GetPageSizeWithRotation(currentPageIndex).Height);
                                      }
                                      else
                                      {
                                      content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
                                      }
                                  }
                              }
                          }
                          catch (Exception exception)
                          {
                          throw new Exception("There has an unexpected exception" +
                          " occured during the pdf merging process.", exception);
                          }
                          finally
                          {
                             // document.Close();
                          }
                          return output;
                      }
                  

                  将内存流序列化为 PDF 的步骤 4

                  Step 4 to Serialize the Memory stream as PDF

                    // ms is the memory stream which is to be converted to PDF
                    Response.Clear();
                          Response.ClearContent();
                          Response.ClearHeaders();
                          Response.ContentType = "application/pdf";
                          Response.Charset = string.Empty;
                          Response.AddHeader("Content-Disposition", 
                          "attachment; filename=" + 
                          "Application of " + FullName.Trim() + ".pdf");
                          //Write the file directly to the HTTP content output stream.
                          Response.OutputStream.Write(ms.ToArray(), 0, 
                                 Convert.ToInt32(ms.Length));
                          Response.OutputStream.Flush();
                          Response.OutputStream.Close();
                          rptCS.Close();
                          rptCS.Dispose();
                          rptAd.Close();
                          rptAd.Dispose();
                  

                  感谢所有帮助我解决此问题的开发人员.这是紧急的,因为它必须在一两天内投入生产.请回复.

                  Thanks for all those Developers helping me with this. This is Urgent because it has to go production in a day or 2. Please respond.

                  钱达南.

                  推荐答案

                  这里有一个简单的合并方法,将PDF文件复制到一个PDF中.我在合并 pdf 时经常使用这种方法.希望对您有所帮助.

                  Here's a simple merge method that copies PDF files into one PDF. I use this method quite often when merging pdfs. Hope it helps.

                      public MemoryStream MergePdfForms(List<byte[]> files)
                      {
                          if (files.Count > 1)
                          {
                              PdfReader pdfFile;
                              Document doc;
                              PdfWriter pCopy;
                              MemoryStream msOutput = new MemoryStream();
                  
                              pdfFile = new PdfReader(files[0]);
                  
                              doc = new Document();
                              pCopy = new PdfSmartCopy(doc, msOutput);
                  
                              doc.Open();
                  
                              for (int k = 0; k < files.Count; k++)
                              {
                                  pdfFile = new PdfReader(files[k]);
                                  for (int i = 1; i < pdfFile.NumberOfPages + 1; i++)
                                  {
                                      ((PdfSmartCopy)pCopy).AddPage(pCopy.GetImportedPage(pdfFile, i));
                                  }
                                  pCopy.FreeReader(pdfFile);
                              }
                  
                              pdfFile.Close();
                              pCopy.Close();
                              doc.Close();
                  
                              return msOutput;
                          }
                          else if (files.Count == 1)
                          {
                              return new MemoryStream(files[0]);
                          }
                  
                          return null;
                      }
                  

                  第 4 步尝试:

                          rptCS.Close();
                          rptCS.Dispose();
                          rptAd.Close();
                          rptAd.Dispose();
                  
                          Response.Clear();
                          Response.ContentType = "application/pdf";
                          Response.AddHeader("Content-Disposition", 
                      "attachment; filename=" + 
                      "Application of " + FullName.Trim() + ".pdf");
                          Response.BinaryWrite(ms.ToArray());
                          ApplicationInstance.CompleteRequest();
                  

                  这篇关于合并内存流以在 c# 中创建 http PDF 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)

                    <tfoot id='IkCEr'></tfoot>
                      <legend id='IkCEr'><style id='IkCEr'><dir id='IkCEr'><q id='IkCEr'></q></dir></style></legend>
                        <tbody id='IkCEr'></tbody>
                    1. <small id='IkCEr'></small><noframes id='IkCEr'>

                          <i id='IkCEr'><tr id='IkCEr'><dt id='IkCEr'><q id='IkCEr'><span id='IkCEr'><b id='IkCEr'><form id='IkCEr'><ins id='IkCEr'></ins><ul id='IkCEr'></ul><sub id='IkCEr'></sub></form><legend id='IkCEr'></legend><bdo id='IkCEr'><pre id='IkCEr'><center id='IkCEr'></center></pre></bdo></b><th id='IkCEr'></th></span></q></dt></tr></i><div id='IkCEr'><tfoot id='IkCEr'></tfoot><dl id='IkCEr'><fieldset id='IkCEr'></fieldset></dl></div>

                            <bdo id='IkCEr'></bdo><ul id='IkCEr'></ul>