#1 2010-08-28 08:57:26

fish
Member
Registered: 2010-08-28
Posts: 3

Convert a Jpeg file to pdf

Hello:

      My software need convet some jpg file to a pdf file,i try it to use the code:

procedure TForm1.Button2Click(Sender: TObject);
var
  testpdf:TPdfDocumentGDI;
begin
  testpdf:=TPdfDocumentGDI.Create;
  try
    testpdf.NewDoc;
    try
      testpdf.AddPage;
      image1.Picture.LoadFromFile('c:\pictest.jpg');  //Image1: TImage;
      testpdf.VCLCanvas.Draw(0,0,image1.Picture.Graphic);
    finally
      testpdf.SaveToFile('test.pdf');
    end;
  finally
    testpdf.Free;
  end;
end;

but i don't know the way is correct?

btw: should write more example to show how to use the componet

Offline

#2 2010-08-28 09:08:18

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

Re: Convert a Jpeg file to pdf

Sounds fine to me.
What's your problem with the above code?

You'd better use StretchDraw instead of Draw to force the dimension of the picture on the page.
Jpg files can have diverse dpi.

You can set some value to the ForceJPEGCompression property, in order to compress the image inside the pdf as jpeg, if you want to save some space.
If you leave the ForceJPEGCompression property to its default value (i.e. 0), it will embed the jpg as bitmap with deflate compression, which will create bigger pdf files.

Offline

#3 2010-09-03 10:12:52

fish
Member
Registered: 2010-08-28
Posts: 3

Re: Convert a Jpeg file to pdf

I  modify the code :

procedure TForm1.Button2Click(Sender: TObject);
var
  testpdf:TPdfDocumentGDI;
  tmprect:TRect;
begin
  testpdf:=TPdfDocumentGDI.Create;
  try
    testpdf.NewDoc;
    try
      testpdf.AddPage;
      image1.Picture.LoadFromFile('c:\pictest.jpg');  //Image1: TImage;
      testpdf.ForceJPEGCompression:=30;
    tmprect.Left:=0;
    tmprect.Top:=0;
    tmprect.Bottom:=image1.Height;
    tmprect.Right:=image1.Width;
    testpdf.VCLCanvas.StretchDraw(tmprect,image1.Picture.Graphic);
   
    finally
      testpdf.SaveToFile('test.pdf');
    end;
  finally
    testpdf.Free;
  end;
end;


the file of test.pdf isn't correct(The size of test.pdf is only 1k and  Adobe Reader 9 can't open test.pdf);

Offline

Board footer

Powered by FluxBB