unit MainForm;
{$I ..\..\..\Lib\PDFlibDefines.inc}

interface

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

Type
  TfrmMain = Class(TForm)
    dlgSave: TSaveDialog;
    dlgOpen: TOpenDialog;
    pcTabs: TPageControl;
    tsGraphics: TTabSheet;
    btnGrid: TButton;
    btnCurves: TButton;
    tsImages: TTabSheet;
    btnLogo: TButton;
    btnMask: TButton;
    tsForms: TTabSheet;
    edtFName: TEdit;
    edtLName: TEdit;
    btnMakeForm: TButton;
    tsText: TTabSheet;
    btnRotatedText: TButton;
    Label7: TLabel;
    Label6: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    edtAge: TEdit;
    tsRender: TTabSheet;
    btnViewPrintLoadPDF: TButton;
    PreviewScrollBox: TScrollBox;
    imgPreview: TImage;
    cmbZoom: TComboBox;
    btnViewPrintPrevPage: TButton;
    btnViewPrintNextPage: TButton;
    btnPrint: TButton;
    dlgPrint: TPrintDialog;
    Label10: TLabel;
    Procedure btnGridClick(Sender: TObject);
    Procedure btnCurvesClick(Sender: TObject);
    Procedure btnLogoClick(Sender: TObject);
    Procedure btnMaskClick(Sender: TObject);
    Procedure btnMakeFormClick(Sender: TObject);
    Procedure btnRotatedTextClick(Sender: TObject);
    Procedure btnViewPrintLoadPDFClick(Sender: TObject);
    Procedure btnPrintClick(Sender: TObject);
    Procedure cmbZoomChange(Sender: TObject);
    Procedure btnViewPrintPrevPageClick(Sender: TObject);
    Procedure btnViewPrintNextPageClick(Sender: TObject);
    Procedure FormShow(Sender: TObject);
    Procedure FormClose(Sender: TObject; Var Action: TCloseAction);
    Procedure Label10MouseEnter(Sender: TObject);
    Procedure Label10MouseLeave(Sender: TObject);
    Procedure Label10Click(Sender: TObject);
  Private
    ViewPrint: TPDFlib;
    ViewPrintPageNum: Integer;
    Procedure RenderPage;
  Public
    { Public declarations }
  End;

Var
  frmMain: TfrmMain;

Implementation

{$R *.dfm}

Procedure TfrmMain.btnGridClick(Sender: TObject);
Var
  PDFlib: TPDFlib;
  X, Y: Integer;
Begin
  Randomize;
  dlgSave.InitialDir := ExtractFilePath(ParamStr(0));
  dlgSave.FileName := 'DemoGrid.pdf';
  If dlgSave.Execute Then
  Begin
    PDFlib := TPDFlib.Create;
    Try
      Begin
        // Set the paper size
        PDFlib.SetPageSize('A4');

        // Set the origin to the top-left corner
        PDFlib.SetOrigin(1);

        // Set the measurement units to millimeters
        PDFlib.SetMeasurementUnits(1);

        // Set the line color and width
        PDFlib.SetLineColor(1, 0, 0); // red
        PDFlib.SetLineWidth(0.6); // 0.6mm

        For X := 0 To 9 Do
        Begin
          For Y := 0 To 5 Do
          Begin
            // Last parameter is 0 = only draw outline
            PDFlib.DrawBox(5 + X * 20, 5 + Y * 25, 20, 25, 0);

            // Set the fill color
            PDFlib.SetFillColor(Random, Random * 0.5 + 0.5, Random * 0.5 + 0.5);

            // Draw a circle in the middle of the box
            PDFlib.DrawCircle(15 + X * 20, 17.5 + Y * 25, 8, 1);
          End;
        End;

        // Compress the contents of the file
        PDFlib.CompressContent;

        // Save the file
        PDFlib.SaveToFile(dlgSave.FileName);

      End
    Finally
      PDFlib.Free;
    End;
  End;
End;

Procedure TfrmMain.btnCurvesClick(Sender: TObject);
Var
  PDFlib: TPDFlib;
  X, Y: Integer;
  XPos: Double;
  YPos: Double;
  C: Integer;
Begin
  Randomize;
  dlgSave.InitialDir := ExtractFilePath(ParamStr(0));
  dlgSave.FileName := 'DemoCurves.pdf';
  If dlgSave.Execute Then
  Begin
    PDFlib := TPDFlib.Create;
    Try

      Begin
        // Set the paper size
        PDFlib.SetPageSize('A4');

        // Set the origin to the top-left corner
        PDFlib.SetOrigin(1);

        // Set the measurement units to millimeters
        PDFlib.SetMeasurementUnits(1);

        // Set the line width
        PDFlib.SetLineWidth(0.7); // 0.7mm

        For X := 1 To 2 Do
        Begin
          For Y := 1 To 2 Do
          Begin
            PDFlib.SetLineColor(Random, 0, Random);
            PDFlib.SetFillColor(Random, 0, Random);
            XPos := (X - 1) * 100 + 50;
            YPos := (Y - 1) * 100 + 50;
            PDFlib.StartPath(XPos + Random(40) - Random(40), YPos + Random(50) - Random(50));
            For C := 1 To Random(5) + 2 Do
            Begin
              PDFlib.AddCurveToPath(XPos + Random(40) - Random(40),
                YPos + Random(50) - Random(50), XPos + Random(40) - Random(40),
                YPos + Random(50) - Random(50), XPos + Random(40) - Random(40),
                YPos + Random(50) - Random(50));
            End;
            PDFlib.ClosePath;
            PDFlib.DrawPath(2);
          End;
        End;

        // Compress the contents of the file
        PDFlib.CompressContent;

        // Save the file
        PDFlib.SaveToFile(dlgSave.FileName);
      End
    Finally
      PDFlib.Free;
    End;
  End;
End;

Procedure TfrmMain.btnLogoClick(Sender: TObject);
Var
  PDFlib: TPDFlib;
  X: Integer;
  FontID: Integer;
  ImageID: Integer;
Begin
  Randomize;
  dlgSave.InitialDir := ExtractFilePath(ParamStr(0));
  dlgSave.FileName := 'DemoLogo.pdf';
  If dlgSave.Execute Then
  Begin
    PDFlib := TPDFlib.Create;
    Try
      Begin
        // Set the paper size
        PDFlib.SetPageSize('A4');

        // Set the origin to the top-left corner
        PDFlib.SetOrigin(1);

        // Set the measurement units to millimeters
        PDFlib.SetMeasurementUnits(1);

        // Add a standard font
        FontID := PDFlib.AddStandardFont(4); // Helvetica

        // Add the logo from an image file
        PDFlib.CompressImages(1);
        ImageID := PDFlib.AddImageFromFile(ExtractFilePath(ParamStr(0)) + 'DemoLogo.png', 0);

        // Add 9 pages, so there are a total of 10 pages in the document
        PDFlib.NewPages(9);

        For X := 1 To 10 Do
        Begin
          PDFlib.SelectPage(X);
          PDFlib.SelectImage(ImageID);
          PDFlib.DrawImage(50, 50, 22.4, 7.5);
          PDFlib.SelectFont(FontID);
          PDFlib.SetTextSize(16);
          PDFlib.SetTextColor(0, 0, 0);
        End;

        // Compress the contents of the file
        PDFlib.CompressContent;

        // Save the file
        PDFlib.SaveToFile(dlgSave.FileName);
      End
    Finally
      PDFlib.Free;
    End;
  End;
End;

Procedure TfrmMain.btnMaskClick(Sender: TObject);
Var
  PDFlib: TPDFlib;
Begin
  Randomize;
  dlgSave.InitialDir := ExtractFilePath(ParamStr(0));
  dlgSave.FileName := 'DemoMask.pdf';
  If dlgSave.Execute Then
  Begin
    PDFlib := TPDFlib.Create;
    Try
      Begin
        // Set the paper size
        PDFlib.SetPageSize('A4');

        // Set the origin to the top-left corner
        PDFlib.SetOrigin(1);

        // Set the measurement units to millimeters
        PDFlib.SetMeasurementUnits(1);

        // Add the logo from an image file
        PDFlib.CompressImages(1);
        PDFlib.AddImageFromFile(ExtractFilePath(ParamStr(0)) + 'DemoLogo.png', 0);

        PDFlib.SetFillColor(0, 0, 0.5);
        PDFlib.DrawBox(80, 80, 50, 50, 1);
        PDFlib.SetImageMask(0.8, 0.8, 0.8, 0.85, 0.85, 0.85); // 95% white - 100% white
        PDFlib.DrawScaledImage(50, 100, 1);

        // Compress the contents of the file
        PDFlib.CompressContent;

        // Save the file
        PDFlib.SaveToFile(dlgSave.FileName);
      End
    Finally
      PDFlib.Free;
    End;
  End;
End;

Procedure TfrmMain.btnMakeFormClick(Sender: TObject);
Var
  PDFlib: TPDFlib;
  FieldIndex: Integer;
  FontID: Integer;
Begin
  dlgSave.InitialDir := ExtractFilePath(ParamStr(0));
  dlgSave.FileName := 'DemoForm.pdf';
  If dlgSave.Execute Then
  Begin
    PDFlib := TPDFlib.Create;
    Try
      Begin
        // Set the paper size
        PDFlib.SetPageSize('A4');

        // Set the origin to the top-left corner
        PDFlib.SetOrigin(1);

        // Set the measurement units to millimeters
        PDFlib.SetMeasurementUnits(1);

        // Add the heading font
        PDFlib.AddStandardFont(5); // Helvetica bold
        PDFlib.SetTextSize(10);
        PDFlib.DrawText(50, 20, 'First Name:');
        PDFlib.DrawText(50, 40, 'Last Name:');
        PDFlib.DrawText(50, 60, 'Your Age:');

        // Add the font to use for the form fields
        FontID := PDFlib.AddStandardFont(0); // Courier

        FieldIndex := PDFlib.NewFormField('FirstName', 1);
        PDFlib.SetFormFieldBounds(FieldIndex, 50, 22, 50, 10);
        PDFlib.SetNeedAppearances(0);
        PDFlib.AddFormFont(FontID);
        PDFlib.SetFormFieldFont(FieldIndex, PDFlib.GetFormFontCount);
        PDFlib.SetFormFieldTextSize(FieldIndex, 12);
        PDFlib.SetFormFieldBorderColor(FieldIndex, 0.3, 0, 0);
        PDFlib.SetFormFieldBorderStyle(FieldIndex, 1, 0, 0, 0);
        PDFlib.SetFormFieldBackgroundColor(FieldIndex, 0.7, 0.6, 0.5);
        PDFlib.SetFormFieldValue(FieldIndex, edtFName.Text);

        FieldIndex := PDFlib.NewFormField('LastName', 1);
        PDFlib.SetFormFieldBounds(FieldIndex, 50, 42, 50, 10);
        PDFlib.SetFormFieldFont(FieldIndex, PDFlib.GetFormFontCount);
        PDFlib.SetFormFieldTextSize(FieldIndex, 12);
        PDFlib.SetFormFieldBorderColor(FieldIndex, 0.3, 0, 0);
        PDFlib.SetFormFieldBorderStyle(FieldIndex, 1, 0, 0, 0);
        PDFlib.SetFormFieldBackgroundColor(FieldIndex, 0.6, 0.7, 0.5);
        PDFlib.SetFormFieldValue(FieldIndex, edtLName.Text);

        FieldIndex := PDFlib.NewFormField('Age', 1);
        PDFlib.SetFormFieldBounds(FieldIndex, 50, 62, 50, 10);
        PDFlib.SetFormFieldFont(FieldIndex, PDFlib.GetFormFontCount);
        PDFlib.SetFormFieldTextSize(FieldIndex, 12);
        PDFlib.SetFormFieldBorderColor(FieldIndex, 0.3, 0, 0);
        PDFlib.SetFormFieldBorderStyle(FieldIndex, 1, 0, 0, 0);
        PDFlib.SetFormFieldBackgroundColor(FieldIndex, 0.6, 0.8, 0.5);
        PDFlib.SetFormFieldValue(FieldIndex, edtAge.Text);

        // Compress the contents of the file
        PDFlib.CompressContent;

        // Save the file
        PDFlib.SaveToFile(dlgSave.FileName);
      End
    Finally
      PDFlib.Free;
    End;
  End;
End;

Procedure TfrmMain.btnRotatedTextClick(Sender: TObject);
Var
  PDFlib: TPDFlib;
  X: Integer;
Begin
  dlgSave.InitialDir := ExtractFilePath(ParamStr(0));
  dlgSave.FileName := 'DemoRotText.pdf';
  If dlgSave.Execute Then
  Begin
    PDFlib := TPDFlib.Create;
    Try
      Begin
        // Set the paper size
        PDFlib.SetPageSize('A4');

        // Set the origin to the top-left corner
        PDFlib.SetOrigin(1);

        // Set the measurement units to millimeters
        PDFlib.SetMeasurementUnits(1);

        // Add a standard font
        PDFlib.AddStandardFont(11); // Times Roman Bold Italic
        PDFlib.SetTextSize(12);
        PDFlib.SetTextSize(10);

        For X := 0 To 35 Do
        Begin
          PDFlib.SetTextColor(X / 35, X / 70 + Random / 2, 1 - X / 35);
          PDFlib.DrawRotatedText(100, 80, X * 10, 'PDFlibrary');
        End;

        // Compress the contents of the file
        PDFlib.CompressContent;

        // Save the file
        PDFlib.SaveToFile(dlgSave.FileName);
      End
    Finally
      PDFlib.Free;
    End;
  End;
End;

Procedure TfrmMain.RenderPage;
Var
  BM: TBitmap;
  MS: TMemoryStream;
  DPI: Integer;
Begin
  BM := TBitmap.Create;
  Try
    imgPreview.Picture.Assign(BM);
    PreviewScrollBox.HorzScrollBar.Position := 0;
    PreviewScrollBox.VertScrollBar.Position := 0;
    imgPreview.Left := 0;
    imgPreview.Top := 0;
  Finally
    BM.Free;
  End;
  If Assigned(ViewPrint) Then
  Begin
    MS := TMemoryStream.Create;
    Try
      DPI := ((cmbZoom.ItemIndex + 1) * 25 * 96) div 100;
      ViewPrint.RenderPageToStream(DPI, ViewPrintPageNum, 0, MS);
      MS.Seek(0, soFromBeginning);
      BM := TBitmap.Create;
      Try
        BM.LoadFromStream(MS);
        imgPreview.AutoSize := True;
        imgPreview.Picture.Assign(BM);
      Finally
        BM.Free;
      End;
    Finally
      MS.Free;
    End;
  End;
End;

Procedure TfrmMain.btnViewPrintLoadPDFClick(Sender: TObject);
Var
  Password: String;
  R: Integer;
  CanProceed: Boolean;
  BlankBM: TBitmap;
Begin

  If Assigned(ViewPrint) Then
  Begin
    ViewPrint.Free;
    ViewPrint := Nil;
  End;
  CanProceed := False;

  dlgOpen.InitialDir := ExtractFilePath(ParamStr(0));
  If dlgOpen.Execute Then
  Begin
    ViewPrint := TPDFlib.Create;
    Begin
      If ViewPrint.LoadFromFile(dlgOpen.FileName, '') = 1 Then
      Begin
        If ViewPrint.EncryptionStatus = 1 Then
        Begin
          CanProceed := False;
          R := ViewPrint.CheckPassword('');
          If (R = 0) or (R = 1) or (R = 2) or (R = 5) Then
          Begin
            ViewPrint.Decrypt;
            CanProceed := True;
          End
          Else
          Begin
            Password := '';
            If InputQuery('PDF Security', 'Password:', Password) Then
            Begin
              R := ViewPrint.CheckPassword('');
              If (R = 1) or (R = 2) or (R = 5) Then
              Begin
                ViewPrint.Decrypt;
                CanProceed := True;
              End
              Else
                MessageDlg('The PDF could not be opened [e1].', mtError, [mbOK], 0);
            End
            Else
              CanProceed := False;
          End;
        End
        Else
          CanProceed := True;
      End
      Else
        MessageDlg('The PDF could not be opened [e2].', mtError, [mbOK], 0);
    End;
  End;
  If CanProceed Then
  Begin
    ViewPrintPageNum := 1;
    RenderPage;
  End
  Else
  Begin
    BlankBM := TBitmap.Create;
    Try
      imgPreview.Picture.Assign(BlankBM);
    Finally
      BlankBM.Free;
    End;
    ViewPrint.Free;
    ViewPrint := Nil;
  End;
End;

Procedure TfrmMain.btnPrintClick(Sender: TObject);
Begin
  If Assigned(ViewPrint) Then
  Begin
    dlgPrint.MinPage := 1;
    dlgPrint.MaxPage := ViewPrint.PageCount;
    dlgPrint.FromPage := 1;
    dlgPrint.ToPage := ViewPrint.PageCount;
    If dlgPrint.Execute Then
    Begin
      ViewPrint.PrintDocument(Printer.Printers[Printer.PrinterIndex],
        dlgPrint.FromPage, dlgPrint.ToPage, ViewPrint.PrintOptions(0, 1, 'PDFlib Demo'));
    End;
  End;
End;

Procedure TfrmMain.cmbZoomChange(Sender: TObject);
Begin
  RenderPage;
End;

Procedure TfrmMain.btnViewPrintPrevPageClick(Sender: TObject);
Begin
  If ViewPrintPageNum > 1 Then
  Begin
    Dec(ViewPrintPageNum);
    RenderPage;
  End;
End;

Procedure TfrmMain.btnViewPrintNextPageClick(Sender: TObject);
Begin
  If Assigned(ViewPrint) Then
  Begin
    If ViewPrintPageNum < ViewPrint.PageCount Then
    Begin
      Inc(ViewPrintPageNum);
      RenderPage;
    End;
  End;
End;

Procedure TfrmMain.FormShow(Sender: TObject);
Begin
  cmbZoom.ItemIndex := 3;
End;

Procedure TfrmMain.FormClose(Sender: TObject; Var Action: TCloseAction);
Var
  BlankBM: TBitmap;
Begin
  BlankBM := TBitmap.Create;
  Try
    imgPreview.Picture.Assign(BlankBM);
  Finally
    BlankBM.Free;
  End;
  ViewPrint.Free;
  ViewPrint := Nil;
End;

Procedure TfrmMain.Label10MouseEnter(Sender: TObject);
Begin
  Label10.Font.Color := clRed;
End;

Procedure TfrmMain.Label10MouseLeave(Sender: TObject);
Begin
  Label10.Font.Color := clBlue;
End;

Procedure TfrmMain.Label10Click(Sender: TObject);
Begin
  ShellExecute(Handle, 'open', 'https://www.loslab.com/purchase/pdf-library', Nil, Nil, SW_SHOW);
End;

End.
