You are not logged in.
Pages: 1
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
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
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
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
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
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
Pages: 1