Print Page | Close Window

Encrypt a pdf file (Delphi)

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: General Discussion
Forum Description: Discussion board for Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3528
Printed Date: 22 Nov 24 at 5:59PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Encrypt a pdf file (Delphi)
Posted By: Amelie
Subject: Encrypt a pdf file (Delphi)
Date Posted: 05 Dec 17 at 5:39PM
Hello!

how to encrypt a pdf file (Delphi) please with "Quick PDF Library" ?
I tried the following code, but it does not encrypt the file:

var
  CanProceed: Boolean;
begin

CanProceed := False;
  if dlgOpen.Execute then
  begin
    ViewPrintQP := TDebenuPDFLibrary.Create;
    if ViewPrintQP.UnlockKey(edtLicenseKey.Text) = 1 then
    begin
      if ViewPrintQP.LoadFromFile(dlgOpen.FileName, '') = 1 then
      begin
        CanProceed := True;
      end else
        MessageDlg('The PDF could not be opened.', mtError, [mbOK], 0);
    end else
      MessageDlg('The license key is invalid or has expired.', mtError, [mbOK], 0);
  end;

ViewPrintQP.Encrypt(dlgOpen.FileName , '' , 'Owner' , 'User' , 1 , 1);

ViewPrintQP.SaveToFile('E:\encrypted_doc.pdf');


it saves the file but it is not encrypted and it is not protected by password.

how to solve this problem please?

Thank you.

Best regards.
Amelie



Replies:
Posted By: Ingo
Date Posted: 05 Dec 17 at 9:31PM
Hi Amelie,

here you can read about the encrypt-function (only four parameters are available):
http://www.debenu.com/docs/pdf_library_reference/Encrypt.php
And here you can read about how to generate the fourth parameter:
http://www.debenu.com/docs/pdf_library_reference/EncodePermissions.php
Sample for Encrypt and EncodePermission:
// . . .
var
p5, p6, p7, p8, p9, p10, p11, p12, perm : LongInt;
perm := ViewPrintQP.EncodePermissions(p6, p7, p8, p10, p9, p11, p12, 0);
ViewPrintQP.Encrypt('Owner' , 'User' , 1 , perm);
// . . .
There's no need to insert dlgOpen.FileName into the encrypt-function 'cause the file is already known with the LoadFromFile.

To make it clean you should check first, if the file is already encrypted and do a decrypt.
This you can do after LoadFromFile.
Here's a sample:
// . . .
If ( ViewPrintQP.EncryptionStatus > 0 ) Then
     ViewPrintQP.Decrypt;
// . . .

Wish you more luck with the next try ;-)



-------------
Cheers,
Ingo



Posted By: tfrost
Date Posted: 05 Dec 17 at 9:32PM
Please check again the documentation for Encrypt.  The parameters for Delphi should be Owner, User, Strength, Permissions.  The filename should not be there, and the final '1' should be replaced by a value returned from an earlier call to EncodePermissions.

Then, check the result code from Encrypt before you save the file.  If you get a 0, call LastErrorCode to get more information.  Alternatively, if all you want to do is encrypt the file, you can use EncryptFile.

I am sure that users here will be happy to help you if we have a bit more detail (including the library version you are using)!


Posted By: Amelie
Date Posted: 06 Dec 17 at 8:25AM
Hi,

Thanks for your help. It works well !
 I use version 11.13.

tfrost : thanks for your advices

Have a good day

Amélie



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk