Print PDF Document Pages by PDF Viewer with C#, VB.NET
Blog author: Lacy | Posted on: 5/28/2012 | Category: C# Blogs | Views: 7268 | Status: [Member] | Points: 75
| Alert Moderator
As its read only characteristic, PDF enjoys great popularity among business field. Especially, when sign contracts or send invoices, almost all the business contacts and invoices must be printed. Thus, printing PDF document pages becomes an unavoidable process, which requires a well knowledge of how to print PDF document in a quick way.This program guide aims at introducing a method to print PDF document by a PDF Viewer component Spire.PDFViewer with C#, VB.NET.This method enables you not only to print PDF document pages but also to open any PDF document on system via Spire.PDFViewer. That is to say, one method can open and print many PDF files. Please look at the procedure below.
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 Viewer1. 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); } } VB.NET Code: Private Sub btnPrint_Click(sender As Object, e As EventArgs) If Me.pdfDocumentViewer1.PageCount > 0 Then Me.pdfDocumentViewer1.Print() End If End Sub Private Sub pdfDocumentViewer1_PdfLoaded(sender As Object, args As EventArgs) 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 Step4. Press F5 to launch the file.
After debugging, you can preview the effect.
The First Page The Fourth Page
When you click the "Open" button, there will be a dialog box appears and then you can open another PDF document from system. Also, you can select any page in the dropdown list of the ComboBox and choose "Print" button to print it.
Found interesting? Add this to:
|
More Blogs from Lacy
- Sort Data Information in Excel Worksheet with C#,VB.NET
- Export Data from Spreadsheet to Datatable in Excel with C#,VB.NET
- Import Data from Datatable to Spreadsheet in Excel with C#, VB.NET
- Export Data with Formulas with C#, VB.NET
- Easy Way to Save Excel File as CSV with C#, VB.NET
- Create Excel Line Chart with C#, VB.NET
- Extract Text from Word File with C#, VB.NET
- Extract Images from Word File with C#, VB.NET
- Best Solution to Create Excel Pivot Table with C#, VB.NET
- Split One PDF Document to Multiple Files with C#, VB.NET
- Three Steps to Save Html File as Word Document with C#, VB.NET
- Page PDF Document by PDF Viewer with C#,VB.NET
  More ...
|
|
|