合并内存流创建在C#中的HTTP响应PDF [英] Merging Memory Streams to create a http PDF response in c#

查看:177
本文介绍了合并内存流创建在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报表MemoryStream1 PDF格式
  2. 导出的第二份报告为MemoryStream2。
  3. 合并内存流
  4. 将流HTTP输出响应为PDF。

下面是code为顺序的每一步。

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);

有关第3步

  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;
    }

步骤4,以序列化内存流为PDF

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();

感谢所有那些开发者帮助我与此有关。 这是因为紧急它要生产在一天或2。 请回答。

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.

Chandanan。

Chandanan.

推荐答案

下面是一个简单的合并方法复制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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆