Click here to Skip to main content
13,259,771 members (47,855 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 1 May 2012

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.


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.


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"), 
    fs.Write(bytes, 0, bytes.Length);
    //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));
    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)
       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);
    //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.


  • May 01, 2012: Article created


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


About the Author

Chamila Ranasinghe
Software Developer
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionNice! Thank you! Pin
Ignacioooooo17-Aug-17 4:33
memberIgnacioooooo17-Aug-17 4:33 
QuestionImages Print Pin
Member 9354253-Nov-15 4:09
memberMember 9354253-Nov-15 4:09 
Questionlanguage change Pin
Member 1130732922-Dec-14 21:23
memberMember 1130732922-Dec-14 21:23 
Questionplease help Pin
Member 1130732915-Dec-14 1:37
memberMember 1130732915-Dec-14 1:37 
QuestionError on Custom Print Pin
Member 1101475119-Sep-14 1:04
memberMember 1101475119-Sep-14 1:04 
AnswerRe: Error on Custom Print Pin
Chamila Ranasinghe19-Sep-14 17:12
memberChamila Ranasinghe19-Sep-14 17:12 
QuestionHow to set the printer orientation? Pin
Nanda Kumar ps14-Aug-14 7:35
memberNanda Kumar ps14-Aug-14 7:35 
QuestionFind Answer Pin
sushilsharma997-Nov-13 3:16
membersushilsharma997-Nov-13 3:16 
QuestionMy vote of 5 stars Pin
Carlo Ingrassia6-Nov-13 22:03
memberCarlo Ingrassia6-Nov-13 22:03 
Questionarbaic letters printed boxess Pin
mah2eng26-Sep-13 10:05
membermah2eng26-Sep-13 10:05 
Questionarbicletter printed boxes Pin
mah2eng26-Sep-13 10:02
membermah2eng26-Sep-13 10:02 
QuestionError Pin
Bhavika Shah23-Jul-13 21:29
memberBhavika Shah23-Jul-13 21:29 
AnswerRe: Error Pin
Chamila Ranasinghe23-Jul-13 22:40
memberChamila Ranasinghe23-Jul-13 22:40 
GeneralRe: Error Pin
Bhavika Shah24-Jul-13 1:24
memberBhavika Shah24-Jul-13 1:24 
GeneralRe: Error Pin
Chamila Ranasinghe25-Jul-13 16:44
memberChamila Ranasinghe25-Jul-13 16:44 
GeneralRe: Error Pin
MURALIBALA199126-Jan-14 23:05
memberMURALIBALA199126-Jan-14 23:05 
AnswerRe: Error Pin
Member 1130732925-Dec-14 20:14
memberMember 1130732925-Dec-14 20:14 
GeneralMy vote of 5 Pin
ankurrajput24-Jan-13 1:44
memberankurrajput24-Jan-13 1:44 
Questionprint problem Pin
pratik chavan25-Oct-12 22:14
memberpratik chavan25-Oct-12 22:14 
AnswerRe: print problem Pin
Chamila Ranasinghe25-Oct-12 22:25
memberChamila Ranasinghe25-Oct-12 22:25 
QuestionCode not working in fire fox Pin
abhis22tr5-Jun-12 1:41
memberabhis22tr5-Jun-12 1:41 
AnswerRe: Code not working in fire fox Pin
Chamila Ranasinghe5-Jun-12 19:28
memberChamila Ranasinghe5-Jun-12 19:28 
GeneralRe: Code not working in fire fox Pin
Pratikme1-Apr-14 0:47
memberPratikme1-Apr-14 0:47 
GeneralRe: Code not working in fire fox Pin
Member 43117162-Aug-15 21:21
memberMember 43117162-Aug-15 21:21 
GeneralRe: Code not working in fire fox Pin
Chamila Ranasinghe3-Aug-15 3:07
memberChamila Ranasinghe3-Aug-15 3:07 
GeneralMy vote of 4 Pin
JP®29-May-12 2:54
memberJP®29-May-12 2:54 
Nice article.
GeneralRe: My vote of 4 Pin
Chamila Ranasinghe29-May-12 19:03
memberChamila Ranasinghe29-May-12 19:03 
GeneralThanks Pin
cosmicsoundz2-May-12 10:38
membercosmicsoundz2-May-12 10:38 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 1 May 2012
Article Copyright 2012 by Chamila Ranasinghe
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid