Append pages from Images

<< Click to Display Table of Contents >>

Navigation:  Tasks >

Append pages from Images

 

This code opens a TOpenDialog and the user can select one or more JPEG images.

 

The the necessary count of pages will be appended to the loaded document and on each page an image will be placed. The viewer will display the complete document with the added images immediately and, when the PDF is saved, the images will be added permanently into the PDF.

(requires WPViewPDF 5 PLUS or later)

 

 

procedure TForm1.btnAppendPageWithJPEGClick(Sender: TObject);
var
    jpeg : TJPEGImage; // uses Vcl.Imaging.jpeg
    i, page_id, image_id, w,h : Integer;
begin
    jpeg := TJPEGImage.Create;
    try
        OpenDialog1.Filter := 'Image Files (*.JPG)|*.JPG;*.JPEG';
        OpenDialog1.Options := [ofAllowMultiSelect,ofFileMustExist];
 
        if OpenDialog1.Execute and (OpenDialog1.Files.Count>0) then
        begin
           wpviewpdf1.command(COMPDF_BeginUpdate);
           try
             for i := 0 to OpenDialog1.Files.Count-1 do
             begin
                   jpeg.LoadFromFile( OpenDialog1.Files[i] );
                   h := MulDiv( jpeg.Height, 72, 96 );
                   w := MulDiv( jpeg.Width, 72, 96 );
                   page_id := wpviewpdf1.CommandStrEx( 

                        COMPDF_AppendPage,
                        '',  
                        h +       // Hight in lo word
                        w shl 16   // Width in high word
                        );
                   if page_id>0 then
                   begin
                     image_id := wpviewpdf1.Plus.AddImage(  

                           OpenDialog1.Files[i] );
                     if image_id>0 then
                     begin
                       wpviewpdf1.Plus.UseImage(image_id,
                              page_id,
                              0, 0,  // X,Y
                              w,
                              h,
                              0, {angle}
                              []);
                     end;
                   end;
             end;
           finally
              wpviewpdf1.command(COMPDF_EndUpdate);
           end;
        end;
    finally
      jpeg.Free;
    end;
end;