securebb-win-demos/PDF Decryptor (WinForms)/pdfdecryptor.cs

274 lines
11 KiB
C#
Raw Normal View History

2024-08-08 13:09:34 +06:00
/*
* SecureBlackbox 2024 .NET Edition - Sample Project
*
* This sample project demonstrates the usage of SecureBlackbox in a
* simple, straightforward way. It is not intended to be a complete
* application. Error handling and other checks are simplified for clarity.
*
* www.nsoftware.com/secureblackbox
*
* This code is subject to the terms and conditions specified in the
* corresponding product license agreement which outlines the authorized
* usage and restrictions.
*
*/
using System;
using System.Windows.Forms;
using nsoftware.SecureBlackbox;
namespace PDFDecryptorDemo
{
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnBrowseSource;
private System.Windows.Forms.Button btnBrowseDest;
private System.Windows.Forms.Label lSource;
private System.Windows.Forms.Label lDest;
private System.Windows.Forms.TextBox tbSource;
private System.Windows.Forms.TextBox tbDest;
private System.Windows.Forms.Button btnDecrypt;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.OpenFileDialog openPDFDialog;
private System.Windows.Forms.SaveFileDialog savePDFDialog;
private System.Windows.Forms.Label lDemoInfo;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tbSource = new System.Windows.Forms.TextBox();
this.tbDest = new System.Windows.Forms.TextBox();
this.btnBrowseSource = new System.Windows.Forms.Button();
this.btnBrowseDest = new System.Windows.Forms.Button();
this.lSource = new System.Windows.Forms.Label();
this.lDest = new System.Windows.Forms.Label();
this.btnDecrypt = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.openPDFDialog = new System.Windows.Forms.OpenFileDialog();
this.savePDFDialog = new System.Windows.Forms.SaveFileDialog();
this.lDemoInfo = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// tbSource
//
this.tbSource.Location = new System.Drawing.Point(8, 62);
this.tbSource.Name = "tbSource";
this.tbSource.Size = new System.Drawing.Size(288, 21);
this.tbSource.TabIndex = 0;
//
// tbDest
//
this.tbDest.Location = new System.Drawing.Point(8, 110);
this.tbDest.Name = "tbDest";
this.tbDest.Size = new System.Drawing.Size(288, 21);
this.tbDest.TabIndex = 2;
//
// btnBrowseSource
//
this.btnBrowseSource.Location = new System.Drawing.Point(304, 61);
this.btnBrowseSource.Name = "btnBrowseSource";
this.btnBrowseSource.Size = new System.Drawing.Size(75, 23);
this.btnBrowseSource.TabIndex = 1;
this.btnBrowseSource.Text = "Browse...";
this.btnBrowseSource.Click += new System.EventHandler(this.btnBrowseSource_Click);
//
// btnBrowseDest
//
this.btnBrowseDest.Location = new System.Drawing.Point(304, 109);
this.btnBrowseDest.Name = "btnBrowseDest";
this.btnBrowseDest.Size = new System.Drawing.Size(75, 23);
this.btnBrowseDest.TabIndex = 3;
this.btnBrowseDest.Text = "Browse...";
this.btnBrowseDest.Click += new System.EventHandler(this.btnBrowseDest_Click);
//
// lSource
//
this.lSource.AutoSize = true;
this.lSource.Location = new System.Drawing.Point(8, 44);
this.lSource.Name = "lSource";
this.lSource.Size = new System.Drawing.Size(83, 13);
this.lSource.TabIndex = 4;
this.lSource.Text = "Source PDF file:";
//
// lDest
//
this.lDest.AutoSize = true;
this.lDest.Location = new System.Drawing.Point(8, 92);
this.lDest.Name = "lDest";
this.lDest.Size = new System.Drawing.Size(104, 13);
this.lDest.TabIndex = 5;
this.lDest.Text = "Destination PDF file:";
//
// btnDecrypt
//
this.btnDecrypt.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnDecrypt.Location = new System.Drawing.Point(117, 148);
this.btnDecrypt.Name = "btnDecrypt";
this.btnDecrypt.Size = new System.Drawing.Size(75, 23);
this.btnDecrypt.TabIndex = 4;
this.btnDecrypt.Text = "Decrypt";
this.btnDecrypt.Click += new System.EventHandler(this.btnDecrypt_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(197, 148);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// openPDFDialog
//
this.openPDFDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
this.openPDFDialog.InitialDirectory = ".";
//
// savePDFDialog
//
this.savePDFDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
this.savePDFDialog.InitialDirectory = ".";
//
// lDemoInfo
//
this.lDemoInfo.ForeColor = System.Drawing.SystemColors.Highlight;
this.lDemoInfo.Location = new System.Drawing.Point(8, 2);
this.lDemoInfo.Name = "lDemoInfo";
this.lDemoInfo.Size = new System.Drawing.Size(371, 40);
this.lDemoInfo.TabIndex = 9;
this.lDemoInfo.Text = "This sample illustrates the use of PDFDecryptor component for decrypting PDF docu" +
"ments. Please pick the PDF document and then click \'Decrypt\'";
this.lDemoInfo.UseMnemonic = false;
//
// MainForm
//
this.AcceptButton = this.btnDecrypt;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(389, 182);
this.Controls.Add(this.lDemoInfo);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnDecrypt);
this.Controls.Add(this.lDest);
this.Controls.Add(this.lSource);
this.Controls.Add(this.btnBrowseDest);
this.Controls.Add(this.btnBrowseSource);
this.Controls.Add(this.tbDest);
this.Controls.Add(this.tbSource);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "PDF Decryptor demo";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
private void btnBrowseSource_Click(object sender, System.EventArgs e)
{
openPDFDialog.FileName = tbSource.Text;
if (openPDFDialog.ShowDialog() == DialogResult.OK)
tbSource.Text = openPDFDialog.FileName;
}
private void btnBrowseDest_Click(object sender, System.EventArgs e)
{
savePDFDialog.FileName = tbDest.Text;
if (savePDFDialog.ShowDialog() == DialogResult.OK)
tbDest.Text = savePDFDialog.FileName;
}
private void btnDecrypt_Click(object sender, System.EventArgs e)
{
using (PDFDecryptor PDFDecryptor = new PDFDecryptor())
{
try
{
PDFDecryptor.OnEncrypted += PDFDecryptor_OnEncrypted;
PDFDecryptor.InputFile = tbSource.Text;
PDFDecryptor.OutputFile = tbDest.Text;
PDFDecryptor.Decrypt();
MessageBox.Show("PDF document successfully decrypted", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
this.Close();
}
private void PDFDecryptor_OnEncrypted(object sender, PDFDecryptorEncryptedEventArgs e)
{
using (EncPropsForm FormPdfencprops = new EncPropsForm())
{
FormPdfencprops.SetPDFEncryptionProps(sender as PDFDecryptor);
e.SkipThis = (FormPdfencprops.ShowDialog() == DialogResult.Cancel);
FormPdfencprops.GetPDFDecryptionInfo(sender as PDFDecryptor);
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}