使用SSRS和ASP.NET合并以Pdf格式生成的报告

时间:2011-10-07 18:10:56

标签: c# asp.net reporting-services ssrs-2008

有没有办法通过将许多rdlc报告合并到.net框架中的一个大型pdf中来创建报告书。 通常我们通过Datasource生成pdfs到rdlc报告,然后我们将其呈现为pdf格式。那么渲染时我们可以合并多个rdlc生成的pdfs吗? 是否有任何工具可以合并使用SSRS(SQL Server报告服务)生成的pdf。

2 个答案:

答案 0 :(得分:0)

我已成功使用http://khsw.blogspot.com/2006/04/merge-pdf-files-using-itextsharp.html处的代码将多个现有PDF合并为一个文档。

答案 1 :(得分:0)

我知道您可以将PDF与iTextSharp合并。尝试这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Components.Pdf
{
    public class PdfDocumentMerger
    {
        private List<string> sourceFileList;

        #region constructors

        public PdfDocumentMerger()
        {
            //initialize the source file list
            sourceFileList = new List<string>();
        }

        public PdfDocumentMerger(params string[] fileNames) : this()
        {
            sourceFileList.AddRange(fileNames.AsEnumerable<string>());
        }

        #endregion

        /// <summary>
        /// Merges multiple PDF documents into one document
        /// </summary>
        /// <param name="DestinationFileName">The name and path to the merged document</param>
        /// <returns>The name and path to the merged document, if successful. Otherwise, the return value is an empty string</returns>
        public string Merge(string destinationFileName)
        {
            try
            {
                int sourceIndex = 0;
                PdfReader reader = new PdfReader(sourceFileList[sourceIndex]);
                int sourceFilePageCount = reader.NumberOfPages;

                Document doc = new Document(reader.GetPageSizeWithRotation(1));
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(destinationFileName, FileMode.Create));
                doc.Open();

                PdfImportedPage page;
                PdfContentByte contentByte = writer.DirectContent;                

                int rotation;
                while (sourceIndex < sourceFileList.Count)
                {
                    int pageIndex = 0;
                    while (pageIndex < sourceFilePageCount)
                    {
                        pageIndex++;
                        doc.SetPageSize(reader.GetPageSizeWithRotation(pageIndex));
                        doc.NewPage();

                        page = writer.GetImportedPage(reader, pageIndex);
                        rotation = reader.GetPageRotation(pageIndex);

                        if (rotation.Equals(90 | 270))
                            contentByte.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pageIndex).Height);
                        else
                            contentByte.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);                   
                    }

                    sourceIndex++;
                    if (sourceIndex < sourceFileList.Count)
                    {
                        reader = new PdfReader(sourceFileList[sourceIndex]);
                        sourceFilePageCount = reader.NumberOfPages;
                    }
                }

                doc.Close();
            }
            catch
            {
                throw;
            }

            return destinationFileName;
        }
    }
}