How to Zoom PDF File via PDFViewer in C#, VB.NET

2012-06-08 02:33:33 Written by  support iceblue
Rate this item
(1 Vote)

Zoom function in PDF file can help people change the text size of PDF document page according to their view preference or need. When you want to increase the percentage of the PDF file text, you can click the button of Zoom Out or choose the percentage number directly as well as scroll down your mouse. And also if you want to zoom back to the original text size, you can click the Actual Size button. Actually, you can completely control the text percentage of the PDF document, which is much more convenient than the zoom of Excel and Word files. Now it is time to learn how to zoom PDF file via PDF Viewer with C#, VB.NET.

In this article, I will introduce seven kinds of zoom to control PDF file text size by using a Spire.PDFViewer. Spire.PDFViewer does NOT require Adobe Reader or any other 3rd party software/library installed on system. You can have a Freely Trial of Spire.PDFViewer. The whole procedure can be finished by below steps.

Step 1: Create a new project

  • Create a new project in Windows Forms Application.
  • Set the Target Framework to be .NET Framework 2 or above in Properties.
  • Add a toolScript and pdfDocumentViewer in Form1 from Toolbox. Then, add seven buttons, two ComboBoxes and a label from the toolScript dropdown list in Form1.
  • Set the Properties of the tools respectively. Such as the "Name", "Display Style", "Text" and "ToolTipText" of buttons, ComboBoxes and label. You can set the Dock property of pdfDocumentViewer in its Properties in order to view the PDF in enough space.

Step 2: Methods to zoom PDF file via Spire.PDFViewer

  • Add Spire. PDFViewer.Forms dll as reference.
  • Add below namespace at the top of the method.
[C#]
using System.IO;
using System.Windows.Forms;
using Spire.PdfViewer.Forms;
[Visual Basic]
Imports System.IO
Imports System.Windows.Forms
Imports Spire.PdfViewer.Forms

Zoom PDF file. In this step, I have seven kinds of zoom functions to control the PDF text size. They are:

  • Zoom: Manually choose the percentage.
  • Zoom Out: Decrease the PDF text size.
  • Zoom In: Increase the PDF text size.
  • Zoom Dynamic: Scroll down/up the mouse directly to change the text size. A second click can cancel zoom dynamic.
  • Actual Size: When you click it, the document text changes to the original size.
  • Fit Page: Fit Page controls the PDF page not going across the PDF page height and margin.
  • Fit Width: When you click the Fit Width button, the margin of PDF document disappears and you only see the PDF text content.

Main Code:

[C#]
namespace pdfzoom
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //private bool _isZoomout = true;

        private int _zoom = 100;
        private bool _isZoomDynamic = false;

        private void Form1_Load(object sender, EventArgs e)
        {
            string pdfDoc = @"D:\michelle\e-iceblue\Spire.PDFViewer\Demos\Data\PDFViewer.pdf";
            if (File.Exists(pdfDoc))
            {
                this.pdfDocumentViewer1.LoadFromFile(pdfDoc);
            }

            //add zoom values to combox
            int[] intZooms = new Int32[] { 25, 50, 75, 100, 125, 150, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
            foreach (int zoom in intZooms)
            {
                this.comBoxZoom.Items.Add(zoom.ToString());
            }
            this.comBoxZoom.SelectedIndex = 3;

            //pdfDocumentViewer mouseWheel event

            this.pdfDocumentViewer1.MouseWheel += new MouseEventHandler(this.pdfDocumentViewer1_MouseWheel);
            this.pdfDocumentViewer1.LostFocus += new EventHandler(this.pdfDocumentViewer_LostFocus);

        }
        private void pdfDocumentViewer_LostFocus(Object sender, EventArgs args)
        {
            this._isZoomDynamic = false;
            this._zoom = 100;
        }

        private void pdfDocumentViewer1_MouseWheel(Object sender, MouseEventArgs args)
        {
            if (this._isZoomDynamic)
            {
                int wheelValue = (Int32)args.Delta / 24;


                this._zoom += wheelValue;

                if (this._zoom < 0)
                    this._zoom = 0;
                this.pdfDocumentViewer1.ZoomTo(this._zoom);
            }

        }

        private void btnOPen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "PDF document (*.pdf)|*.pdf";

            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    string pdfFile = dialog.FileName;
                    this.pdfDocumentViewer1.LoadFromFile(pdfFile);

                }
                catch (Exception exe)
                {
                    MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void comBoxZoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int zoomValue = Int32.Parse(this.comBoxZoom.SelectedItem.ToString());
                this.pdfDocumentViewer1.ZoomTo(zoomValue);
            }
        }

        private void btnZoomOut_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int delta = 10;
                this._zoom += delta;
                this.pdfDocumentViewer1.ZoomTo(this._zoom);
            }
        }

        private void btnZoonIn_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int delta = 5;
                this._zoom -= delta;
                if (this._zoom < 0)
                    this._zoom = 0;
                this.pdfDocumentViewer1.ZoomTo(this._zoom);
            }
        }

        private void btnActural_Click(object sender, EventArgs e)
        {
            this._zoom = 100;
            this._isZoomDynamic = false;
            this.btnDynamic.Text = "Zoom Dynamic";
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                this.pdfDocumentViewer1.ZoomTo(100);
                this.comBoxZoom.SelectedIndex = 3;
            }
        }

        private void btnFitPage_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                this.pdfDocumentViewer1.ZoomTo(ZoomMode.FitPage);
            }
        }

        private void btnFitWidth_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                this.pdfDocumentViewer1.ZoomTo(ZoomMode.FitWidth);
            }
        }

        private void btnDynamic_Click(object sender, EventArgs e)
        {
            this._isZoomDynamic = !this._isZoomDynamic;
            if (this._isZoomDynamic)
            {

                this.btnDynamic.Text = "Cancel dynamic zoom";
                this.btnDynamic.ToolTipText = "Cancel dynamic zoom";
            }

            else
            {
                this.btnDynamic.Text = "Zoom dynamic";
                this.btnDynamic.ToolTipText = "Zoom dynamic";
            }

        }

        private void pdfDocumentViewer1_PdfLoaded(object sender, EventArgs args)
        {
            this.btnDynamic.Enabled = true;
            this.comBoxPages.Items.Clear();
            int totalPage = this.pdfDocumentViewer1.PageCount;

            for (int i = 1; i <= totalPage; i++)
            {
                this.comBoxPages.Items.Add(i.ToString());
            }

            this.comBoxPages.SelectedIndex = 0;
        }

        private void pdfDocumentViewer1_PageNumberChanged(object sender, EventArgs args)
        {
            if (this.comBoxPages.Items.Count <= 0)
                return;
            if (this.pdfDocumentViewer1.CurrentPageNumber != this.comBoxPages.SelectedIndex + 1)
            {
                this.comBoxPages.SelectedIndex = this.pdfDocumentViewer1.CurrentPageNumber - 1;
            }
        }

        private void comBoxPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            int soucePage = this.pdfDocumentViewer1.CurrentPageNumber;
            int targetPage = this.comBoxPages.SelectedIndex + 1;
            if (soucePage != targetPage)
            {
                this.pdfDocumentViewer1.GoToPage(targetPage);
            }
        }

        private void comBoxPages_Click(object sender, EventArgs e)
        {

        }

    }
}
[VB.NET]
Namespace pdfzoom
	Public Partial Class Form1
		Inherits Form
		Public Sub New()
			InitializeComponent()
		End Sub

		'private bool _isZoomout = true;

		Private _zoom As Integer = 100
		Private _isZoomDynamic As Boolean = False

		Private Sub Form1_Load(sender As Object, e As EventArgs)
			Dim pdfDoc As String = "D:\michelle\e-iceblue\Spire.PDFViewer\Demos\Data\PDFViewer.pdf"
			If File.Exists(pdfDoc) Then
				Me.pdfDocumentViewer1.LoadFromFile(pdfDoc)
			End If

			'add zoom values to combox
			Dim intZooms As Integer() = New Int32() {25, 50, 75, 100, 125, 150, _
				200, 300, 400, 500, 600, 700, _
				800, 900, 1000}
			For Each zoom As Integer In intZooms
				Me.comBoxZoom.Items.Add(zoom.ToString())
			Next
			Me.comBoxZoom.SelectedIndex = 3

			'pdfDocumentViewer mouseWheel event

			Me.pdfDocumentViewer1.MouseWheel += New MouseEventHandler(AddressOf Me.pdfDocumentViewer1_MouseWheel)
			Me.pdfDocumentViewer1.LostFocus += New EventHandler(AddressOf Me.pdfDocumentViewer_LostFocus)

		End Sub
		Private Sub pdfDocumentViewer_LostFocus(sender As [Object], args As EventArgs)
			Me._isZoomDynamic = False
			Me._zoom = 100
		End Sub

		Private Sub pdfDocumentViewer1_MouseWheel(sender As [Object], args As MouseEventArgs)
			If Me._isZoomDynamic Then
				Dim wheelValue As Integer = DirectCast(args.Delta, Int32) / 24


				Me._zoom += wheelValue

				If Me._zoom < 0 Then
					Me._zoom = 0
				End If
				Me.pdfDocumentViewer1.ZoomTo(Me._zoom)
			End If

		End Sub

		Private Sub btnOPen_Click(sender As Object, e As EventArgs)
			Dim dialog As New OpenFileDialog()
			dialog.Filter = "PDF document (*.pdf)|*.pdf"

			Dim result As DialogResult = dialog.ShowDialog()
			If result = DialogResult.OK Then
				Try
					Dim pdfFile As String = dialog.FileName

					Me.pdfDocumentViewer1.LoadFromFile(pdfFile)
				Catch exe As Exception
					MessageBox.Show(exe.Message, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.[Error])

				End Try
			End If
		End Sub

		Private Sub comBoxZoom_SelectedIndexChanged(sender As Object, e As EventArgs)
			If Me.pdfDocumentViewer1.PageCount > 0 Then
				Dim zoomValue As Integer = Int32.Parse(Me.comBoxZoom.SelectedItem.ToString())
				Me.pdfDocumentViewer1.ZoomTo(zoomValue)
			End If
		End Sub

		Private Sub btnZoomOut_Click(sender As Object, e As EventArgs)
			If Me.pdfDocumentViewer1.PageCount > 0 Then
				Dim delta As Integer = 10
				Me._zoom += delta
				Me.pdfDocumentViewer1.ZoomTo(Me._zoom)
			End If
		End Sub

		Private Sub btnZoonIn_Click(sender As Object, e As EventArgs)
			If Me.pdfDocumentViewer1.PageCount > 0 Then
				Dim delta As Integer = 5
				Me._zoom -= delta
				If Me._zoom < 0 Then
					Me._zoom = 0
				End If
				Me.pdfDocumentViewer1.ZoomTo(Me._zoom)
			End If
		End Sub

		Private Sub btnActural_Click(sender As Object, e As EventArgs)
			Me._zoom = 100
			Me._isZoomDynamic = False
			Me.btnDynamic.Text = "Zoom Dynamic"
			If Me.pdfDocumentViewer1.PageCount > 0 Then
				Me.pdfDocumentViewer1.ZoomTo(100)
				Me.comBoxZoom.SelectedIndex = 3
			End If
		End Sub

		Private Sub btnFitPage_Click(sender As Object, e As EventArgs)
			If Me.pdfDocumentViewer1.PageCount > 0 Then
				Me.pdfDocumentViewer1.ZoomTo(ZoomMode.FitPage)
			End If
		End Sub

		Private Sub btnFitWidth_Click(sender As Object, e As EventArgs)
			If Me.pdfDocumentViewer1.PageCount > 0 Then
				Me.pdfDocumentViewer1.ZoomTo(ZoomMode.FitWidth)
			End If
		End Sub

		Private Sub btnDynamic_Click(sender As Object, e As EventArgs)
			Me._isZoomDynamic = Not Me._isZoomDynamic
			If Me._isZoomDynamic Then

				Me.btnDynamic.Text = "Cancel dynamic zoom"
				Me.btnDynamic.ToolTipText = "Cancel dynamic zoom"
			Else

				Me.btnDynamic.Text = "Zoom dynamic"
				Me.btnDynamic.ToolTipText = "Zoom dynamic"
			End If

		End Sub

		Private Sub pdfDocumentViewer1_PdfLoaded(sender As Object, args As EventArgs)
			Me.btnDynamic.Enabled = True
			Me.comBoxPages.Items.Clear()
			Dim totalPage As Integer = Me.pdfDocumentViewer1.PageCount

			For i As Integer = 1 To totalPage
				Me.comBoxPages.Items.Add(i.ToString())
			Next

			Me.comBoxPages.SelectedIndex = 0
		End Sub

		Private Sub pdfDocumentViewer1_PageNumberChanged(sender As Object, args As EventArgs)
			If Me.comBoxPages.Items.Count <= 0 Then
				Return
			End If
			If Me.pdfDocumentViewer1.CurrentPageNumber <> Me.comBoxPages.SelectedIndex + 1 Then
				Me.comBoxPages.SelectedIndex = Me.pdfDocumentViewer1.CurrentPageNumber - 1
			End If
		End Sub

		Private Sub comBoxPages_SelectedIndexChanged(sender As Object, e As EventArgs)
			Dim soucePage As Integer = Me.pdfDocumentViewer1.CurrentPageNumber
			Dim targetPage As Integer = Me.comBoxPages.SelectedIndex + 1
			If soucePage <> targetPage Then
				Me.pdfDocumentViewer1.GoToPage(targetPage)
			End If
		End Sub

		Private Sub comBoxPages_Click(sender As Object, e As EventArgs)

		End Sub

	End Class
End Namespace

Note:

  • If you click the buttons and they cannot operate the zoom functions, please check the click event of each button.
  • When the ComboBox cannot show page number in the dropdown list, please check the SelectedIndexChanged event of it.

Preview:

Form 1

Room Percentage

Dynamic Zoom

Fit Width

As above picture, you can easily control the whole PDF document pages by the methods that I introduced by using Spire.PDF Viewer.

Spire.PDFViewer not only enables you to open a PDF document that you load from system in the code, but also allows you to open any other PDF file by clicking the Open button in Form1, and then, choose it from a dialog box. You also can choose which page you want to view or read in the dropdown list. Why not give it a try?

Additional Info

  • tutorial_title: Zoom PDF File in C#, VB.NET
Last modified on Wednesday, 15 September 2021 03:36