📄 mainform.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SBCustomCertStorage"
#pragma link "SBPDF"
#pragma link "SBX509"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
SetLicenseKey((AnsiString)
"ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76" +
"FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C" +
"F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47" +
"5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9" +
"F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A" +
"8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43" +
"DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8" +
"BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnBrowseSourceClick(TObject *Sender)
{
if (OpenDialogPDF->Execute())
editSource->Text = OpenDialogPDF->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnCancelClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnOKClick(TObject *Sender)
{
// creating a temporary file copy
AnsiString TempPath = GenerateTempFilename();
if (!CopyFile(editSource->Text.c_str(), TempPath.c_str(), false))
{
MessageDlg("Failed to create a temporary file", mtError, TMsgDlgButtons()<<mbOK, 0);
return;
}
// opening the temporary file
bool DocumentChanged = false;
TFileStream* F = new TFileStream(TempPath, fmOpenReadWrite | fmShareDenyWrite);
try
{
try
{
// opening the document
Document->Open(F);
try
{
// checking if the document is secured
if ((!Document->Encrypted) && (!Document->Signed))
{
MessageDlg("The document is neither encrypted nor signed", mtInformation, TMsgDlgButtons()<<mbOK, 0);
return;
}
// checking if the document is encrypted
if (Document->Encrypted)
{
// showing encryption information to user
if (DisplayEncryptionInfo())
{
// decrypting the document
if (AnsiString(Document->EncryptionHandler->ClassName()) == "TElPDFPasswordSecurityHandler")
{
AnsiString Pass = RequestPassword("Please supply a password to decrypt the document:");
// trying the supplied password
bool PV = false;
((TElPDFPasswordSecurityHandler*)(Document->EncryptionHandler))->OwnerPassword = Pass;
if (((TElPDFPasswordSecurityHandler*)(Document->EncryptionHandler))->IsOwnerPasswordValid())
PV = true;
else
{
((TElPDFPasswordSecurityHandler*)(Document->EncryptionHandler))->UserPassword = Pass;
if (((TElPDFPasswordSecurityHandler*)(Document->EncryptionHandler))->IsUserPasswordValid())
PV = true;
}
if (PV)
{
// password is OK
Document->Decrypt();
DocumentChanged = true;
MessageDlg("Document was successfully decrypted", mtInformation, TMsgDlgButtons()<<mbOK, 0);
}
else
MessageDlg("Invalid password", mtError, TMsgDlgButtons()<<mbOK, 0);
}
else if (AnsiString(Document->EncryptionHandler->ClassName()) == "TElPDFPublicKeySecurityHandler")
{
// requesting certificate from user
if (RequestCertificate())
{
CertStorage->Clear();
CertStorage->Add(Cert, true);
((TElPDFPublicKeySecurityHandler*)(Document->EncryptionHandler))->CertStorage = CertStorage;
Document->Decrypt();
DocumentChanged = true;
MessageDlg("Document was successfully decrypted", mtInformation, TMsgDlgButtons()<<mbOK, 0);
}
else
MessageDlg("No certificate for decryption found", mtError, TMsgDlgButtons()<<mbOK, 0);
}
else
MessageDlg("The document is encrypted with unsupported security handler", mtError, TMsgDlgButtons()<<mbOK, 0);
}
}
// displaying signatures info for signed documents
if (Document->Signed)
DisplaySignaturesInfo();
}
__finally
{
// closing the document
Document->Close(DocumentChanged);
}
}
__finally
{
delete F;
}
}
catch(Exception &e)
{
MessageDlg((AnsiString)"Error: " + e.Message, mtError, TMsgDlgButtons()<<mbOK, 0);
DocumentChanged = false;
}
// if the document was changed (e.g., decrypted), moving the temporary file to the place
// of destination file
if (DocumentChanged)
{
if (SaveDialogPDF->Execute())
{
if (!CopyFile(TempPath.c_str(), SaveDialogPDF->FileName.c_str(), false))
MessageDlg("Failed to save the decrypted file", mtError, TMsgDlgButtons()<<mbOK, 0);
else
MessageDlg("The decrypted file was successfully saved", mtInformation, TMsgDlgButtons()<<mbOK, 0);
}
}
// deleting the temporary file
DeleteFile(TempPath);
Close();
}
//---------------------------------------------------------------------------
AnsiString TfrmMain::GenerateTempFilename(void)
{
const char* szSBB = "sbb";
DWORD dwNeeded = GetTempPathA(0, NULL);
char* szPath = new char[dwNeeded+1];
dwNeeded = GetTempPathA(dwNeeded, szPath);
if (dwNeeded == 0)
{
delete[] szPath;
throw(Exception("Failed to get temporary path"));
}
char* szResult = new char[MAX_PATH];
if (GetTempFileNameA(szPath, szSBB, 0, szResult) == 0)
{
delete[] szPath;
delete[] szResult;
throw(Exception("Failed to generate a temporary file name"));
}
AnsiString sResult = AnsiString(szResult);
delete[] szPath;
delete[] szResult;
return sResult;
}
//---------------------------------------------------------------------------
bool TfrmMain::DisplayEncryptionInfo(void)
{
frmEncryptionProps->Initialize(Document);
return (frmEncryptionProps->ShowModal() == mrOk);
}
//---------------------------------------------------------------------------
AnsiString TfrmMain::RequestPassword(const AnsiString Prompt)
{
frmRequestPassword->lPrompt->Caption = Prompt;
frmRequestPassword->editPassword->Text = "";
if (frmRequestPassword->ShowModal() == mrOk)
return frmRequestPassword->editPassword->Text;
else
return "";
}
//---------------------------------------------------------------------------
bool TfrmMain::RequestCertificate(void)
{
bool Result = false;
if (OpenDialogCert->Execute())
{
try
{
int R;
TFileStream* F = new TFileStream(OpenDialogCert->FileName, fmOpenRead | fmShareDenyWrite);
try
{
AnsiString Pass = RequestPassword("Password is needed to decrypt the certificate:");
R = Cert->LoadFromStreamPFX(F, Pass, F->Size);
}
__finally
{
delete F;
}
if (R != 0)
MessageDlg((AnsiString)"Failed to load certificate, error " + R, mtError, TMsgDlgButtons()<<mbOK, 0);
else
Result = true;
}
catch(Exception &e)
{
MessageDlg((AnsiString)"Failed to open certificate file: " + e.Message, mtError, TMsgDlgButtons()<<mbOK, 0);
}
}
return Result;
}
//---------------------------------------------------------------------------
void TfrmMain::DisplaySignaturesInfo(void)
{
frmSignaturesProps->Initialize(Document);
frmSignaturesProps->ShowModal();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -