#1 2016-09-26 08:20:42

PB
Member
Registered: 2016-09-26
Posts: 2

AV saving encrypted PDF

Hello,

when creating encrypted PDF file, the following code causes Access Violation at address 00030000:

procedure TPdfForm.CreatePdfClick(Sender: TObject);
var
  D: TPdfDocument;
begin
  D := TPdfDocument.Create(False, 0, False, TPdfEncryption.New(elRC4_128, '', 'XXXX', PDF_PERMISSION_NOMODIF));
  try
    D.NewDoc;         // <--- This line causes the AV during SaveToFile
    D.AddPage;
    D.Canvas.SetFont('FixedWidth', 10, []);
    D.Canvas.TextOut(50, 500, 'Text');
    D.SaveToFile('Test.pdf');
  finally
    D.Free;
  end;
end;

The problem is related to the NewDoc method - after deleting the line D.NewDoc; the code works fine.
My problem is that I want to generate several PDF files, so after SaveToFile I need to initialize new document by calling NewDoc, AddPage and so on. This works fine without encryption, but with PdfEncryption the second call to NewDoc (the first call is part of the TPdfDocument constructor) causes the next call to SaveToFile will raise AV.

Example of saving two PDF files:

procedure TPdfForm.CreateTwoFilesClick(Sender: TObject);
var
  D: TPdfDocument;
begin
  D := TPdfDocument.Create(False, 0, False, TPdfEncryption.New(elRC4_128, '', 'XXXX', PDF_PERMISSION_NOMODIF));
  try
    D.AddPage;
    D.SaveToFile('Test1.pdf');     // Ok, create valid empty PDF

    D.NewDoc;
    D.AddPage;
    D.SaveToFile('Test2.pdf');     // Fails with AV.
  finally
    D.Free;
  end;
end;

Remove the encryption from this example and both fiels will be saved OK.

Thank you,

Petr

Offline

#2 2016-09-26 12:53:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: AV saving encrypted PDF

Why just not create anothe TPdfDocument instance?

Offline

#3 2016-09-27 08:11:58

PB
Member
Registered: 2016-09-26
Posts: 2

Re: AV saving encrypted PDF

Hello,

Thank you for you reply.

Creating another TPdfDocument instance should of course help. But I wanted to inform you that there is probably a bug in the SynPdf code, because the procedure that woked fine without encryption (the NewDoc method) suddenly has stopped working when using with the TPdfEncryption. The description of the NewDoc menthod says:

    /// create a new document
    // - this method is called first, by the Create constructor
    // - you can call it multiple time if you want to reset the whole document content
    procedure NewDoc;

There is no mention that it cannot be used for encrypted documents, so I believe that this is a bug rather than a feature by design. This may also confuse another users; you can find a post called "How to create a protected PDF ?" by fantoni (2015-04-22) that is related to the same bug.

Sincerely

Petr

Offline

Board footer

Powered by FluxBB