Link Search Menu Expand Document

Print PDF - VB.NET

PDF Renderer SDK sample in VB.NET demonstrating ‘Print PDF’

Form1.Designer.vb
��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




Form1.vb
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(ByVal sender As Object, ByVal e As EventArgs)

        ' Set landscape orientation if needed
        Dim pageRectangle As RectangleF = _rasterRenderer.GetPageRectangle(0)
        If (pageRectangle.Width > pageRectangle.Height) Then
            pageSetupDialog1.PageSettings.Landscape = True
        End If

        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
            ' For the best quality set the rendering resolution equal to the printer resolution
            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

Program.vb
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

Resources.Designer.vb
��'------------------------------------------------------------------------------

' <auto-generated>

'     This code was generated by a tool.

'     Runtime Version:2.0.50727.4952

'

'     Changes to this file may cause incorrect behavior and will be lost if

'     the code is regenerated.

' </auto-generated>

'------------------------------------------------------------------------------



Namespace Properties





	''' <summary>

	'''   A strongly-typed resource class, for looking up localized strings, etc.

	''' </summary>

	' This class was auto-generated by the StronglyTypedResourceBuilder

	' class via a tool like ResGen or Visual Studio.

	' To add or remove a member, edit your .ResX file then rerun ResGen

	' with the /str option, or rebuild your VS project.

	<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")> _

	<System.Diagnostics.DebuggerNonUserCodeAttribute> _

	<System.Runtime.CompilerServices.CompilerGeneratedAttribute> _

	Friend Class Resources



		Private Shared resourceMan As Global.System.Resources.ResourceManager



		Private Shared resourceCulture As Global.System.Globalization.CultureInfo



		<System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")> _

		Friend Sub New()

		End Sub



		''' <summary>

		'''   Returns the cached ResourceManager instance used by this class.

		''' </summary>

		<System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _

		Friend Shared ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager

			Get

				If (resourceMan Is Nothing) Then

					Dim temp As New Global.System.Resources.ResourceManager("PrintPDF.Properties.Resources", GetType(Resources).Assembly)

					resourceMan = temp

				End If

				Return resourceMan

			End Get

		End Property



		''' <summary>

		'''   Overrides the current thread's CurrentUICulture property for all

		'''   resource lookups using this strongly typed resource class.

		''' </summary>

		<System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _

		Friend Shared Property Culture() As Global.System.Globalization.CultureInfo

			Get

				Return resourceCulture

			End Get

			Set

				resourceCulture = value

			End Set

		End Property

	End Class

End Namespace


Settings.Designer.vb
��'------------------------------------------------------------------------------

' <auto-generated>

'     This code was generated by a tool.

'     Runtime Version:2.0.50727.4952

'

'     Changes to this file may cause incorrect behavior and will be lost if

'     the code is regenerated.

' </auto-generated>

'------------------------------------------------------------------------------



Namespace Properties





	<System.Runtime.CompilerServices.CompilerGeneratedAttribute> _

	<System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "8.0.0.0")> _

	Friend NotInheritable Partial Class Settings

		Inherits Global.System.Configuration.ApplicationSettingsBase



		Private Shared defaultInstance As Settings = DirectCast(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New Settings()), Settings)



		Public Shared ReadOnly Property [Default]() As Settings

			Get

				Return defaultInstance

			End Get

		End Property

	End Class

End Namespace


Download Source Code (.zip)

Return to the previous page Explore PDF Renderer SDK


Copyright © 2016 - 2021 ByteScout