📄 mainform.pas
字号:
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, SBPDF, SBPDFSecurity, SBX509, SBCustomCertStorage, SBUtils;
type
TfrmMain = class(TForm)
lSourceFile: TLabel;
editSource: TEdit;
btnBrowseSource: TButton;
btnOK: TButton;
btnCancel: TButton;
OpenDialogPDF: TOpenDialog;
Document: TElPDFDocument;
Cert: TElX509Certificate;
CertStorage: TElMemoryCertStorage;
OpenDialogCert: TOpenDialog;
SaveDialogPDF: TSaveDialog;
procedure btnCancelClick(Sender: TObject);
procedure btnBrowseSourceClick(Sender: TObject);
procedure btnOKClick(Sender: TObject);
private
function GenerateTempFilename : string;
function RequestPassword(const Prompt: string) : string;
function RequestCertificate : boolean;
function DisplayEncryptionInfo : boolean;
procedure DisplaySignaturesInfo;
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
uses PasswordRequestForm, EncPropsForm, SigPropsForm;
{$R *.DFM}
procedure TfrmMain.btnCancelClick(Sender: TObject);
begin
Close();
end;
procedure TfrmMain.btnBrowseSourceClick(Sender: TObject);
begin
if OpenDialogPDF.Execute then
editSource.Text := OpenDialogPDF.Filename;
end;
function TfrmMain.RequestPassword(const Prompt: string) : string;
begin
frmRequestPassword.lPrompt.Caption := Prompt;
frmRequestPassword.editPassword.Text := '';
if frmRequestPassword.ShowModal = mrOk then
Result := frmRequestPassword.editPassword.Text
else
Result := '';
end;
function TfrmMain.RequestCertificate : boolean;
var
F : TFileStream;
R : integer;
Pass : string;
begin
Result := false;
if OpenDialogCert.Execute then
begin
try
F := TFileStream.Create(OpenDialogCert.FileName, fmOpenRead or fmShareDenyWrite);
try
Pass := RequestPassword('Password is needed to decrypt the certificate:');
R := Cert.LoadFromStreamPFX(F, Pass);
finally
FreeAndNil(F);
end;
if R <> 0 then
MessageDlg('Failed to load certificate, error ' + IntToStr(R), mtError,
[mbOk], 0)
else
Result := true;
except
on E : Exception do
MessageDlg('Failed to open certificate file: ' + E.Message, mtError,
[mbOk], 0);
end;
end;
end;
function TfrmMain.GenerateTempFilename : string;
var
Needed : DWORD;
Path : string;
Index : integer;
const
SBB : string = 'sbb'#0;
begin
Needed := GetTempPathA(0, nil);
SetLength(Path, Needed);
Needed := GetTempPathA(Needed, @Path[1]);
if Needed = 0 then
raise Exception.Create('Failed to get temporary path');
SetLength(Result, MAX_PATH);
if GetTempFileNameA(@Path[1], @SBB[1], 0, @Result[1]) = 0 then
raise Exception.Create('Failed to generate a temporary file name');
Index := Pos(#0, Result);
Result := Copy(Result, 1, Index - 1);
end;
function TfrmMain.DisplayEncryptionInfo: boolean;
begin
frmEncryptionProps.Initialize(Document);
Result := frmEncryptionProps.ShowModal = mrOk;
end;
procedure TfrmMain.DisplaySignaturesInfo;
begin
frmSignaturesProps.Initialize(Document);
frmSignaturesProps.ShowModal;
end;
procedure TfrmMain.btnOKClick(Sender: TObject);
var
TempPath : string;
DocumentChanged : boolean;
F : TFileStream;
Pass : string;
PV : boolean;
begin
// creating a temporary file copy
TempPath := GenerateTempFilename;
if not CopyFile(PChar(editSource.Text), PChar(TempPath), false) then
begin
MessageDlg('Failed to create a temporary file', mtError, [mbOk], 0);
Exit;
end;
// opening the temporary file
DocumentChanged := false;
F := TFileStream.Create(TempPath, fmOpenReadWrite or fmShareDenyWrite);
try
try
// opening the document
Document.Open(F);
try
// checking if the document is secured
if (not Document.Encrypted) and (not Document.Signed) then
begin
MessageDlg('The document is neither encrypted nor signed', mtInformation,
[mbOk], 0);
Exit;
end;
// checking if the document is encrypted
if Document.Encrypted then
begin
// showing encryption information to user
if DisplayEncryptionInfo then
begin
// decrypting the document
if Document.EncryptionHandler is TElPDFPasswordSecurityHandler then
begin
Pass := RequestPassword('Please supply a password to decrypt the document:');
// trying the supplied password
PV := false;
TElPDFPasswordSecurityHandler(Document.EncryptionHandler).OwnerPassword := Pass;
if TElPDFPasswordSecurityHandler(Document.EncryptionHandler).IsOwnerPasswordValid then
PV := true
else
begin
TElPDFPasswordSecurityHandler(Document.EncryptionHandler).UserPassword := Pass;
if TElPDFPasswordSecurityHandler(Document.EncryptionHandler).IsUserPasswordValid then
PV := true;
end;
if PV then
begin
// password is OK
Document.Decrypt;
DocumentChanged := true;
MessageDlg('Document was successfully decrypted', mtInformation,
[mbOk], 0);
end
else
MessageDlg('Invalid password', mtError, [mbOK], 0);
end
else if Document.EncryptionHandler is TElPDFPublicKeySecurityHandler then
begin
// requesting certificate from user
if RequestCertificate then
begin
CertStorage.Clear;
CertStorage.Add(Cert);
TElPDFPublicKeySecurityHandler(Document.EncryptionHandler).CertStorage := CertStorage;
Document.Decrypt;
DocumentChanged := true;
MessageDlg('Document was successfully decrypted', mtInformation,
[mbOk], 0);
end
else
MessageDlg('No certificate for decryption found', mtError, [mbOk],
0);
end
else
MessageDlg('The document is encrypted with unsupported security handler',
mtError, [mbOk], 0);
end;
end;
// displaying signatures info for signed documents
if Document.Signed then
DisplaySignaturesInfo;
finally
// closing the document
Document.Close(DocumentChanged);
end;
finally
FreeAndNil(F);
end;
except
on E : Exception do
begin
MessageDlg('Error: ' + E.Message, mtError, [mbOk], 0);
DocumentChanged := false;
end;
end;
// if the document was changed (e.g., decrypted), moving the temporary file to the place
// of destination file
if DocumentChanged then
begin
if SaveDialogPDF.Execute then
begin
if not CopyFile(PChar(TempPath), PChar(SaveDialogPDF.Filename), false) then
MessageDlg('Failed to save the decrypted file', mtError, [mbOk], 0)
else
MessageDlg('The decrypted file was successfully saved', mtInformation, [mbOk], 0);
end;
end;
// deleting the temporary file
DeleteFile(TempPath);
Close();
end;
initialization
SetLicenseKey('ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76' +
'FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C' +
'F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47' +
'5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9' +
'F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A' +
'8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43' +
'DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8' +
'BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F');
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -