Unit MainForm;

{$I ..\..\..\Lib\PDFlibDefines.inc}

Interface

Uses
{$IFDEF XE2+}
  Winapi.Windows,
  System.Classes,
  System.SysUtils,
  Vcl.Controls,
  Vcl.StdCtrls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.Graphics,
  Vcl.ExtCtrls,
  Winapi.ShellAPI,
{$ELSE}
  Windows,
  Classes,
  SysUtils,
  Controls,
  StdCtrls,
  Forms,
  Dialogs,
  Graphics,
  ExtCtrls,
  ShellAPI,
{$ENDIF}
  PDFlibrary;

Type
  TForm1 = Class(TForm)
    Image1: TImage;
    Button1: TButton;
    Procedure Button1Click(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
  End;

Var
  Form1: TForm1;

Implementation

{$R *.dfm}

Procedure TForm1.Button1Click(Sender: TObject);
Var
  PDF: TPDFlib;
  FontID, ImageID: Integer;
  Image: TImage;
  Bitmap: TBitmap;
  PStream, IStream: TMemoryStream;
Begin
  PDF := TPDFlib.Create;
  Try
    Begin
      PDF.SetPageSize('A4');
      PDF.SetOrigin(1); // Set the origin to the top-left corner
      PDF.SetMeasurementUnits(1); // Set the measurement units to millimeters
      FontID := PDF.AddStandardFont(4); // Add a standard font, Helvetica

      PStream := TMemoryStream.Create;
      PDF.SelectPage(1);
      PDF.SelectFont(FontID);
      PDF.SetTextColor(255, 0, 0);
      PDF.SetTextSize(11);
      PDF.DrawText(5, 5, 'Hello world.');
      PDF.SaveToStream(PStream);

      Image := TImage.Create(Nil);
      Image.Picture.LoadFromFile('Image1.bmp');
      IStream := TMemoryStream.Create;
      Image.Picture.Bitmap.SaveToStream(IStream);

      PDF.LoadFromStream(PStream, '');
      PDF.SetOrigin(1);
      ImageID := PDF.AddImageFromStream(IStream, 0);
      PDF.SelectImage(ImageID);
      PDF.DrawImage(8, 20, 280, 160);
      PStream.Seek(0, soFromBeginning);
      PDF.SaveToStream(PStream);

      PDF.LoadFromStream(PStream, '');
      PDF.SetOrigin(1);
      PStream.Seek(0, soFromBeginning);
      PDF.DrawText(10, 190, 'Image added from memory stream.');
      PDF.SaveToStream(PStream);

      IStream.Seek(0, soFromBeginning);
      PDF.RenderPageToStream(96, 1, 0, IStream);

      Bitmap := TBitmap.Create;
      IStream.Seek(0, soFromBeginning);
      Bitmap.LoadFromStream(IStream);
      Image1.Picture.Assign(Bitmap);

      PDF.SaveToFile('DoInTheStream.pdf');
      ShellExecute(Handle, 'open', 'DoInTheStream.pdf', Nil, Nil, SW_SHOWNORMAL);
      Bitmap.Free;
      PStream.Free;
      IStream.Free;

    End
  Finally
    PDF.Free;
  End;
End;

End.
