The Portable Document Format or PDF is a file format developed by Adobe in the 1990s to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.
As we known, PDF is not Microsoft technology. It is developed and commonly used by the Adobe system for exchange of documents based on the post-script. The. Net framework provides no library to handle PDF files easily on .Net.
When we want to display a PDF file in .net, we can use many different methods, such as the use of a web browser or office library, but the Acrobat Reader controls can also be used.
Adobe offers an ActiveX COM control that we can add to CSharp toolbox. It's an Adobe Acrobat PDF Reader which is free of charge.
Create a C# Windows application project then add the C# toolbox control. Right-click on any tab of toolbox and select "Choose Items...
Select the "COM Components" tab from the open window and click the check "Adobe PDF Reader" then click OK.
Note: In order to use the Reader control, make sure you have the free Acrobat Reader from Adobe downloaded and installed.
We will see the Adobe PDF Reader control icon in the toolbox, then we can drag and drop this control onto our form.
Add a button to the form and them add code to its click event for opening PDF files.
openFileDialog.Filter = "PDF Files(*.pdf) |*.pdf;";
openFileDialog.ShowDialog();
if(openFileDialog.FileName!=null)
{
axAcroPDF1.LoadFile(openFileDialog.FileName);
}
We can also navigate the pdf pages on button click.
For go to First Page of pdf file write the code on button click.
axAcroPDF1.gotoFirstPage();
for go to Next Page of pdf file write the code on button click.
axAcroPDF1.gotoNextPage();
for go to Previous Page of pdf file write the code on button click.
axAcroPDF1.gotoPreviousPage();
for go to Last Page of pdf file write the code on button click.
axAcroPDF1.gotoLastPage();
Now, If we want jump to another page we can use the following code:
axAcroPDF1.setCurrentPage(int PageNumber);
For Print Displayed PDF use the code:
axAcroPDF1.Print();
Complete Source Code
using System;
using System.Windows.Forms;
namespace OpenPDF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnOpenPDF_Click(object sender, EventArgs e)
{
openFileDialog.Filter = "PDF Files(*.pdf) |*.pdf;";
openFileDialog.ShowDialog();
if(openFileDialog.FileName!=null)
{
axAcroPDF1.LoadFile(openFileDialog.FileName);
}
}
private void BtnFirst_Click(object sender, EventArgs e)
{
axAcroPDF1.gotoFirstPage();
}
private void BtnNext_Click(object sender, EventArgs e)
{
axAcroPDF1.gotoNextPage();
}
private void BtnPrevious_Click(object sender, EventArgs e)
{
axAcroPDF1.gotoPreviousPage();
}
private void BtnLast_Click(object sender, EventArgs e)
{
axAcroPDF1.gotoLastPage();
}
private void BtnGoTo_Click(object sender, EventArgs e)
{
axAcroPDF1.setCurrentPage(Convert.ToInt32(nudPage.Value));
}
private void Button1_Click(object sender, EventArgs e)
{
axAcroPDF1.Print();
}
private void Button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}