Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Merging Reports - SSRS

RSS
Modified on Tue, Sep 06, 2011, 11:03 AM by Administrator Categorized as ASP·NET Web Forms, PostScript and PDF, SSRS (SQL Server Reporting Services)

Overview

The solution involves the PdfSharp library (available here) to merge multiple PDF files.

Code

public void RenderPdfToHttpResponse(HttpResponseBase response)
{
    /*--- Inits ---*/
    VPublicationIssue issue;
    var data = MyDataProvider.GetMyData();
    var fileName = "My Report.pdf";
    fileName = fileName.Replace(" ", "-");
    var outputDoc = new PdfDocument();

    /*--- By Name ---*/
    var rptByName = new ReportA(data);
    AppendReport(outputDoc, rptByName);

    /*--- By Address ---*/
    var rptByAddress = new ReportB(data);
    AppendReport(outputDoc, rptByAddress);

    /*--- Clean Up ---*/
    var ms = new MemoryStream();
    outputDoc.Save(ms);
    response.Clear();
    response.ContentType = "application/pdf";
    response.AddHeader("content-disposition", "attachment; filename=" + fileName);
    response.BinaryWrite(ms.ToArray());
    response.End();

}
private static void AppendReport(PdfDocument outputDoc, ReportBase rpt)
{
    var bytes = rpt.RenderReport(RenderOptionEnum.Pdf);
    var inputDoc = PdfReader.Open(new MemoryStream(bytes), PdfDocumentOpenMode.Import);
    var pageCount = inputDoc.PageCount;

    for (var i = 0; i < pageCount; i++)
        outputDoc.AddPage(inputDoc.Pages[i]);
}

ReportBase Class

public override byte[] RenderReport(RenderOptionEnum renderOptionEnum)
{
    SetMainReportDataSource(LocalReport);
    Render(LocalReport, renderOptionEnum);
    return RenderedBytes;
}

protected override void SetMainReportDataSource(LocalReport localReport)
{
    SetMainReportDataSource(localReport, true);
}

protected void SetMainReportDataSource(LocalReport localReport, bool setDataSource)
{
    // (typeof(TClass)) returns the fully-qualified class name
    localReport.ReportEmbeddedResource = (typeof(TClass)) + ".rdlc";
    if (!setDataSource) return;
    localReport.DataSources.Clear();
    localReport.DataSources.Add(new ReportDataSource("MainDataSet", _data));
}
protected void Render(LocalReport localReport, RenderOptionEnum type)
{
    string mimeType = "application/pdf";
    string fileNameExtension = "pdf";
    byte[] renderedBytes = null;

    string typeName = "pdf";

    //string reportType = typeName;
    string encoding;

    //The DeviceInfo settings should be changed based on the reportType
    //http://msdn2.microsoft.com/en-us/library/ms155397.aspx

    // Per http://forum.pdfsharp.net/viewtopic.php?p=1613#p1613, we add the <HumanReadablePDF>
    // tag to disable PDF file compression for SSRS 2008 and thus make the resulting PDF compatible
    // with PDFSharp
    string deviceInfo =
        "<DeviceInfo>" +
        "  <OutputFormat>" + typeName + "</OutputFormat>" +
        "  <PageWidth>" + PageWidth + "in</PageWidth>" +
        "  <PageHeight>" + PageHeight + "in</PageHeight>" +
        "  <MarginTop>" + TopMargin + "in</MarginTop>" +
        "  <MarginLeft>" + LeftMargin + "in</MarginLeft>" +
        "  <MarginRight>" + RightMargin + "in</MarginRight>" +
        "  <MarginBottom>" + BottomMargin + "in</MarginBottom>" +
        "  <HumanReadablePDF>True</HumanReadablePDF>" +
        "</DeviceInfo>";

    Warning[] warnings;
    string[] streams;

    //Render the report            
    renderedBytes = localReport.Render(
        typeName,
        deviceInfo,
        out mimeType,
        out encoding,
        out fileNameExtension,
        out streams,
        out warnings);

    RenderedBytes = renderedBytes;
}





ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.