Monday, December 3, 2012

Lacy Print PDF Document Pages by PDF Viewer with C#, VB.NET

Step1. Create a new project.

1.      Create a new project in Visual Studio. Please note that this project needs a Form.

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

Step2. Add reference and Set up the Form.


1.      Add Spire.PDFViewer Form Dll as reference from your downloaded Spire.PDFViewer.

2.      Add a toolScript  and pdfDocumentViewer in the default Form " Form1".

3.      Add two buttons and a ComboBox  in Form1 from toolScript dropdown list

4.      Set the "Name", "Display Style", "Text" and "ToolTipText" of button1 in Properties to be "btnOpen", "Text", "Open" and "Open PDF document" and button2 to be "btnPrint", "Text","Print" and "Print PDF document".

5.      Set the Dock property of pdfDocumentViewer in order to view PDF file page in a enough space.
Step3. Print PDF Document Pages by PDF Viewer

1.      Add below namespaces at the top of the method.

C# Code:
using System.IO;
using Spire.PdfViewer.Forms;

VB.NET Code:
Imports System.IO
Imports Spire.PdfViewer.Forms
2.      Load a PDF document from system

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

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

C# Code:
        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);
            }
        }
 
VB.NET Code:
         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
                 Dim pdfFile As String = dialog.FileName
                 Me.pdfDocumentViewer1.LoadFromFile(pdfFile)
                 End If
         End Sub
4.      Print PDF document pages by PDF Viewer

C# Code:
        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.pdfDocumentViewer1.PageCount > 0)
            {
                this.pdfDocumentViewer1.Print();
            }
        }
        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 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);
            }
        }

No comments:

Post a Comment