#1 2011-03-21 10:47:51

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

Problem creating pdf document from multiple jpeg file

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

#2 2011-03-21 13:09:16

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

Re: Problem creating pdf document from multiple jpeg file

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

#3 2011-03-21 13:53:51

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

Re: Problem creating pdf document from multiple jpeg file

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

#4 2011-03-21 15:51:03

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

Re: Problem creating pdf document from multiple jpeg file

The breakpoint is to be set inside SynPdf.pas, in the TPdfDocument.CreateOrGetImage method.

Perhaps there is a problem here.

Offline

#5 2011-03-22 09:45:24

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

Re: Problem creating pdf document from multiple jpeg file

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

#6 2011-03-22 10:22:14

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

Re: Problem creating pdf document from multiple jpeg file

AFAIK, the latest pdfDoc.ForceJPEGCompression value set will be used in TPdfImage.Create

So I guess only pdfDoc.ForceJPEGCompression := 70 is used.

Offline

#7 2011-03-22 10:44:30

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

Re: Problem creating pdf document from multiple jpeg file

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

#8 2011-03-22 16:42:15

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

Re: Problem creating pdf document from multiple jpeg file

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

#9 2011-03-22 18:25:34

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

Re: Problem creating pdf document from multiple jpeg file

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

#10 2011-03-23 10:19:09

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

Re: Problem creating pdf document from multiple jpeg file

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

#11 2011-03-23 12:05:02

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

Re: Problem creating pdf document from multiple jpeg file

Indeed: you should not use the Jpeg unit in your project, but SynGdiPlus instead.

Offline

#12 2011-03-24 07:26:27

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

Re: Problem creating pdf document from multiple jpeg file

Ok ab.... thank you for your kind help

Offline

Board footer

Powered by FluxBB