Ejemplo n.º 1
0
        private void EncryptPDF()
        {
            this.EnableViewWindow();
            LoadValuesFromView();

            _runProgress = true;
            //bckGroundProgress.RunWorkerAsync();

            byte[] copiedFile    = File.ReadAllBytes(_selectedFilePath);
            byte[] encryptedFile = null;
            using (Stream ms = new MemoryStream()) {
                var document = new QPDFDocument {
                    InputFileBytes     = copiedFile,
                    EncryptionPassword = _encryptedPassword
                };

                var output = new QPDFOutput {
                    OutputFilePath = _destinationFilePath
                };

                QPDF.ModifyPDF(document, output);
            }

            // Spit the copied file in memory back out..?
            if (_createCopy && copiedFile != null && _destinationFilePath == _selectedFilePath)
            {
                // NOTE: only use the copy feature IF the new
                // encrypted PDF will have the same name and path
                // write the copy back into a file now the encryption has completed
                string fileName = this.CreateCopyFileName(_selectedFilePath);
                File.WriteAllBytes(fileName, copiedFile);
            }

            // message the user as the process/task/encryption has been completed
            MessageBox.Show("Your PDF has now been protected with the assigned password.", "PDFMagikLITE", MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.EnableViewWindow();
            _runProgress = false;
            //bckGroundProgress.CancelAsync();

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
Ejemplo n.º 2
0
        private DialogResult Execute()
        {
            // this is the main logic
            // check if we have a file, do we overwrite it
            bool overwrite = false;

            if (File.Exists(_destinationFilePath))
            {
                DialogResult confirmResult = MessageBox.Show("File exists! Would you like to overwrite the current file?", "PDF Magik - PDF Exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                overwrite = (confirmResult == System.Windows.Forms.DialogResult.Yes);
            }

            if (File.Exists(_destinationFilePath) && !overwrite)
            {
                return(System.Windows.Forms.DialogResult.Abort);
            }

            // we can continue from here!
            try {
                // does the source file still exist?
                if (!File.Exists(_sourceFilePath))
                {
                    MessageBox.Show("Source file cannot be found! Unable to continue.", "PDF Magik - File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(System.Windows.Forms.DialogResult.Cancel);
                }

                // check if we want to write a copy out
                if (chkCreateCopy.Checked)
                {
                    this.WriteCopyFile(_sourceFilePath, _destinationFilePath);
                }

                var document = new QPDFDocument(_sourceFilePath);

                // setup paging
                if (_pagingOption == PagingOptions.All)
                {
                    document.Paging = new QPDFDocumentPaging();
                }
                else
                {
                    document.Paging = new QPDFDocumentPaging(int.Parse(numStart.Value.ToString()), int.Parse(numEnd.Value.ToString()));
                }

                // setup encryption
                document.Encryption = new QPDFDocumentEncryption(_encryptionOption);
                document.Encryption.DecryptPassword       = txtbCurrentPassword.Text;
                document.Encryption.EncryptPassword       = txtbNewPassword.Text;
                document.Encryption.AccessibilityFeatures = chkAccessibility.Checked;
                document.Encryption.TextGraphicExtraction = chkGraphicExtraction.Checked;
                document.Encryption.ModificationMethod    = _encryptionModifyOption;
                document.Encryption.PrintMethod           = _encryptionPrintOption;

                // setup environment
                // we are going to overoad here and allow custom QPDFPath
                QPDFEnvironment environment = null;
                if (!String.IsNullOrWhiteSpace(this.QPDFLocation))
                {
                    environment          = QPDF.Environment;
                    environment.QPDFPath = this.QPDFLocation;
                }

                // setup output
                var output = new QPDFOutput()
                {
                    OutputFilePath = _destinationFilePath
                };

                QPDF.ModifyPDF(document, environment, output);
                MessageBox.Show("Process completed.", "PDF Magik - Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(System.Windows.Forms.DialogResult.OK);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "PDF Magik - Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(System.Windows.Forms.DialogResult.Cancel);
            }
        }