Skip to main content
Table of Contents

Getting started with TMS FNC PDF Library

Prerequisites

  • TMS FNC Core installed and the runtime package added to the project.
  • The matching unit in your uses clause: FMX.TMSFNCPDFLib, VCL.TMSFNCPDFLib, or WEBLib.TMSFNCPDFLib.

Generate a simple PDF

BeginDocument starts the document, NewPage adds a page, and all drawing goes through Graphics. EndDocument finishes it — writing to the file you named, or returning the bytes as a TMemoryStream when you started the document without a file name.

Generate a simple PDFPascalC++
procedure TForm1.GenerateHelloWorld(const AFileName: string);
var
  p: TTMSFNCPDFLib;
begin
  p := TTMSFNCPDFLib.Create;
  try
    { Passing a file name writes the document straight to disk; pass an empty
      string to get the bytes back from EndDocument as a TMemoryStream. }
    p.BeginDocument(AFileName);
    try
      p.NewPage;
      p.Graphics.Font.Name := 'Segoe UI';
      p.Graphics.Font.Size := 14;
      p.Graphics.DrawText('Hello, TMS FNC PDF Library!', PointF(40, 40));
    finally
      p.EndDocument;
    end;
  finally
    p.Free;
  end;
end;
void __fastcall TForm1::GenerateHelloWorld(const String AFileName)
{
  TTMSFNCPDFLib *p = new TTMSFNCPDFLib();
  try
  {
    // Passing a file name writes the document straight to disk; pass an empty
    // string to get the bytes back from EndDocument as a TMemoryStream.
    p->BeginDocument(AFileName);
    try
    {
      p->NewPage();
      p->Graphics()->Font->Name = "Segoe UI";
      p->Graphics()->Font->Size = 14;
      p->Graphics()->DrawText("Hello, TMS FNC PDF Library!", PointF(40, 40));
    }
    __finally
    {
      p->EndDocument();
    }
  }
  __finally
  {
    delete p;
  }
}

Drop on a form

  1. Drop TTMSFNCPDFLib from the TMS FNC UI palette page onto a form or data module.
  2. Call BeginDocument / NewPage / Graphics.DrawText / EndDocument in a button handler.

Export graphics canvas to PDF

Export a control to PDFPascalC++
procedure TForm1.ExportButtonClick(Sender: TObject);
begin
  { The export object and its rectangle are passed to Save; TTMSFNCGraphicsPDFIO
    renders that object with the standard graphics drawing API. }
  GraphicsPDFIO1.Save('export.pdf', MyControl,
    RectF(0, 0, MyControl.Width, MyControl.Height));
end;
void __fastcall TForm1::ExportButtonClick(TObject *Sender)
{
  // The export object and its rectangle are passed to Save; TTMSFNCGraphicsPDFIO
  // renders that object with the standard graphics drawing API.
  GraphicsPDFIO1->Save("export.pdf", MyControl,
    RectF(0, 0, MyControl->Width, MyControl->Height));
}
Print a controlPascalC++
procedure TForm1.PrintButtonClick(Sender: TObject);
begin
  GraphicsPrintIO1.Print(MyControl,
    RectF(0, 0, MyControl.Width, MyControl.Height));
end;
void __fastcall TForm1::PrintButtonClick(TObject *Sender)
{
  GraphicsPrintIO1->Print(MyControl,
    RectF(0, 0, MyControl->Width, MyControl->Height));
}

Edit an existing PDF

OpenDocument parses a PDF that already exists so you can restructure, stamp, and read it back, and SaveDocument writes the result.

Open an existing documentPascalC++
function TForm1.CountPages(const AFileName: string): Integer;
var
  p: TTMSFNCPDFLib;
begin
  Result := 0;
  p := TTMSFNCPDFLib.Create;
  try
    p.OpenDocument(AFileName);
    try
      if p.IsDocumentOpened then
        Result := p.GetPageCount;
    finally
      p.CloseDocument;
    end;
  finally
    p.Free;
  end;
end;
int __fastcall TForm1::CountPages(const String AFileName)
{
  int Result = 0;
  TTMSFNCPDFLib *p = new TTMSFNCPDFLib();
  try
  {
    p->OpenDocument(AFileName);
    try
    {
      if (p->IsDocumentOpened())
        Result = p->GetPageCount();
    }
    __finally
    {
      p->CloseDocument();
    }
  }
  __finally
  {
    delete p;
  }
  return Result;
}

Next steps