#1 2022-12-14 11:24:01

rvk
Member
Registered: 2022-04-14
Posts: 115

Problem with resaving an encrypted PDF

There still seems to be a problem with password protecting a PDF.
When I try to resave a password protected PDF (generated from SynPDF), Adobe Reader complains with the message

The document could not be saved. Bad parameter.

Code to duplicate this behavior:

uses SynPdf;

procedure MakePdfSynPdf;
var
  FileTemp: string;
  PDF: TPdfDocumentGDI;
  Enc: TPdfEncryption;
begin
  FileTemp := 'C:\Temp\Test1.pdf';
  Enc := TPdfEncryption.New(elRC4_40 { elRC4_128 same result } , '', 'testpassword', PDF_PERMISSION_NOCOPY);
  PDF := TPdfDocumentGDI.Create(false, 0, false, Enc);
  try
    PDF.Info.Author := 'User name';
    PDF.Info.Creator := 'Software name';
    PDF.Info.subject := 'Auto generated document';
    PDF.CompressionMethod := cmNone;
    PDF.EmbeddedTTF := true;
    PDF.EmbeddedTTFIgnore.Text := RawUTF8('Arial'#13#10'Times New Roman'#13#10'Courier New'#13#10'Symbol'#13#10'WingDings');;
    PDF.EmbeddedWholeTTF := true;
    PDF.Root.ViewerPreference := [vpFitWindow];
    PDF.DefaultPageLandscape := false;
    PDF.AddPage;
    PDF.VCLCanvas.Font.Name := 'Arial';
    PDF.VCLCanvas.Font.size := 20;
    PDF.VCLCanvas.TextOut(400, 400, 'Test');
    PDF.SaveToFile(FileTemp);
    // ExecAssociatedApp(FileTemp);
  finally
      PDF.Free;
  end;
end;

Open C:\Temp\Test1.pdf with Adobe Reader (Windows) and try to save it to the desktop.
Without password it works just fine.

Offline

#2 2022-12-14 11:44:59

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

Re: Problem with resaving an encrypted PDF

My guess is that it is because you specified PDF_PERMISSION_NOCOPY.

You deny to make any copy - so you deny any save?

Offline

#3 2022-12-14 12:19:27

rvk
Member
Registered: 2022-04-14
Posts: 115

Re: Problem with resaving an encrypted PDF

ab wrote:

My guess is that it is because you specified PDF_PERMISSION_NOCOPY.

You deny to make any copy - so you deny any save?

Woops, yes. But in my problem code I have this:

Enc := TPdfEncryption.New(elRC4_40 { elRC4_128 same result } , '', 'testpassword', PDF_PERMISSION_NOMODIF + [epFillingForms, epAuthoringComment]);

(Try it yourself with these options)

With this it works

Enc := TPdfEncryption.New(elRC4_40 { elRC4_128 same result } , '', 'testpassword', PDF_PERMISSION_NOMODIF);

Why would adding epFillingForms and epAuthoringComment make Adobe not be able to save?

These are the permissions when doing that:
qUYUIiq.png

When merging a PDF with pdftk and including the same options it does work. Permissions:
l2XbFLi.png

Those permissions are the same but with the SynPDF in Adobe Reader I get the error when "saving as".

I found this out because normally the SynPDF goes through pdftk.exe to merge a background and there is no problem with saving.
But when creating a "blanc" PDF (without background), so clean from SynPDF, saving as in Adobe isn't possible.

Edit:
It's also a problem if you just use PDF_PERSMISSION_NOPRINT. Copying should be allowed then.
In PDF_PERSMISSION_NOPRINT the epContentCopy and epAuthoringComment are included.

Examining further, it seems to happen when epAuthoringComment is included, while this is no problem with another PDF generator.

Edit #2: BTW. Subsequently this also doesn't work because it includes epAuthoringComment.

Enc := TPdfEncryption.New(elRC4_40 { elRC4_128 same result } , '', 'testpassword', PDF_PERMISSION_ALL);

Then I thought this would work but it also doesn't:

Enc := TPdfEncryption.New(elRC4_40 { elRC4_128 same result } , '', 'testpassword', PDF_PERMISSION_ALL - [epAuthoringComment]);

(Also epContentCopy is only for Content copying. "Save as" should always work in Adobe reader. Even with the most strict PDF.)

Last edited by rvk (2022-12-14 13:59:19)

Offline

Board footer

Powered by FluxBB