using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using pdftron; using pdftron.PDF; using pdftron.Filters; using pdftron.Common; using PDFTRON.SDF; namespace PDFViewSimpleCS { /// /// Summary description for Form1. /// public class PDFViewSimple : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem5; private System.Windows.Forms.MenuItem MenuFileOpen; private System.Windows.Forms.MenuItem MenuFileExit; private PDFView _pdfview; private PDFDoc _pdfdoc; public PDFViewSimple() { // // Required for Windows Form Designer support // InitializeComponent(); _pdfview = new PDFView(); _pdfview.Location = new Point(0, 0); _pdfview.Dock = System.Windows.Forms.DockStyle.Fill; //_pdfview.SetErrorReportProc(new PDFViewErrorDelegate(ErrorMsg), null); //_pdfview.SetCurrentPageProc(new PDFViewCurrentPageDelegate(UpdateStatusBar), main_form); Controls.Add(_pdfview); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PDFViewSimple)); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.MenuFileOpen = new System.Windows.Forms.MenuItem(); this.menuItem5 = new System.Windows.Forms.MenuItem(); this.MenuFileExit = new System.Windows.Forms.MenuItem(); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.MenuFileOpen, this.menuItem5, this.MenuFileExit}); this.menuItem1.Text = "File"; // // MenuFileOpen // this.MenuFileOpen.Index = 0; this.MenuFileOpen.Text = "Open.."; this.MenuFileOpen.Click += new System.EventHandler(this.menuFileOpen_Click); // // menuItem5 // this.menuItem5.Index = 1; this.menuItem5.Text = "-"; // // MenuFileExit // this.MenuFileExit.Index = 2; this.MenuFileExit.Text = "Exit"; this.MenuFileExit.Click += new System.EventHandler(this.MenuFileExit_Click); // // PDFViewSimple // this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); this.ClientSize = new System.Drawing.Size(744, 512); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Menu = this.mainMenu1; this.Name = "PDFViewSimple"; this.Text = "PDFViewSimple"; } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { PDFNet.Initialize(); // Search for PDFNet resource file (i.e. 'pdfnet.res'). string resource_path = "resources"; bool found = false; for (int i=0; !found && i<7; ++i) { found = PDFNet.SetResourcesPath(resource_path); if (!found) resource_path = "../" + resource_path; } if (!found) { MessageBox.Show("PDFNet resource file 'pdfnet.res' was not found.\nSome files may not render properly.", "PDFView Error"); } Application.Run(new PDFViewSimple()); PDFNet.Terminate(); } private void menuFileOpen_Click(object sender, System.EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.CheckFileExists = true; dlg.CheckPathExists = true; dlg.Filter="PDF (*.pdf)|*.pdf|All files (*.*)|*.*"; dlg.DefaultExt = ".pdf"; if(dlg.ShowDialog() == DialogResult.OK) { OpenPDF(dlg.FileName); } } public bool OpenPDF(String filename) { try { _pdfdoc = new PDFDoc(filename); if (!_pdfdoc.InitSecurityHandler()) // In case _pdfdoc is encrypted { //a password is required so create a dialog //to allow the user to authenticate AuthorizeDlg dlg = new AuthorizeDlg(); int Count=0; bool failed=true; //The user has three opportunities to enter //the correct password while(Count<3 && failed) { DialogResult res=dlg.ShowDialog(); if (res == DialogResult.OK && _pdfdoc.InitStdSecurityHandler(dlg.pass.Text)) { failed=false; } else if(res == DialogResult.Cancel) { break; } else { MessageBox.Show("Authorize Failed...", "PDFView Error"); Count++; } } if(failed) { //user either failed to authenticate three //times or pressed cancel MessageBox.Show("Document authentication error", "PDFView Error"); return false; } } _pdfview.SetDoc(_pdfdoc); } catch(PDFNetException ex) { MessageBox.Show(ex.Message); return false; } catch(Exception ex) { MessageBox.Show(ex.ToString()); return false; } this.Text = filename; // Set the title return true; } private void MenuFileExit_Click(object sender, System.EventArgs e) { //when the user clicks exit, exit the application Application.Exit(); } } }