Click here to Skip to main content
Click here to Skip to main content
Go to top

Custom Print Functionality for Microsoft Report Viewer (RDLC)

, 1 May 2012
Rate this:
Please Sign up or sign in to vote.
This tip describes how to create your own printing functionality rather than using built in print functionality on Microsoft Report Viewer.

Introduction

The following tip will guide you to create a custom printing functionality to print RDLC report rather than using the built in ActiveX print functionality.

Background

There is a major problem in print button on Microsoft Report Viewer. Once you deploy a web application, Client needs to install the ActiveX on their PC in order to print the report. It is said that installing ActiveX is not a secure practice on secured environment. The worst part is when the server/client is on automatic update, there is a security patch which disables the printing functionality completely.

Read more about ActiveX Kill Bits here.

Because of the above reasons, it is wise to have your own printing functionality rather than using the built-in one.

Using the Code

You need to download the third party component called iTextSharp.dll.

You need to export the report to PDF, then print using iTextSharp. Also client needs to install the PDF reader as well.

  1. Create hidden iFrame as follows:
    <iframe id="frmPrint" name="IframeName" width="500" 
      height="200" runat="server" 
      style="display: none" runat="server"></iframe>
  2. Add an ASP.NET button:
    <asp:ImageButton ID="btnPrint" runat="server" OnClick="btnPrint_Click"  />
  3. Add the following references:
    using iTextSharp.text.pdf;
    using iTextSharp.text;
    using System.IO;
  4. Add the following code to button click event:
    Warning[] warnings;              
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;
    
    byte[] bytes = View.ReportViewer.LocalReport.Render("PDF", null, out mimeType, 
                   out encoding, out extension, out streamids, out warnings);
                
    FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output.pdf"), 
    FileMode.Create);
    fs.Write(bytes, 0, bytes.Length);
    fs.Close();
    
    //Open existing PDF
    Document document = new Document(PageSize.LETTER);
    PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output.pdf"));
    //Getting a instance of new PDF writer
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(
       HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
    document.Open();
    PdfContentByte cb = writer.DirectContent;
    
    int i = 0;
    int p = 0;
    int n = reader.NumberOfPages;
    Rectangle psize = reader.GetPageSize(1);
     
    float width = psize.Width;             
    float height = psize.Height;
    
    //Add Page to new document
    while (i < n)
    {
       document.NewPage();
       p++;
       i++;
    
       PdfImportedPage page1 = writer.GetImportedPage(reader, i);
       cb.AddTemplate(page1, 0, 0);
    }
    
    //Attach javascript to the document
    PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
    writer.AddJavaScript(jAction);
    document.Close();
                    
    //Attach pdf to the iframe
    frmPrint.Attributes["src"] = "Print.pdf";

Points of Interest

You may need to add read/write permission on the folder where the PDF is created on the server.

History

  • May 01, 2012: Article created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chamila Ranasinghe
Software Developer
Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
QuestionError on Custom Print PinmemberMember 1101475119-Sep-14 0:04 
AnswerRe: Error on Custom Print PinmemberChamila Ranasinghe19-Sep-14 16:12 
QuestionHow to set the printer orientation? PinmemberNanda Kumar ps14-Aug-14 6:35 
QuestionFind Answer Pinmembersushilsharma997-Nov-13 2:16 
QuestionMy vote of 5 stars PinmemberCarlo Ingrassia6-Nov-13 21:03 
Questionarbaic letters printed boxess Pinmembermah2eng26-Sep-13 9:05 
Questionarbicletter printed boxes Pinmembermah2eng26-Sep-13 9:02 
QuestionError PinmemberBhavika Shah23-Jul-13 20:29 
AnswerRe: Error PinmemberChamila Ranasinghe23-Jul-13 21:40 
GeneralRe: Error PinmemberBhavika Shah24-Jul-13 0:24 
GeneralRe: Error PinmemberChamila Ranasinghe25-Jul-13 15:44 
GeneralRe: Error PinmemberMURALIBALA199126-Jan-14 22:05 
GeneralMy vote of 5 Pinmemberankurrajput24-Jan-13 0:44 
Questionprint problem Pinmemberpratik chavan25-Oct-12 21:14 
Hi,
I use this Code on multiple Pages but its printing same pdf content on each and every page ... can you please help to resolve this issue....
Thanks in advance
AnswerRe: print problem PinmemberChamila Ranasinghe25-Oct-12 21:25 
QuestionCode not working in fire fox Pinmemberabhis22tr5-Jun-12 0:41 
AnswerRe: Code not working in fire fox PinmemberChamila Ranasinghe5-Jun-12 18:28 
GeneralRe: Code not working in fire fox PinmemberPratikme31-Mar-14 23:47 
GeneralMy vote of 4 PinmemberJP®29-May-12 1:54 
GeneralRe: My vote of 4 PinmemberChamila Ranasinghe29-May-12 18:03 
GeneralThanks Pinmembercosmicsoundz2-May-12 9:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 1 May 2012
Article Copyright 2012 by Chamila Ranasinghe
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid