#1 2011-02-25 12:13:45

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

Join muliple Jpg image to Pdf file

Hi,
How i can  Join all Jpg images files contained in a directory into a single PDF file while maintaining the proportions?

Thanks to all.....

Fantoni

Offline

#2 2011-02-25 13:31:17

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

Re: Join muliple Jpg image to Pdf file

Use a TPdfDocument class.

Creates the instance, then call AddPage for each page.

For each file, create a TPdfImage instance via the CreateJpegDirect constructor, supplying the file name, then register it via TPdfDocument.AddXObject() supplying an unique object name (like 'Jpeg1', 'Jpeg2'...).

Then draw the object name on the page, via Canvas.DrawXObject(), setting the appropriate rectangle to maintain the proportions.

Using CreateJpegDirect() will embed the JPG file as it is. Depending on the expected resolution and the compression of the jpeg file, the resulting pdf can be huge.
So if you want to create some lighter pdf, you can convert each Jpeg file into a bitmap with the expected dpi (e.g. 72 for on-screen resolution), then draw every bitmap on the Pdf with ForceJPEGCompression=80.

Offline

#3 2011-02-25 14:49:17

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

Re: Join muliple Jpg image to Pdf file

Thanks for your help.
I tried this code but the images are larger than the PDF page and do not maintain the proportions ... where am I wrong ....

procedure TForm1.SpeedButton5Click(Sender: TObject);
var
pdfDoc:TPdfDocument;
pdfpage:TPdfpage;
JpegImage:TJpegImage;
pdfImage:TPdfImage;

begin
pdfDoc := TPdfDocument.Create;
pdfpage := pdfDoc.AddPage;
JpegImage := TJpegImage.Create;
JpegImage.LoadFromFile('C:\SorgentiDelphi\Twain2\immagini\pag_1.jpg');
//JpegImage.ForceJPEGCompression:=90;
pdfImage := TPdfImage.Create(pdfDoc, jpegimage,true);
pdfDoc.AddXObject('image1', pdfimage);
pdfDoc.Canvas.DrawXObject(0, pdfpage.PageHeight-JpegImage.Height,
JpegImage.Width, JpegImage.Height, 'image1');
pdfDoc.SaveToFile('test2.pdf');
pdfDoc.Free;


Fantoni

Offline

#4 2011-02-25 15:10:25

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

Re: Join muliple Jpg image to Pdf file

You'll have to scale the JpegImage.Width, JpegImage.Height parameters according to the page width.

JpegImage.Width, JpegImage.Height are in pixels, whereas the expected parameters of DrawXObject are in mm.

Offline

#5 2011-02-25 17:24:04

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

Re: Join muliple Jpg image to Pdf file

Thanks but ....
My image of width 1653 and length 2338 pixels, but I can not let her in on a sheet of A4 paper 210 x295 mm ....

Offline

#6 2011-02-25 17:33:58

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

Re: Join muliple Jpg image to Pdf file

Ok I tried this way and now seems to be fine even if I am not convinced that the 'final image is not deformed ...


procedure TForm1.SpeedButton5Click(Sender: TObject);
var
pdfDoc:TPdfDocument;
pdfpage:TPdfpage;
JpegImage:TJpegImage;
pdfImage:TPdfImage;

begin
pdfDoc := TPdfDocument.Create;
pdfpage := pdfDoc.AddPage;
JpegImage := TJpegImage.Create;
JpegImage.LoadFromFile('C:\SorgentiDelphi\Twain2\immagini\pag_1.jpg');
//JpegImage.ForceJPEGCompression:=90;
pdfImage := TPdfImage.Create(pdfDoc, jpegimage,true);
//pdfImage:= TPdfImage.CreateJpegDirect(pdfDoc,'C:\SorgentiDelphi\Twain2\immagini\pag_1.jpg');
//pdfImage := TPdfImage.Create(pdfDoc, jpegimage);
pdfDoc.AddXObject('image1', pdfimage);
//pdfDoc.Canvas.DrawXObject(0,0,120,230, 'image1');


  pdfDoc.Canvas.DrawXObject(0, pdfpage.PageHeight-900,600, 900, 'image1');

//pdfDoc.Canvas.DrawXObject(0, pdfpage.PageHeight-JpegImage.Height,JpegImage.Width, JpegImage.Height, 'image1');

pdfDoc.SaveToFile('test2.pdf');
pdfDoc.Free;

Offline

Board footer

Powered by FluxBB