Why Save PDF File via PDF Viewer?
Both PDF and PDF Viewer can save a PDF file. The main difference is that people cannot view that PDF file when save a PDF document by PDF. While using PDF Viewer, you can save any PDF file from your system as well as open and view it in Windows Forms. Furthermore, tasks such as print, zoom, page, export PDF to images all can be realized by PDF Viewer. Thus, save a PDF file via Spire. PDFViewer is rather helpful and useful in people's daily work.
How to Save PDF file via Spire.PDFViewer with C#, VB.NET
Spire. PDFViewer for .NET, as a powerful PDF Viewer component, allows you to finish the whole PDF saving task quickly with the below procedure.
Step 1: Create a new project
- Create a new project in Windows Forms Application.
- Set the target Framework to in Properties of this project to be .NET Framework 2 or above.
Step 2: Add reference and Set up the Form
- Add Spire.PDFViewer Forms dll as reference from Spire.PDFViewer
- Add a toolScript and pdfDocumentViewer in the default Form” Form1”.
- Add three buttons and a ComboBox in Form1 from toolScript dropdown list by right clicking it.
- Set the properties of three buttons and ComboBox as picture below
Step 3: Save PDF file by PDF Viewer
Add below namespaces at the top of the method.
using System.IO; using Spire.PdfViewer.Forms;
Imports System.IO Imports Spire.PdfViewer.Forms
Load a PDF file from system and open it. This step allows you to choose a PDF Document directly from system in a dialog box.
private void Form1_Load(object sender, EventArgs e) { } 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 Sub Form1_Load(sender As Object, e As EventArgs) 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
Save the PDF file. This step enables you not only to save the PDF file but also to view the PDF Document page by selecting the page number in ComboBox dropdown list. And the page number will automatically change to the page that you view when you scroll your mouse up and down.
private void BtnSave_Click(object sender, EventArgs e) { if (this.pdfDocumentViewer1.PageCount > 0) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "PDF document (*.pdf)|*.pdf"; DialogResult result = dialog.ShowDialog(); string fileName = dialog.FileName; if (result == DialogResult.OK) { pdfDocumentViewer1.SaveToFile(fileName); MessageBox.Show("You have saved this PdfDocuemnt as:\n" + fileName, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } private void BtnSaveStream_Click(object sender, EventArgs e) { if (this.pdfDocumentViewer1.PageCount > 0) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "PDF document (*.pdf)|*.pdf"; DialogResult result = dialog.ShowDialog(); string fileName = dialog.FileName; if (result == DialogResult.OK) { MemoryStream stream = new MemoryStream(); pdfDocumentViewer1.SaveToFile(stream); byte[] fileBytes = stream.ToArray(); FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite); fileStream.Write(fileBytes, 0, fileBytes.Length); fileStream.Flush(); fileStream.Close(); stream.Close(); MessageBox.Show("You have first saved this PDF docuemnt as memory stream,\nthen write the memory stream in a file :\n" + fileName, "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 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 Sub BtnSave_Click(sender As Object, e As EventArgs) If Me.pdfDocumentViewer1.PageCount > 0 Then Dim dialog As New SaveFileDialog() dialog.Filter = "PDF document (*.pdf)|*.pdf" Dim result As DialogResult = dialog.ShowDialog() Dim fileName As String = dialog.FileName If result = DialogResult.OK Then pdfDocumentViewer1.SaveToFile(fileName) MessageBox.Show("You have saved this PdfDocuemnt as:" & vbLf & fileName, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information) End If End If End Sub Private Sub BtnSaveStream_Click(sender As Object, e As EventArgs) If Me.pdfDocumentViewer1.PageCount > 0 Then Dim dialog As New SaveFileDialog() dialog.Filter = "PDF document (*.pdf)|*.pdf" Dim result As DialogResult = dialog.ShowDialog() Dim fileName As String = dialog.FileName If result = DialogResult.OK Then Dim stream As New MemoryStream() pdfDocumentViewer1.SaveToFile(stream) Dim fileBytes As Byte() = stream.ToArray() Dim fileStream As New FileStream(fileName, FileMode.Create, FileAccess.ReadWrite) fileStream.Write(fileBytes, 0, fileBytes.Length) fileStream.Flush() fileStream.Close() stream.Close() MessageBox.Show("You have first saved this PDF docuemnt as memory stream," & vbLf & "then write the memory stream in a file :" & vbLf & fileName, "Spire.PdfViewer Demo", MessageBoxButtons.OK, MessageBoxIcon.Information) End If 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
Check the Events of your buttons, comBoBox and pdfDocumentViewer.
Step 4: Debug the project
After debugging, you can preview the effect.
Besides, if you need to perform other tasks such as export PDF pages to images of different popular formats, read encrypted PDF files, display PDF pages in various ways and so on, you still can realize them by using Spire.PDFViewer.