#1 2015-04-22 17:01:45

fantoni
Member
Registered: 2011-02-25
Posts: 16

How to create a protected PDF ?

Hi,
I use this code on Delphi XE7 to create a pdf document on multiple pages .

The procedure works well if you do not protect the pdf .

If I protect the file returns error " Access Violation.."

Where is the error ?

and eventually...i can create a pdf file protected from a pdf file unprotected ?

Thanks......

Fantoni

procedure TForm1.Button2Click(Sender: TObject);
var
pdfDoc:TPdfDocument;
pdfDocProtect:TPdfDocument;
pdfpage:TPdfpage;
JpegImage:TJPEGImage;
pdfImage:TPdfImage;
fs: TFileStream;
i:integer;
begin
Qualitapdf:=SpinEdit1.value;

//Make protect document
if length (LabeledEditPdfPw.text)>0 then
pdfDoc := TPdfDocument.Create(false,0,false,TPdfEncryption.New(elRC4_128,LabeledEditPdfPw.text,LabeledEditPdfPw.text,PDF_PERMISSION_NOCOPYNORPRINT))
else
pdfDoc :=TPdfDocument.Create;
JpegImage:= TJPEGImage.Create;
Nomefilepdf := percorso+'PdfFile\' + FormatDateTime( 'dd_mm_yy_hhnnss' , Now() ) +'_.pdf';
fs:= TFileStream.Create(nomefilepdf,fmCreate);

try
pdfDoc.NewDoc;
pdfDoc.Info.Author:='';
pdfDoc.Info.Creator:='';
pdfDoc.DefaultPaperSize:= psA4;
pdfDoc.DefaultPageLandscape:=PdfLandscape;
pdfDoc.CompressionMethod:=cmFlateDecode;
pdfDoc.ForceJPEGCompression:=Qualitapdf;
pdfDoc.SaveToStreamDirectBegin(fs);
Sb.Panels[0].Text:='Create pdf wait...';

for i := 0 to FileListBox1.Count - 1 do
begin
FileListBox1.Selected[i] := true;
JpegImage.LoadFromFile(FileListBox1.Items.Strings[i]);
pdfpage:= pdfDoc.AddPage;
pdfImage := TPdfImage.Create(pdfDoc, JpegImage, true);
pdfDoc.AddXObject('image' + IntToStr(i), pdfImage);
pdfDoc.Canvas.DrawXObject(0, 0, pdfpage.PageWidth, pdfpage.PageHeight, 'image' + IntToStr(i));
pdfDoc.SaveToStreamDirectPageFlush(True);
end;
pdfDoc.SaveToStreamDirectEnd;
//pdfDoc.SaveToFile(nomefilepdf);
finally
fs.Free;
pdfDoc.Free;
JpegImage.Free;
ShowMessage('Pdf Create!!');
ShellExecute(Application.MainForm.Handle,'open',Pchar(nomefilepdf),nil,nil,SW_NORMAL);
  end;

end;

Last edited by fantoni (2015-04-22 17:02:38)

Offline

Board footer

Powered by FluxBB