开发者

Merging Reports Generated in Pdf format by using SSRS and ASP.NET

开发者 https://www.devze.com 2023-04-11 06:56 出处:网络
Is there a way to create book of reports by merging many rdlc reports into one large pdf in .net framework.

Is there a way to create book of reports by merging many rdlc reports into one large pdf in .net framework. Usually we pass Datasource to generate the pdfs to rdlc report and then we render it into pdf form. So while rendering can we merge multiple rdlc generated pdfs? Are 开发者_JS百科there any tools available that will merge the pdfs generated by using SSRS(SQL Server Reporting Service).


I've successfully used the code at http://khsw.blogspot.com/2006/04/merge-pdf-files-using-itextsharp.html to merge multiple existing PDFs into a single document.


I know you can merge PDFs with iTextSharp. Try something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号