ByteScout PDF Renderer SDK - VB.NET - Print PDF - ByteScout

ByteScout PDF Renderer SDK – VB.NET – Print PDF

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF Renderer SDK – VB.NET – Print PDF

How to print PDF in VB.NET using ByteScout PDF Renderer SDK

The tutorial below will demonstrate how to print PDF in VB.NET

Every ByteScout tool contains example VB.NET source codes that you can find here or in the folder with installed ByteScout product. ByteScout PDF Renderer SDK is the software development kit for rendering of PDF files into high quality images or thumbnails. Various functions are included as well: batch processing, PNG, TIFF output, selection area to render, layers management to render text or images or vectors only. Designed for use from web, desktop and middleware applications and you can use it to print PDF with VB.NET.

Fast application programming interfaces of ByteScout PDF Renderer SDK for VB.NET plus the instruction and the code below will help you quickly learn how to print PDF. In order to implement the functionality, you should copy and paste this code for VB.NET below into your code editor with your app, compile and run your application. This basic programming language sample code for VB.NET will do the whole work for you to print PDF.

ByteScout free trial version is available for download from our website. It includes all these programming tutorials along with source code samples.

Try ByteScout PDF Renderer SDK today: Get 60 Day Free Trial or sign up for Web API

Partial Class Form1 ''' <summary> ''' Required designer variable. ''' </summary> Private components As System.ComponentModel.IContainer = Nothing ''' <summary> ''' Clean up any resources being used. ''' </summary> ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> Protected Overrides Sub Dispose(disposing As Boolean) If disposing AndAlso (components IsNot Nothing) Then components.Dispose() End If MyBase.Dispose(disposing) End Sub #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 Sub InitializeComponent() Dim resources As New System.ComponentModel.ComponentResourceManager(GetType(Form1)) Me.printDocument1 = New System.Drawing.Printing.PrintDocument() Me.printPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog() Me.pageSetupDialog1 = New System.Windows.Forms.PageSetupDialog() Me.printDialog1 = New System.Windows.Forms.PrintDialog() Me.buttonPageSetup = New System.Windows.Forms.Button() Me.buttonPrintPreview = New System.Windows.Forms.Button() Me.buttonPrint = New System.Windows.Forms.Button() Me.SuspendLayout() ' ' printDocument1 ' AddHandler Me.printDocument1.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf Me.printDocument1_PrintPage) ' ' printPreviewDialog1 ' Me.printPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0) Me.printPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0) Me.printPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300) Me.printPreviewDialog1.Document = Me.printDocument1 Me.printPreviewDialog1.Enabled = True Me.printPreviewDialog1.Icon = DirectCast(resources.GetObject("printPreviewDialog1.Icon"), System.Drawing.Icon) Me.printPreviewDialog1.Name = "printPreviewDialog1" Me.printPreviewDialog1.UseAntiAlias = True Me.printPreviewDialog1.Visible = False ' ' pageSetupDialog1 ' Me.pageSetupDialog1.Document = Me.printDocument1 ' ' printDialog1 ' Me.printDialog1.Document = Me.printDocument1 Me.printDialog1.UseEXDialog = True ' ' buttonPageSetup ' Me.buttonPageSetup.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.buttonPageSetup.Location = New System.Drawing.Point(12, 12) Me.buttonPageSetup.Name = "buttonPageSetup" Me.buttonPageSetup.Size = New System.Drawing.Size(253, 41) Me.buttonPageSetup.TabIndex = 0 Me.buttonPageSetup.Text = "Page Setup" Me.buttonPageSetup.UseVisualStyleBackColor = True AddHandler Me.buttonPageSetup.Click, New System.EventHandler(AddressOf Me.buttonPageSetup_Click) ' ' buttonPrintPreview ' Me.buttonPrintPreview.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.buttonPrintPreview.Location = New System.Drawing.Point(12, 59) Me.buttonPrintPreview.Name = "buttonPrintPreview" Me.buttonPrintPreview.Size = New System.Drawing.Size(253, 41) Me.buttonPrintPreview.TabIndex = 1 Me.buttonPrintPreview.Text = "Print Preview" Me.buttonPrintPreview.UseVisualStyleBackColor = True AddHandler Me.buttonPrintPreview.Click, New System.EventHandler(AddressOf Me.buttonPrintPreview_Click) ' ' buttonPrint ' Me.buttonPrint.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.buttonPrint.Location = New System.Drawing.Point(12, 106) Me.buttonPrint.Name = "buttonPrint" Me.buttonPrint.Size = New System.Drawing.Size(253, 41) Me.buttonPrint.TabIndex = 2 Me.buttonPrint.Text = "Print" Me.buttonPrint.UseVisualStyleBackColor = True AddHandler Me.buttonPrint.Click, New System.EventHandler(AddressOf Me.buttonPrint_Click) ' ' Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(277, 161) Me.Controls.Add(Me.buttonPrint) Me.Controls.Add(Me.buttonPrintPreview) Me.Controls.Add(Me.buttonPageSetup) Me.Name = "Form1" Me.Text = "Print PDF" AddHandler Me.Load, New System.EventHandler(AddressOf Me.Form1_Load) Me.ResumeLayout(False) End Sub #End Region Private printDocument1 As System.Drawing.Printing.PrintDocument Private printPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog Private pageSetupDialog1 As System.Windows.Forms.PageSetupDialog Private printDialog1 As System.Windows.Forms.PrintDialog Private buttonPageSetup As System.Windows.Forms.Button Private buttonPrintPreview As System.Windows.Forms.Button Private buttonPrint As System.Windows.Forms.Button End Class

Try ByteScout PDF Renderer SDK today: 60 Day Free Trial (on-premise version) or sign up for Web API (on demand version)

Imports System.Drawing Imports System.Drawing.Imaging 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("Could not open PDF document." & vbLf & 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 _rasterRenderer.LoadDocumentFromFile(_document) ' For the best quality set the rendering resoultion equal to the printer resoultion Dim renderingResolution As Single = e.PageSettings.PrinterResolution.X ' Render page to image Using image As Image = _rasterRenderer.GetImage(_page, renderingResolution) ' Fit image into the print rectangle keeping the aspect ratio Dim printRect As Rectangle = e.MarginBounds Dim ratio As Single = printRect.Width / CSng(image.Width) Dim width As Integer = printRect.Width Dim height As Integer = CInt(Math.Round(image.Height * ratio)) If height > printRect.Height Then ratio = printRect.Height / CSng(image.Height) width = CInt(Math.Round(image.Width * ratio)) height = printRect.Height End If ' 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) End Using If _page < _rasterRenderer.GetPageCount() - 1 Then _page += 1 e.HasMorePages = True End If Catch exception As Exception MessageBox.Show(exception.Message) Finally Cursor = Cursors.Default End Try End Sub End Class

Try ByteScout PDF Renderer SDK today: 60 Day Free Trial (on-premise version) or sign up for Web API (on demand version)

Imports System.Collections.Generic Imports System.Windows.Forms NotInheritable Class Program Private Sub New() End Sub ''' <summary> ''' The main entry point for the application. ''' </summary> <STAThread> _ Friend Shared Sub Main() Application.EnableVisualStyles() Application.SetCompatibleTextRenderingDefault(False) Application.Run(New Form1()) End Sub End Class

Try ByteScout PDF Renderer SDK today: 60 Day Free Trial (on-premise version) or sign up for Web API (on demand version)



Get 60 Day Free Trial or Visit ByteScout PDF Renderer SDK page

Explore ByteScout PDF Renderer SDK documentation


Sign Up for free Web API key

Explore Web API Documentation