How to print PDF with PDF Renderer SDK in C# and VB.NET

The following samples show how to print PDF from your applications in C# and Visual Basic .NET using Bytescout PDF Renderer SDK.


C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
 
using Bytescout.PDFRenderer;
 
 
namespace PrintPDF
{
	public partial class Form1 : Form
	{
		private string _document = @"multipage.pdf";
		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("Failed to open PDF document.\r\n" + exception.Message);
			}
			finally
			{
				Cursor = Cursors.Default;
			}
		}
 
		private void buttonPageSetup_Click(object sender, EventArgs e)
		{
			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
			{
				using (VectorRenderer vectorRenderer = new VectorRenderer("demo", "demo"))
				{
					vectorRenderer.LoadDocumentFromFile(_document);
 
					Rectangle printRect = e.MarginBounds;
 
					using (Metafile image = vectorRenderer.RenderPageToMetafile(_page))
					{
						// calculate print rectangle
						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 page 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 < vectorRenderer.GetPageCount() - 1)
					{
						_page++;
						e.HasMorePages = true;
					}
				}
			}
			finally
			{
				Cursor = Cursors.Default;
			}
		}
	}
}

 

VB.NET

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Text
Imports System.Windows.Forms
 
Imports Bytescout.PDFRenderer
 
 
Public Partial Class Form1
	Inherits Form
	Private _document As String = "multipage.pdf"
	Private _rasterRenderer As RasterRenderer = Nothing
	Private _page As Integer = 0
 
 
	Public Sub New()
		InitializeComponent()
 
		' Create an instance of Bytescout.PDFRenderer.RasterRenderer object and register it.
		_rasterRenderer = New RasterRenderer()
		_rasterRenderer.RegistrationName = "demo"
		_rasterRenderer.RegistrationKey = "demo"
	End Sub
 
	Private Sub Form1_Load(sender As Object, e As EventArgs)
		Cursor = Cursors.WaitCursor
 
		Try
			' Load PDF document.
			_rasterRenderer.LoadDocumentFromFile(_document)
		Catch exception As Exception
			MessageBox.Show("Failed to open PDF document." & vbCr & vbLf & exception.Message)
		Finally
			Cursor = Cursors.[Default]
		End Try
	End Sub
 
	Private Sub buttonPageSetup_Click(sender As Object, e As EventArgs)
		pageSetupDialog1.ShowDialog()
	End Sub
 
	Private Sub buttonPrintPreview_Click(sender As Object, e As EventArgs)
		_page = 0
		printPreviewDialog1.Width = 800
		printPreviewDialog1.Height = 600
		printPreviewDialog1.ShowDialog()
	End Sub
 
	Private Sub buttonPrint_Click(sender As Object, e As EventArgs)
		If printDialog1.ShowDialog() = DialogResult.OK Then
			printDocument1.Print()
		End If
	End Sub
 
	Private Sub printDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs)
		Cursor = Cursors.WaitCursor
 
		Try
			Using vectorRenderer As New VectorRenderer("demo", "demo")
				vectorRenderer.LoadDocumentFromFile(_document)
 
				Dim printRect As Rectangle = e.MarginBounds
 
				Using image As Metafile = vectorRenderer.RenderPageToMetafile(_page)
					' calculate print rectangle
					Dim ratio As Single = printRect.Width / CSng(image.Width)
					Dim width As Integer = printRect.Width
					Dim height As Integer = CInt(Math.Truncate(image.Height * ratio))
 
					If height > printRect.Height Then
						ratio = printRect.Height / CSng(image.Height)
						width = CInt(Math.Truncate(image.Width * ratio))
						height = printRect.Height
					End If
 
					' draw page on device
					e.Graphics.DrawImage(image, New Rectangle(printRect.X, printRect.Y, width, height), New Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel)
				End Using
 
				If _page < vectorRenderer.GetPageCount() - 1 Then
					_page += 1
					e.HasMorePages = True
				End If
			End Using
		Finally
			Cursor = Cursors.[Default]
		End Try
	End Sub
End Class
Filed in: PDF Renderer SDK