Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Windows Controls C# » PrintPreviewControl in C#

PrintPreviewControl in C#

The PrintPreviewControl control represents the raw part of the print preview of a document that is ready go to the printer. In this article, I will discuss how to use a PrintPreviewControl control to display print previews of documents in Windows Forms applications.

Author Rank :
Page Views : 14621
Downloads : 402
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
PrintPreviewControlSampleCSharp.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


PrintPreviewControl
The PrintPreviewDialog control displays the Print Preview Dialog with all buttons and settings so the users can change their settings before a document goes to the printer. But there are circumstances when you would need to just display print preview without any buttons and options and show only the preview. This is where the PrintPreviewControl can be used.
The PrintPreviewControl control represents the raw part of print preview of a document that is ready go to the printer. In this article, I will discuss how to use a PrintPreviewControl control to display print previews of documents in Windows Forms applications.

Creating a PrintPreviewControl

PrintPreviewControl is defined in the System.Drawing.Printing namespace. You must import this namespace before using the PrintPreviewControl class.

We can create a PrintPreviewControl by dragging the control from the Toolbox onto a Form or we can use the PrintPreviewControl class.

The following code snippet creates a PrintPreviewControl, sets it's properties and adds control to the Form by calling Controls.Add() method.

C# Code:

private PrintPreviewControl ppc;

private PrintDocument docToPrint = new PrintDocument();      

 

private void CreatePrintPreviewControl()

{

    ppc = new PrintPreviewControl();               

    ppc.Name = "PrintPreviewControl1";

    ppc.Dock = DockStyle.Fill;

    ppc.Location = new Point(88, 80);           

    ppc.Document = docToPrint;

    ppc.Zoom = 0.25;

    ppc.Document.DocumentName = "c:\\";

    ppc.UseAntiAlias = true;

 

    // Add PrintPreviewControl to Form

    Controls.Add(this.ppc);           

}

 

VB.NET Code:

 

 

Private Sub CreatePrintPreviewControl()

        ppc = New PrintPreviewControl()

        ppc.Name = "PrintPreviewControl1"

        ppc.Dock = DockStyle.Fill

        ppc.Location = New Point(88, 80)

        ppc.Document = docToPrint

        ppc.Zoom = 0.25

        ppc.Document.DocumentName = "c:\"

        ppc.UseAntiAlias = True

 

        AddHandler docToPrint.PrintPage, AddressOf PrintPage

 

        ' Add PrintPreviewControl to Form

        Controls.Add(ppc)

    End Sub

Adding the PrintDocument.PrintPage Event Handler

Remember, a PrintPreviewControl displays preview only and does not print a document. To print a document, we must implement the PrintDocument.PrintPage event handler.  The following code snippet adds a PrintDocument.PrintPage event handler and prints some text using the DrawString method of the Graphics object.

C# Code:                                                                                                                                  

// Add PrintDocument PrintPage event handler

this.docToPrint.PrintPage +=

    new System.Drawing.Printing.PrintPageEventHandler(PrintPage);

 

private void PrintPage(object sender, PrintPageEventArgs e)

{

    string text = "This text to be printed. ";

    e.Graphics.DrawString(text, new Font("Georgia", 35, FontStyle.Bold),

        Brushes.Black, 10, 10);

}

 

VB.NET Code:

 

AddHandler docToPrint.PrintPage, AddressOf PrintPage

 

Private Sub PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)

        Dim text As String = "This text to be printed. "

        e.Graphics.DrawString(text, New Font("Georgia", 35, FontStyle.Bold),

          Brushes.Black, 10, 10)

End Sub

 

The output looks like Figure 1 where the print preview of a document to be printed is displayed.

PrintPreviewImg1.jpg

Summary
In this article, we discussed how to use a PrintPreviewControl control to display print preview of documents in Windows Forms applications. Download the attached project to learn more about this control.

Further Readings

Here is a list of top articles and tutorials about printing you will want to read to learn more about printing.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Thnx by Ankit On March 9, 2011
great article
Reply | Email | Modify 
Re : Print Preview Control by sagar On June 7, 2011
Mahesh,Great article... I have one query regarding this... If we have .doc or .pdf format file then how can we show in print preview control ? Thanks in advance...
Reply | Email | Modify 
Print Previews DefaultPageSettings.Landscape =true is not working by Varinder On November 16, 2011
hello Sir, I have following problem Print Previews DefaultPageSettings.Landscape =true is not working Even after setting true it is showing preview in Portrait . Please help me how can i achieve this. thanks.
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.