With this source code sample you may quickly learn how to print PDF in C#. What is ByteScout PDF Renderer SDK? It is the SDK for rendering of PDF into high-quality thumbnails and images. Includes various functions like batch processing, PNG, TIFF output. Can be used from web and desktop applications. It can help you to print PDF in your C# application.
This rich sample source code in C# for ByteScout PDF Renderer SDK includes the number of functions and options you should do calling the API to print PDF. In order to implement the functionality, you should copy and paste this code for C# below into your code editor with your app, compile and run your application. Code testing will allow the function to be tested and work properly with your data.
Free trial version of ByteScout PDF Renderer SDK is available on our website. Documentation and source code samples are included.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
namespace PrintPDF { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.printDocument1 = new System.Drawing.Printing.PrintDocument(); this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog(); this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog(); this.printDialog1 = new System.Windows.Forms.PrintDialog(); this.buttonPageSetup = new System.Windows.Forms.Button(); this.buttonPrintPreview = new System.Windows.Forms.Button(); this.buttonPrint = new System.Windows.Forms.Button(); this.SuspendLayout(); // // printDocument1 // this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage); // // printPreviewDialog1 // this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0); this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0); this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300); this.printPreviewDialog1.Document = this.printDocument1; this.printPreviewDialog1.Enabled = true; this.printPreviewDialog1.Icon = ((System.Drawing.Icon) (resources.GetObject("printPreviewDialog1.Icon"))); this.printPreviewDialog1.Name = "printPreviewDialog1"; this.printPreviewDialog1.UseAntiAlias = true; this.printPreviewDialog1.Visible = false; // // pageSetupDialog1 // this.pageSetupDialog1.Document = this.printDocument1; // // printDialog1 // this.printDialog1.Document = this.printDocument1; this.printDialog1.UseEXDialog = true; // // buttonPageSetup // this.buttonPageSetup.Anchor = ((System.Windows.Forms.AnchorStyles) (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.buttonPageSetup.Location = new System.Drawing.Point(12, 12); this.buttonPageSetup.Name = "buttonPageSetup"; this.buttonPageSetup.Size = new System.Drawing.Size(253, 41); this.buttonPageSetup.TabIndex = 0; this.buttonPageSetup.Text = "Page Setup"; this.buttonPageSetup.UseVisualStyleBackColor = true; this.buttonPageSetup.Click += new System.EventHandler(this.buttonPageSetup_Click); // // buttonPrintPreview // this.buttonPrintPreview.Anchor = ((System.Windows.Forms.AnchorStyles) (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.buttonPrintPreview.Location = new System.Drawing.Point(12, 59); this.buttonPrintPreview.Name = "buttonPrintPreview"; this.buttonPrintPreview.Size = new System.Drawing.Size(253, 41); this.buttonPrintPreview.TabIndex = 1; this.buttonPrintPreview.Text = "Print Preview"; this.buttonPrintPreview.UseVisualStyleBackColor = true; this.buttonPrintPreview.Click += new System.EventHandler(this.buttonPrintPreview_Click); // // buttonPrint // this.buttonPrint.Anchor = ((System.Windows.Forms.AnchorStyles) (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.buttonPrint.Location = new System.Drawing.Point(12, 106); this.buttonPrint.Name = "buttonPrint"; this.buttonPrint.Size = new System.Drawing.Size(253, 41); this.buttonPrint.TabIndex = 2; this.buttonPrint.Text = "Print"; this.buttonPrint.UseVisualStyleBackColor = true; this.buttonPrint.Click += new System.EventHandler(this.buttonPrint_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(277, 161); this.Controls.Add(this.buttonPrint); this.Controls.Add(this.buttonPrintPreview); this.Controls.Add(this.buttonPageSetup); this.Name = "Form1"; this.Text = "Print PDF"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion private System.Drawing.Printing.PrintDocument printDocument1; private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1; private System.Windows.Forms.PageSetupDialog pageSetupDialog1; private System.Windows.Forms.PrintDialog printDialog1; private System.Windows.Forms.Button buttonPageSetup; private System.Windows.Forms.Button buttonPrintPreview; private System.Windows.Forms.Button buttonPrint; } }
60 Day Free Trial or Visit ByteScout PDF Renderer SDK Home Page
Explore ByteScout PDF Renderer SDK Documentation
Explore Samples
Sign Up for ByteScout PDF Renderer SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
using System; using System.Drawing; using System.Windows.Forms; using Bytescout.PDFRenderer; namespace PrintPDF { public partial class Form1 : Form { private string _document = @"multipage.pdf"; readonly RasterRenderer _rasterRenderer = null; private int _page = 0; public Form1() { InitializeComponent(); // Create an instance of Bytescout.PDFRenderer.RasterRenderer object and register it. _rasterRenderer = new RasterRenderer(); _rasterRenderer.RegistrationName = "demo"; _rasterRenderer.RegistrationKey = "demo"; } private void Form1_Load(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; try { // Load PDF document _rasterRenderer.LoadDocumentFromFile(_document); } catch (Exception exception) { MessageBox.Show("Could not open PDF document.\n\n" + exception.Message); } finally { Cursor = Cursors.Default; } } private void buttonPageSetup_Click(object sender, EventArgs e) { // Set landscape orientation if needed RectangleF pageRectangle = _rasterRenderer.GetPageRectangle(0); if (pageRectangle.Width > pageRectangle.Height) pageSetupDialog1.PageSettings.Landscape = true; pageSetupDialog1.ShowDialog(); } private void buttonPrintPreview_Click(object sender, EventArgs e) { _page = 0; printPreviewDialog1.Width = 800; printPreviewDialog1.Height = 600; printPreviewDialog1.ShowDialog(); } private void buttonPrint_Click(object sender, EventArgs e) { if (printDialog1.ShowDialog() == DialogResult.OK) { printDocument1.Print(); } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Cursor = Cursors.WaitCursor; try { // For the best quality set the rendering resolution equal to the printer resolution float renderingResolution = e.PageSettings.PrinterResolution.X; // Render page to image using (Image image = _rasterRenderer.GetImage(_page, renderingResolution)) { // Fit image into the print rectangle keeping the aspect ratio Rectangle printRect = e.MarginBounds; float ratio = printRect.Width / (float) image.Width; int width = printRect.Width; int height = (int) (image.Height * ratio); if (height > printRect.Height) { ratio = printRect.Height / (float) image.Height; width = (int) (image.Width * ratio); height = printRect.Height; } // Draw image on device e.Graphics.DrawImage(image, new Rectangle(printRect.X, printRect.Y, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); } if (_page < _rasterRenderer.GetPageCount() - 1) { _page++; e.HasMorePages = true; } } catch (Exception exception) { MessageBox.Show(exception.Message); } finally { Cursor = Cursors.Default; } } } }
60 Day Free Trial or Visit ByteScout PDF Renderer SDK Home Page
Explore ByteScout PDF Renderer SDK Documentation
Explore Samples
Sign Up for ByteScout PDF Renderer SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
using System; using System.Collections.Generic; using System.Windows.Forms; namespace PrintPDF { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
60 Day Free Trial or Visit ByteScout PDF Renderer SDK Home Page
Explore ByteScout PDF Renderer SDK Documentation
Explore Samples
Sign Up for ByteScout PDF Renderer SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout PDF Renderer SDK Home Page
Explore ByteScout PDF Renderer SDK Documentation
Explore Samples
Sign Up for ByteScout PDF Renderer SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples