You are not logged in.
Hello everyone,
I wrote this simple code in Delphi to create form more jpg files contenent in to a directory on a single pdf file:
procedure TForm1.MakePdf;
var
pdf: TPdfDocumentGDI;
i: Integer;
pict: TPicture;
FileImmagine:string;
terminato:boolean;
begin
pdf:=TPdfDocumentGDI.Create;
pdf.Info.Author:='';
pdf.Info.Creator:='';
pdf.DefaultPaperSize:=psA4;
pdf.ForceJPEGCompression:=80;
pdf.CompressionMethod:=cmFlateDecode;
pict:= TPicture.Create;
for i := 0 to FileListBoxEx1.Count - 1 do
begin
FileImmagine:= FileListBoxEx1.Items.Strings[i];
pict.LoadFromFile(FileImmagine);
with pdf.AddPage do
begin
PageWidth:=Round(pict.Width *0.73);
PageHeight:=Round(pict.Height * 0.73);
end;
pdf.VCLCanvas.Draw(0,0,pict.Graphic);
FileListBoxEx1.Selected[i] := true;
StatusBar1.Panels[0].Text:='Process page '+inttostr(i)+' of '+inttostr(FileListBoxEx1.Count)+' wait...';
Application.ProcessMessages;
end;
try
pdf.SaveToFile('C:\Test.pdf');
StatusBar1.Panels[0].Text:='Create pdf wait...';
finally
pict.Free;
pdf.Free;
StatusBar1.Panels[0].Text:='Pdf document is created...';
end;
ShellExecute(Application.MainForm.Handle,'open',Pchar(nomefilepdf),
nil,nil,SW_NORMAL);
end;
I tried inserting a seven jpg image file in to directory.
When I go to run the procedure, the result is that you create a pdf with seven pages of which only the first two are correct.
The remaining five pages contain the second image instead of the subsequent images.
Does anybody know kindly tell me where am I wrong .......
Thanks
Offline
Put a breakpoint in TPdfDocument.CreateOrGetImage in line
result := GetXObjectImageName(Hash,bmi^.bmiHeader.biWidth,bmi^.bmiHeader.biHeight);
It should each time return ''.
Then it should each time create a 'SynImg1', 'SynImg2', 'SynImg3'.... and such unique names for each picture.
Does it happen?
Offline
Thanks for your help ...
any image has a unique name for example: 1.jpg, 2.jpeg, 3.jpg, 4.jpg. etc....
Did not understand how to use the recommended code to the breakpoint.
Should I use result: boolean with a timer1?
Could you kindly make an explicit example of how to use pdf.GetXObjectImageName ..
many thanks.
Offline
Hi
to solve the problem I had to rewrite the code.
Now it seems to successfully create the pdf document but I can not diminish the quality ... where am I wrong?
procedure TForm1.CreazionePdf;
var
pdfDoc:TPdfDocument;
pdfpage:TPdfpage;
JpegImage:TJpegImage;
pdfImage:TPdfImage;
i:integer;
begin
pdfDoc := TPdfDocument.Create;
pdfDoc.Info.Author:='';
pdfDoc.Info.Creator:='';
pdfDoc.DefaultPaperSize:=psA4;
pdfDoc.ForceJPEGCompression:=20;
pdfDoc.CompressionMethod:=cmFlateDecode;
JpegImage := TJpegImage.Create;
for i := 0 to FileListBoxEx1.Count - 1 do
begin
JpegImage.LoadFromFile( FileListBoxEx1.Items.Strings[i]);
pdfpage := pdfDoc.AddPage;
pdfpage.PageWidth:=Round(jpegimage.Width *0.73);
pdfpage.PageHeight:=Round(jpegimage.Height * 0.73);
pdfDoc.ForceJPEGCompression:=70;
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.ForceJPEGCompression:=70;
FileListBoxEx1.Selected[i] := true;
StatusBar1.Panels[0].Text:='Process page '+inttostr(i)+' of '+inttostr(FileListBoxEx1.Count)+' wait...';
Application.ProcessMessages;
end;
pdfDoc.SaveToFile(nomefilepdf);
StatusBar1.Panels[0].Text:='Create pdf wait...';
pdfDoc.Free;
end;
Offline
Sorry but there is only pdfDoc.ForceJPEGCompression one correct code and still does not work......the pdf file size is always the same
procedure TForm1.CreazionePdf;
var
pdfDoc:TPdfDocument;
pdfpage:TPdfpage;
JpegImage:TJpegImage;
pdfImage:TPdfImage;
i:integer;
begin
pdfDoc := TPdfDocument.Create;
pdfDoc.Info.Author:='';
pdfDoc.Info.Creator:='';
pdfDoc.DefaultPaperSize:=psA4;
pdfDoc.CompressionMethod:=cmFlateDecode;
//pdfDoc.ForceJPEGCompression:=Qualitapdf;
pdfDoc.ForceJPEGCompression:=20 ;
JpegImage := TJpegImage.Create;
for i := 0 to FileListBoxEx1.Count - 1 do
begin
JpegImage.Compress;
JpegImage.LoadFromFile( FileListBoxEx1.Items.Strings[i]);
pdfpage := pdfDoc.AddPage;
pdfpage.PageWidth:=Round(jpegimage.Width *0.73);
pdfpage.PageHeight:=Round(jpegimage.Height * 0.73);
pdfImage := TPdfImage.Create(pdfDoc,jpegimage,true);
pdfDoc.AddXObject('image'+inttostr(i), pdfimage);
pdfDoc.Canvas.DrawXObject(0,0,pdfpage.PageWidth,pdfpage.PageHeight,'image'+inttostr(i));
FileListBoxEx1.Selected[i] := true;
StatusBar1.Panels[0].Text:='Process page '+inttostr(i)+' of '+inttostr(FileListBoxEx1.Count)+' wait...';
Application.ProcessMessages;
end;
pdfDoc.SaveToFile(nomefilepdf);
StatusBar1.Panels[0].Text:='Create pdf wait...';
pdfDoc.Free;
end;
Offline
After several unsuccessful attempts I put the unit in my project SynGdiPlus and ...... now the compression of pdf files working properly.
Can anyone tell me why?
Thanks ...
Offline
You need the TJpegImage component from SynGdiPlus, not the one supplied with Delphi, from Jpeg unit.
Our SynPdf library use SynGdiPlus for handling its JPEG compression.
and with SynGdiPlus, your exe size will be smaller than with Jpeg unit
Offline
Well then I can say that before was not working because I did not put the unit SynGdiPlus in the project ... right?
Thank you for your kind help
Offline
Ok ab.... thank you for your kind help
Offline