Sunday, 5 August 2012

Page PDF Document by PDF Viewer with C#,VB.NET

Things become difficult when a PDF document has tens or hundreds of pages. It wastes much time for people to reach the page that they want to go. Especially for those products manuals or statute books, because of its unchangeable character, PDF format becomes their best choice. But at the same time, it is neither possible to finish reading in a short period nor reach the right page in seconds. While things turn very easy when you use the PDF Viewer paging function, you can get the page by choose the page number or other buttons such as First, Previous, Next, Last and so on.

In this post, I will share a method to page PDF Document by PDF Viewer with C#. VB.NET.It is very necessary for me to choose PDF Viewer compoenent Spire.PDFViewer to be my tool. So please make sure that you freely install Spire.PDFViewer on system.

Freely Download Spire.PDFViewer

How to page PDF Document by PDF Viewer with C#, VB.NET
Using Spire.PDFViewer, I only use the below steps to finish this task. Because the code is a little long, so I only use C# to page the PDF Document. Please look at the following procedure.
Step1. Create a new project.

1.     Create a new project in Windows Forms Application in Visual Studio.

2.     Choose .NET Framework 2 or above as the Target framework in Properties.

Step2.Add reference and design the Form.
1.     Add Spire.PDF DLL and Spire.PDFViewer Forms DLL as reference from your downladed Spire.PDFViewer.

2.     Add a toolScript in Form 1. And then, add five buttons and a ComboBox at the same time in toolScript dropdown list.

3.     Set the Properties of the five buttons and the ComboBox. I set the buttons' Name to be btn_Open, btn_First, btn_Previous, btn_Next, btn_Last.  The ComboBox to be comBoxPages . Also the default button DisplayStyle is "image", I change it to be "Text" and set the "Text" to be "Open, First, Previous, Next, Last"

4.     Add pdfDocumentViewer in Form1, and set its Dock in Properties to set the Form size.

Step3. Page PDF Document by PDF Viewer 

1.     Add the following using at the top of the method.

C# 
using System.IO;
using Spire.PdfViewer;
using Spire.PdfViewer.Forms;
using Spire.Pdf;

2.     Load a PDF file from system.

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

3.     Open a PDF Document .

       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)
            {
                {
                    string pdfFile = dialog.FileName;
                    this.pdfDocumentViewer1.LoadFromFile(pdfFile);
                }
            }
        }
4.   Page the PDF file by PDF Viewer.

private void btnFirst_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                if (currentPage != 1)
                {
                    this.pdfDocumentViewer1.GoToFirstPage();
                }
                else
                {
                    MessageBox.Show("Current page is already the first!", "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                if (currentPage > 1)
                {
                    this.pdfDocumentViewer1.GoToPreviousPage();
                }
                else
                {
                    MessageBox.Show("Current page is already the first!", "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                int totalPage = this.pdfDocumentViewer1.PageCount;
                if (currentPage < totalPage)
                {
                    this.pdfDocumentViewer1.GoToNextPage();
                }
                else
                {
                    MessageBox.Show("Current page is already the last!", "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void pdfDocumentViewer1_PdfLoaded(object sender, EventArgs args)
        {
            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 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 btnLast_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                int currentPage = this.pdfDocumentViewer1.CurrentPageNumber;
                int totalPage = this.pdfDocumentViewer1.PageCount;
                if (currentPage != totalPage)
                {
                    this.pdfDocumentViewer1.GoToLastPage();
                }
                else
                {
                    MessageBox.Show("Current page is already the last!", "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        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;
            }
        }


Preview

                              
You can either click the First, Previous, Next, Last button to view the PDF file or choose the page number to directly reach the page. The Open button allows you to open another PDF Document.

No comments:

Post a Comment

Popular posts