Unit uTextPaging;

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

Interface

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

Type
  TForm1 = Class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Procedure Button1Click(Sender: TObject);
    Procedure Button2Click(Sender: TObject);
    Procedure Button3Click(Sender: TObject);
    Procedure Button4Click(Sender: TObject);
    Procedure Button5Click(Sender: TObject);
    Procedure Button6Click(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
  End;

Var
  Form1: TForm1;
Implementation
{$R *.dfm}

Procedure TForm1.Button1Click(Sender: TObject);
Var
  PDF: TPDFlib;
  FontID, FontID2, FontID3: Integer;
  PStream: TMemoryStream;
Begin
  PDF := TPDFlib.Create;
  Try
    Begin
      PStream := TMemoryStream.Create;
      PDF.SetPageSize('A5');
      PDF.SetOrigin(1);           // Set the origin to the top-left corner
      PDF.SetMeasurementUnits(1); // Set the measurement units to millimeters
      // ShowMessage('PageSize: ' + IntToStr(Trunc(PDF.PageWidth)) + ' * ' + IntToStr(Trunc(PDF.PageHeight)));
      // FontID := PDF.AddStandardFont(4); // Add a standard font, Helvetica
      FontID := PDF.AddTrueTypeFont('TimesNewRoman {1250}', 1);
      FontID2 := PDF.AddTrueTypeFont('Helvetica {1250}', 1);
      FontID3 := PDF.AddTrueTypeFont('Arial {1250}', 1);
      PDF.SelectPage(1);
      PDF.SelectFont(FontID);
      PDF.SetTextColor(255, 0, 0);
      PDF.SetTextSize(14);
      PDF.DrawText(5, 5, 'TimesNewRoman: przyjęcie materiału');
      PDF.SelectFont(FontID2);
      PDF.SetTextColor(255, 0, 0);
      PDF.SetTextSize(14);
      PDF.DrawText(5, 15, 'Helvetica: przyjęcie materiału');
      PDF.SelectFont(FontID3);
      PDF.SetTextColor(255, 0, 0);
      PDF.SetTextSize(14);
      PDF.DrawText(5, 25, 'Arial: przyjęcie materiału');
      PDF.DrawLine(0, 30, PDF.PageWidth, 30);
      PDF.NewPage;
      PDF.SelectPage(2);
      PDF.SelectFont(FontID);
      PDF.SetTextColor(255, 0, 0);
      PDF.SetTextSize(14);
      PDF.DrawText(5, 5, 'Text on page two.');
      PDF.SaveToStream(PStream);
      PStream.SaveToFile('memstream1.dat');
      PStream.Free;
      PDF.SaveToFile('memstream1.pdf')
    End
  Finally
    PDF.Free;
  End;
End;

Procedure TForm1.Button2Click(Sender: TObject);
Var
  PDF: TPDFlib;
  ImageID: Integer;
  Image: TImage;
  PStream, IStream: TMemoryStream;
Begin
  PDF := TPDFlib.Create;
  Try
    Begin
      PStream := TMemoryStream.Create;
      PStream.LoadFromFile('memstream1.dat');
      PDF.LoadFromStream(PStream, '');
      PDF.SetOrigin(1);
      PDF.SetMeasurementUnits(1); // Set the measurement units to millimeters
      // ShowMessage('PageSize: ' + IntToStr(Trunc(PDF.PageWidth)) + ' * ' + IntToStr(Trunc(PDF.PageHeight)));
      PDF.SelectPage(2);
      Image := TImage.Create(Nil);
      Image.Picture.LoadFromFile('Image1.bmp');
      IStream := TMemoryStream.Create;
      Image.Picture.Bitmap.SaveToStream(IStream);
      ImageID := PDF.AddImageFromStream(IStream, 0);
      PDF.SelectImage(ImageID);
      // PDF.DrawImage(8, 30, 180 * 0.2645833333, 160 * 0.2645833333);
      // if we use the width height value show in Windows, we need to multiply it by 0.2645833333, 1 pixel=0.2645833333 mm
      PDF.DrawImage(8, 30, Image.Width, Image.Height);
      // if we use Image.Width & Image.Height, do not need to multiply it by 0.2645833333
      PStream.Seek(0, soFromBeginning);
      PDF.SaveToStream(PStream);
      PStream.SaveToFile('memstream2.dat');
      PStream.Free;
      PDF.SaveToFile('memstream2.pdf');
    End
  Finally
    PDF.Free;
  End;
End;

Procedure TForm1.Button3Click(Sender: TObject);
Var
  PDF: TPDFlib;
  FontID: Integer;
  PStream: TMemoryStream;
  Text, OverflowOut: String;
Begin
  PDF := TPDFlib.Create;
  Try
    Begin
      PStream := TMemoryStream.Create;
      PStream.LoadFromFile('memstream2.dat');
      PDF.LoadFromStream(PStream, '');
      PDF.SetOrigin(1);                // Set the origin to the top-left corner
      PDF.SetMeasurementUnits(1);      // Set the measurement units to millimeters
      PDF.SetPageDimensions(148, 210); // A5
      //FontID := PDF.AddTrueTypeFont('TimesNewRoman {1250}', 1);
      FontID := PDF.SetHTMLNormalFont('Default', PDF.AddTrueTypeFont('Arial {1250}', 1));
      PDF.SetHTMLBoldFont('Default', PDF.AddTrueTypeFont('Helvetica Bold {1250}', 1));
      PDF.SelectPage(2);
      PDF.SelectFont(FontID);
      PDF.SetTextSize(16);
      //PDF.DrawText(5, 25, 'Next text on page two.');

      //PDF.DrawLine(0, 32, PDF.PageWidth, 32);
      //Text := 'Line 1' + chr(13) + chr(10) + 'Line 2' + chr(13) + chr(10) +
      //  'Line 3' + chr(13) + chr(10) + 'Line 4' + chr(13) + chr(10) + 'Line 5' +
      //  chr(13) + chr(10) + 'Line 6' + chr(13) + chr(10) + 'Line 7';
      //PDF.DrawMultiLineText(5, PDF.PageHeight - 20, chr(13) + chr(10), Text);
      Text := 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas vestibulum fermentum dui, id sagittis mi varius id. '
        + 'Morbi at eros vitae magna lobortis bibendum vitae dignissim lectus. Vestibulum ante ipsum primis in faucibus orci luctus et '
        + 'ultrices posuere cubilia Curae; Praesent posuere consequat arcu quis ultricies. Suspendisse varius augue non lorem tristique, at'
        + 'semper diam luctus. Pellentesque accumsan eu velit eu facilisis. Integer ac massa urna. Nunc nibh tortor, posuere ac finibus ac, '
        + 'dapibus et neque. Vivamus elit nisl, tristique ac dictum blandit, tempor et dolor. Vivamus gravida tellus eros, quis interdum '
        + 'turpis congue congue. Vivamus nec risus eget ligula ultrices pulvinar eu quis urna. In porta, quam in aliquam porta, eros dui '
        + 'mollis lorem, quis ultricies lorem turpis quis ex. Duis aliquet quam nunc. <br><br>'
        + 'Donec in dapibus nisi, a malesuada nulla. Praesent nec euismod felis, sed iaculis nunc. In luctus nulla quis semper posuere. '
        + 'Curabitur eget tempor odio. Phasellus porta hendrerit ante, eu fringilla diam efficitur sit amet. Morbi maximus aliquam vehicula. '
        + 'Nam pellentesque ullamcorper justo, id facilisis nunc viverra ut. Ut convallis porta enim id imperdiet. In urna ipsum, efficitur '
        + 'id consectetur id, ultricies consequat nisi. Nam ac tempor nunc. Nulla in lorem ut nunc cursus fermentum. <br><br>'
        + 'Donec hendrerit eget nisl vitae sagittis. Cras placerat ipsum porta lacus malesuada, et condimentum tellus finibus. Sed laoreet '
        + 'congue tellus, ac tempor dui. Donec metus tortor, convallis sed erat a, imperdiet rhoncus felis. Duis placerat mollis hendrerit. '
        + 'Quisque rutrum ligula vel urna dapibus, pulvinar suscipit nulla ultrices. Praesent consequat quam et tellus gravida, vel vehicula '
        + 'erat tincidunt. Duis gravida vehicula urna, id placerat tortor convallis eget. Integer velit enim, tincidunt vel neque elementum, '
        + 'tempor auctor sapien.';

      OverflowOut := PDF.DrawHTMLTextBox(10, 140, PDF.PageWidth() - 20, 210-140-10, Text);
      // A5 page height 210 minus the reserved height 140 and bottom margin 10
      while (OverflowOut.Length <> 0) do
      begin
        PDF.NewPage();
        // Draw the remainder of the text
        OverflowOut := PDF.DrawHTMLTextBox(10, 10, PDF.PageWidth() - 20, 210-20, OverflowOut);
        // A5 page height 210 minus the top & bottom margin 20
      end;


      PStream.Seek(0, soFromBeginning);
      PDF.SaveToStream(PStream);
      PStream.SaveToFile('memstream3.dat');
      PStream.Free;
      PDF.SaveToFile('memstream3.pdf');
    End
  Finally
    PDF.Free;
  End;
End;

Procedure TForm1.Button4Click(Sender: TObject);
Var
  PDF: TPDFlib;
  Bitmap: TBitmap;
  PStream, IStream: TMemoryStream;
Begin
  PDF := TPDFlib.Create;
  Try
    Begin
      PStream := TMemoryStream.Create;
      PStream.LoadFromFile('memstream3.dat');
      PDF.LoadFromStream(PStream, '');
      PDF.SetOrigin(1);
      PStream.Seek(0, soFromBeginning);
      PDF.DrawText(10, 190, 'Image added from memory stream.');
      PDF.SaveToStream(PStream);
      IStream := TMemoryStream.Create;
      IStream.Seek(0, soFromBeginning);
      Bitmap := TBitmap.Create;
      IStream.Seek(0, soFromBeginning);
      Bitmap.LoadFromStream(IStream);
      Image1.Picture.Assign(Bitmap);
      PDF.SaveToFile('memstream3.pdf');
      ShellExecute(Handle, 'open', 'memstream3.pdf', Nil, Nil, SW_SHOWNORMAL);
      Bitmap.Free;
      PStream.Free;
      IStream.Free;
    End
  Finally
    PDF.Free;
  End;
End;

Procedure TForm1.Button5Click(Sender: TObject);
Var
  PDFlib: TPDFlib;
  CustPrinter: WideString;
  PrintOpt: Integer;
  PStream: TMemoryStream;
Begin
  PDFlib := TPDFlib.Create;
  PStream := TMemoryStream.Create;
  PStream.LoadFromFile('memstream3.dat');
  Try
    With PDFlib Do
    Begin
      LoadFromStream(PStream, '');
      CustPrinter := NewCustomPrinter(GetDefaultPrinterName);
      // A5 paper size
      SetupPrinter(CustPrinter, 1, 11);
      // ShowMessage(SetupPrinter(CustPrinter, 11, 1).tostring);
      // portrait orientation
      If SetupPrinter(CustPrinter, 11, 1) = 1
      Then
      Begin
        PrintOpt := PrintOptions(1, 0, 'A5 Portrait Page');
        // Set PageScaling to 1, Fit to paper
        PrintDocument(CustPrinter, 1, PageCount, PrintOpt);
      End;
    End;
  Finally
    PDFlib.Free;
  End;
End;

Procedure TForm1.Button6Click(Sender: TObject);
Var
  PDF: TPDFlib;
  FontID, FontID2, FontID3, ImageID: Integer;
  Image: TImage;
  Bitmap: TBitmap;
  PStream, IStream: TMemoryStream;
Begin
  PDF := TPDFlib.Create;
  Try
    Begin
      PDF.SetPageSize('A5');
      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
      // FontID := PDF.AddTrueTypeFont('ARIAL{1250}',1);
      FontID := PDF.AddTrueTypeFont('TimesNewRoman {1250}', 1);
      FontID2 := PDF.AddTrueTypeFont('Helvetica {1250}', 1);
      FontID3 := PDF.AddTrueTypeFont('Arial {1250}', 1);
      PStream := TMemoryStream.Create;
      PDF.SelectPage(1);
      PDF.SelectFont(FontID);
      PDF.SetTextColor(255, 0, 0);
      PDF.SetTextSize(14);
      PDF.DrawText(5, 5, 'TimesNewRoman: I do see serif in letters C c T I');
      PDF.SelectFont(FontID2);
      PDF.SetTextColor(255, 0, 0);
      PDF.SetTextSize(14);
      PDF.DrawText(5, 15, 'Helvetica: przyjęcie materiału.');
      PDF.SelectFont(FontID3);
      PDF.SetTextColor(255, 0, 0);
      PDF.SetTextSize(14);
      PDF.DrawText(5, 25, 'Arial: Letters look ok. Letters C c T I.');
      PDF.DrawLine(0, 30, PDF.PageWidth, 30);
      {

       //showmessage(PDF.GetInstalledFontsByCharset(16,0));

       PDF.SetTextSize(8);
       PDF.DrawText(5, 15,
       'przyjęcie materiału: 19.04.2017 godz. 12:12');

       PDF.SetTextSize(10);
       PDF.DrawText(5, 25, 'Hello world.');

       PDF.SetTextSize(8);
       PDF.DrawText(5, 35, widechar(260);
      }
      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, 120, 180, 160);
      PStream.Seek(0, soFromBeginning);
      PDF.SaveToStream(PStream);
      PDF.LoadFromStream(PStream, '');
      PDF.SetOrigin(1);
      PStream.Seek(0, soFromBeginning);
      PDF.DrawText(8, 280, '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.
