using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
// The PDF Viewer namespace
using PdfViewer4WinNet;
namespace PdfViewerWinNetDemo
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
LoadPageLayoutModes();
LoadDocumentDisplayModes();
LoadPageFitModes();
LoadViewerPreferences();
formTooltip.SetToolTip(btnOpenFile, "Open a file in viewer");
formTooltip.SetToolTip(btnPrintDialog, "Opens the print dialog");
string demoFilePath = System.IO.Path.Combine(Application.StartupPath, "evopdf.pdf");
pdfViewerControl.LicenseKey = "gw0cDBkbDBwMGQIcDB8dAh0eAhUVFRU=";
if (System.IO.File.Exists(demoFilePath))
{
// load PDF from file
pdfViewerControl.LoadFile(demoFilePath);
lblPageCount.Text = pdfViewerControl.PageCount > 0 ?
String.Format("Page Count: {0}", pdfViewerControl.PageCount) : String.Empty;
}
}
private void LoadPageLayoutModes()
{
string[] pageLayoutModes = Enum.GetNames(typeof(PageLayoutMode));
ddlPageLayout.DataSource = pageLayoutModes;
ddlPageLayout.SelectedItem = PageLayoutMode.NotSet.ToString();
}
private void LoadDocumentDisplayModes()
{
string[] documentDisplayModes = Enum.GetNames(typeof(DocumentDisplayMode));
ddlDocumentDisplayMode.DataSource = documentDisplayModes;
ddlDocumentDisplayMode.SelectedItem = DocumentDisplayMode.NotSet.ToString();
}
private void LoadPageFitModes()
{
string[] pageFitModes = Enum.GetNames(typeof(PageFitMode));
ddlPageFitMode.DataSource = pageFitModes;
ddlPageFitMode.SelectedItem = PageFitMode.NotSet.ToString();
}
private void LoadViewerPreferences()
{
cbShowNavigationPanel.Checked = pdfViewerControl.ShowNavigationPanel;
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
using (FileStream pdfFileStream = new FileStream(fd.FileName, FileMode.Open, FileAccess.Read))
{
// load PDF from stream
pdfViewerControl.LoadStream(pdfFileStream);
lblPageCount.Text = pdfViewerControl.PageCount > 0 ?
String.Format("Page Count: {0}", pdfViewerControl.PageCount) : String.Empty;
}
}
}
private void btnPrintDialog_Click(object sender, EventArgs e)
{
pdfViewerControl.PrintWithDialog();
}
private void btnZoom_Click(object sender, EventArgs e)
{
SetZoomDialog zd = new SetZoomDialog();
zd.Percentage = 100;
if (zd.ShowDialog() == DialogResult.OK)
{
pdfViewerControl.SetZoom(zd.Percentage);
}
}
private void cbShowToolbar_CheckedChanged(object sender, EventArgs e)
{
pdfViewerControl.SetShowToolbar(cbShowToolbar.Checked);
}
private void btnSetPageNumber_Click(object sender, EventArgs e)
{
SetPageNumberDialog pnd = new SetPageNumberDialog(pdfViewerControl.PageCount);
pnd.PageNumber = 1;
if (pnd.ShowDialog() == DialogResult.OK)
{
pdfViewerControl.SetCurrentPage(pnd.PageNumber);
}
}
private void btnFirstPage_Click(object sender, EventArgs e)
{
pdfViewerControl.GoToFirstPage();
}
private void btnLastPage_Click(object sender, EventArgs e)
{
pdfViewerControl.GoToLastPage();
}
private void btnPreviousPage_Click(object sender, EventArgs e)
{
pdfViewerControl.GoToPreviousPage();
}
private void btnNextPage_Click(object sender, EventArgs e)
{
pdfViewerControl.GoToNextPage();
}
private void btnBack_Click(object sender, EventArgs e)
{
pdfViewerControl.GoBackwardStack();
}
private void btnForward_Click(object sender, EventArgs e)
{
pdfViewerControl.GoForwardStack();
}
private void ddlPageLayout_SelectedIndexChanged(object sender, EventArgs e)
{
pdfViewerControl.PageLayoutMode = (PageLayoutMode)Enum.Parse(typeof(PageLayoutMode),
ddlPageLayout.SelectedItem.ToString());
if (pdfViewerControl.PageLayoutMode == PageLayoutMode.NotSet)
pdfViewerControl.ReloadPdfDocument();
}
private void ddlPageFitMode_SelectedIndexChanged(object sender, EventArgs e)
{
pdfViewerControl.PageFitMode = (PageFitMode)Enum.Parse(typeof(PageFitMode),
ddlPageFitMode.SelectedItem.ToString());
if (pdfViewerControl.PageFitMode == PageFitMode.NotSet)
pdfViewerControl.ReloadPdfDocument();
}
private void ddlDocumentDisplayMode_SelectedIndexChanged(object sender, EventArgs e)
{
pdfViewerControl.DocumentDisplayMode = (DocumentDisplayMode)Enum.Parse(typeof(DocumentDisplayMode),
ddlDocumentDisplayMode.SelectedItem.ToString());
if (pdfViewerControl.DocumentDisplayMode == DocumentDisplayMode.NotSet)
pdfViewerControl.ReloadPdfDocument();
}
private void cbShowScrollbar_CheckedChanged(object sender, EventArgs e)
{
pdfViewerControl.ShowScrollbars = cbShowScrollbar.Checked;
}
private void cbShowNavigationPanel_MouseClick(object sender, MouseEventArgs e)
{
cbShowNavigationPanel.Checked = !cbShowNavigationPanel.Checked;
MessageBox.Show("The left navigation panel visibility can be changed by ShowNavigationPanel
property only before loading the control. For example you can set this property in the Properties
windows from Visual Studio before running the demo application.");
}
private void btnApplySecurityOptions_Click(object sender, EventArgs e)
{
pdfViewerControl.PdfSecurityOptions.CanPrint = cbAllowPrint.Checked;
pdfViewerControl.PdfSecurityOptions.CanCopyContent = cbAllowContentCopy.Checked;
pdfViewerControl.PdfSecurityOptions.CanFillFormFields = cbAllowFormFilling.Checked;
pdfViewerControl.PdfSecurityOptions.CanEditContent = cbAllowContentEdit.Checked;
pdfViewerControl.PdfSecurityOptions.CanEditAnnotations = cbAllowEditAnnotation.Checked;
pdfViewerControl.PdfSecurityOptions.CanAssembleDocument = cbAllowAssemble.Checked;
pdfViewerControl.PdfSecurityOptions.UserPassword = textBoxUserPassword.Text.Trim();
pdfViewerControl.ReloadPdfDocument();
}
private void cbAllowFormFilling_CheckedChanged(object sender, EventArgs e)
{
if (!cbAllowFormFilling.Checked)
{
cbAllowContentEdit.Checked = false;
cbAllowEditAnnotation.Checked = false;
}
}
}
}