Wednesday, February 13, 2019

SSRS : merge multiple rdlc report PDF in single PDF

In this blog we will discuss how to merge more than one PDF in single PDF file generated by RDLC report.


Here is code to generate PDF report data (in memory stream) from rdlc file:
C# code:

public MemoryStream WritePDFReport(List<ReportModelreportData)
        {
            Microsoft.Reporting.WinForms.Warning[] warnings;
            string[streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            string deviceInfo =
          "<DeviceInfo>" +
          "  <OutputFormat>PDF</OutputFormat>" +
          "</DeviceInfo>";

            ReportViewer viewer = new ReportViewer();
            try
            {
                string exeFolder = System.AppDomain.CurrentDomain.BaseDirectory;
                string reportPath = Path.Combine(exeFolderstring.Format(@"Sample.rdlc));

                viewer.ProcessingMode = ProcessingMode.Local;
                viewer.LocalReport.ReportPath = reportPath;                              
                
                ReportDataSource ds = new ReportDataSource("ReportDataSet"reportData);
                viewer.LocalReport.DataSources.Add(ds);

               viewer.LocalReport.Refresh();
              
               byte[] bytes = viewer.LocalReport.Render("PDF"deviceInfo
               out mimeTypeout encoding, out extension, out streamIdsout warnings);

                return new MemoryStream(bytes);           
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                viewer.LocalReport.Dispose();
                viewer = null;
            }
        }

WritePDfReport() method takes report data as input and it generates report by using ReportViewer Class and it calls Render method to generate the memory stream of report.

the above code generates only single PDF file and if you want to generate more than one fPDF iles and merge all into a single PDF file, for this you need to use PDFsharp.


Here is code to merge PDF files
C# Code:

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;


          List<ReportModelreportData;

          PdfDocument document = new PdfDocument();
            for (int ix = 0; ix < 4; ix++)
            {
                PdfDocument tempPDFDoc = PdfReader.Open(WritePDFReport(reportData), PdfDocumentOpenMode.Import);

                for (int i = 0; i < tempPDFDoc.PageCounti++)
                {
                    PdfPage page = tempPDFDoc.Pages[i];
                    document.AddPage(page);
                }
            }

            document.Save("C://logs/sample.pdf");       
            document.Close()

·         Call WritePDFReport() method to generate report and get memory stream ( in byte arrays)
·         Import report data into temporary PdfDocument object - tempPDFDoc by Call PdfReader.Open() method
·         Add the imported PDF page from into Main PdfDocument object – document.
·         Call PdfDocument ‘s save method to save all imported pdf page into single pdf page (sample.pdf)
·         Call Close() method of PdfDocument – document

For more information about PDFSharp dll, Code Samples and installation steps please visit at below URLs
·         http://www.pdfsharp.net 
Thanks for visiting!!

1 comment:

Anonymous said...

Hello Rajeev Tiwari,
I found this post very useful and informative. Thank you for sharing this post. I have been using ZetPDF for a while now and it is a .net SDK for adding PDF render and print support in .net applications. It is designed to solve most developer’s needs with regards to PDF rendering.

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...