Пример #1
0
        // Close and reopen the document from the file - useful if you've made changes to the PDF
        private void RefreshButton_Click(object sender, EventArgs e)
        {
            // We only need to refesh if a doc is open
            if (PDFDoc != null)
            {
                if (docWasModified)
                {
                    runSaveDialog();
                }
                // Clear the existing views
                mainTree.Nodes.Clear();
                clearDataGrid();

                PDFDoc.Dispose();

                // If we opened with a password, use it again
                if (currPassword != null)
                {
                    PDFDoc = new Document(currFilePath, currPassword, PermissionRequestOperation.Open, false);
                }
                else
                {
                    PDFDoc = new Document(currFilePath);
                }

                // Now repopulate the tree
                GrabDocRootAndInfo();

                // Re-initialize the data view
                InitDataView();

                editor = new PDFObjectExplorer(PDFDoc, mainTree, dataGridView);
            }
        }
Пример #2
0
        // Open a PDF to examine
        private void OpenPDFButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "PDF File (*.pdf)|*.pdf";
            dialog.Title  = "Select a PDF file";
            DialogResult result = dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                String FileName = dialog.FileName;
                try
                {
                    // Dispense with the previous PDF (if any)
                    if (PDFDoc != null)
                    {
                        if (docWasModified)
                        {
                            runSaveDialog();
                        }
                        PDFDoc.Dispose();
                    }
                    mainWindow.ActiveForm.Text = FileName;

                    // First attempt to open the document with no password
                    try
                    {
                        PDFDoc       = new Document(FileName);
                        currPassword = null;    // Null out the password if we didn't need it
                    }
                    catch (Exception fileOpenException)
                    {
                        string exceptionMessage = fileOpenException.Message;

                        // Trap password-required exception
                        if (exceptionMessage.Contains("1073938471"))
                        {
                            // Prompt user for a password
                            string password = "";

                            frmPassword  passwordForm       = new frmPassword();
                            DialogResult passwordFormResult = passwordForm.ShowDialog();

                            try
                            {
                                if (passwordFormResult == DialogResult.OK)
                                {
                                    password = passwordForm.password;

                                    PDFDoc       = new Document(FileName, password, PermissionRequestOperation.Open, false);
                                    currPassword = password;    // Save the password
                                }
                                // If not OK, user cancelled.  Abandon the open, no document available.
                                else
                                {
                                    mainTree.Nodes.Clear();
                                    mainTree.Nodes.Add("(no document)");
                                    return;
                                }
                            }
                            catch (Exception fileOpenWithPasswordException)
                            {
                                // Just throw to the outer handler
                                throw fileOpenWithPasswordException;
                            }
                        }
                    }

                    // Now repopulate the tree
                    GrabDocRootAndInfo();

                    // Re-initialize the data view
                    InitDataView();

                    editor = new PDFObjectExplorer(PDFDoc, mainTree, dataGridView);

                    // Save the file path
                    currFilePath = FileName;
                }
                catch (Exception exc)
                {
                    MessageBox.Show("The File " + FileName + " cannot be opened. \n\n" + exc.ToString());
                }
            }
        }