Sunday, 5 August 2012

Methods to Zoom PDF File by PDF Viewer with C#

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 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 by PDF Viewer with C#, VB.NET.

How to zoom PDF document by PDF Viewer with C#, VB.NET

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


Step1.  Create a new project.

1.     Create a new project in Windows Forms Application.

2.     Set the Target Framework to be .NET Framework 2 or above in Properties.

3.      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. 

4.      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.

Step2. Methods to zoom PDF file by Spire.PDFViewer

1.      Add Spire. PDFViewer.Forms dll as reference.

2.      Add below namespace at the top of the method.


C# Code:

using System.IO;using System.Windows.Forms;using Spire.PdfViewer.Forms;

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


Zoom: Manually choose the percentage.
Zoom Out: Increase the PDF text size. 
Zoom In: Decrease 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 changes to the original size.
Fit Page: Fit page control the PDF page not going across the PDF page height and margin.
Fit Width: When you click the Fit Width button, you cannot see the margin of PDF document and only see the PDF text content.

Please look at the main code:

C# Code:

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\PDFViewer.pdf";            if (File.Exists(pdfDoc))            {                this.pdfDocumentViewer1.LoadFromFile(pdfDoc);            }            //add zoom values to comboBox            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)        {        }    }}

Step3. Debug the project 

Press F5 to launch the project

Note: 1. If you click the buttons and they cannot operate the zoom functions, please check the click event of each button.

          2. When the ComboBox cannot show page number in the dropdown list, pleae check the SelectedIndexChanged event of it.


Preview

                                                                        Form1

       
                                                               Zoom Percentage
        
                                                               Zoom Dynamic

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?

No comments:

Post a Comment

Popular posts