PDF Library for Android

Delphi and C++ Builder library for creating, processing and rendering PDF documents.
  • uses PdfBox-Android library
  • available for Delphi/C++ Builder 10 - 10.3
  • royalty free distribution in applications

Library API

type
  EPdfLibraryError = class(Exception);

  TPdfLineCapStyle = (lcButt, lcRound, lsSquare);
  TPdfLineJoinStyle = (ljMiter, ljRound, ljBevel);
  TPdfRotation = (ro0 = 0, ro90 = 90, ro180 = 180, ro270 = 270);

  TPdfDocument = class
    constructor Create;
    constructor Create(const FileName: string; const Password: string = '');

    function AddPage: TPdfPage;
    function AddPage(const MediaBox: TRectF): TPdfPage;
    function ImportPage(Page: TPdfPage): TPdfPage;
    function LoadFont(const FileName: string): TPdfFont;
    function LoadFontAsset(const FileName: string; Subset: Boolean = True): TPdfFont;
    procedure RemovePage(Page: TPdfPage);
    procedure RemovePage(Index: Integer);
    function RenderPage(Index: Integer): TBitmap;
    procedure Save(const FileName: string);

    property Author: string read write;
    property CreationDate: TDateTime read write;
    property Creator: string read write;
    property CustomMetadata[const FieldName: string]: string read write;
    property Document: JPDDocument read;
    property Encrypted: Boolean read;
    property Id: Int64 read write;
    property Information: JPDDocumentInformation read;
    property Keywords: string read write;
    property ModificationDate: TDateTime read write;
    property PageCount: Integer read;
    property Pages[Index: Integer]: TPdfPage read;
    property Producer: string read write;
    property Subject: string read write;
    property Title: string read write;
    property Version: Single read write;
  end;

  TPdfPage = class
    function CreateContentStream(AppendContent: Boolean = False; Compress: Boolean = True; ResetContext: Boolean = False): TPdfPageContentStream;

    property ArtBox: TRectF read write;
    property BleedBox: TRectF read write;
    property CropBox: TRectF read write;
    property Document: TPdfDocument read;
    property MediaBox: TRectF read write;
    property Page: JPDPage read;
    property Rotation: TPdfRotation read write;
    property TrimBox: TRectF read write;
  end;

  TPdfPageContentStream = class
    procedure BeginText;
    procedure Clip;
    procedure ClipEvenOdd;
    procedure Close;
    procedure CloseAndStroke;
    procedure ClosePath;
    procedure CurveTo(X1, Y1, X2, Y2, X3, Y3: Single);
    procedure CurveTo(const Point1, Point2, Point3: TPointF);
    procedure CurveTo1(X1, Y1, X3, Y3: Single);
    procedure CurveTo1(const Point1, Point3: TPointF);
    procedure CurveTo2(X2, Y2, X3, Y3: Single);
    procedure CurveTo2(const Point2, Point3: TPointF);
    procedure EndText;
    procedure Fill;
    procedure FillEvenOdd;
    procedure Font(Font: TPdfFont; Size: Single);
    procedure Image(const FileName: string; X, Y: Single);
    procedure Image(const FileName: string; const Point: TPointF);
    procedure Image(const FileName: string; const Rect: TRectF);
    procedure MoveTo(X, Y: Single);
    procedure MoveTo(const Point: TPointF);
    procedure NewLine;
    procedure NewLineAtOffset(OffsetX, OffsetY: Single);
    procedure Leading(Leading: Double);
    procedure LineCapStyle(LineCapStyle: TPdfLineCapStyle);
    procedure LineJoinStyle(LineJoinStyle: TPdfLineJoinStyle);
    procedure LineTo(X, Y: Single);
    procedure LineTo(const Point: TPointF);
    procedure LineWidth(Width: Single);
    procedure NonStrokingColor(Color: TAlphaColor);
    procedure Rectangle(const Rect: TRectF);
    procedure RestoreGraphicsState;
    procedure SaveGraphicsState;
    procedure Stroke;
    procedure StrokingColor(Color: TAlphaColor);
    procedure Text(const Text: string);

    property Page: TPdfPage read FPage;
    property Stream: JPDPageContentStream read FStream;
  end;

  TPdfFont = class
    property AverageWidth: Single read;
    property Embedded: Boolean read;
    property Font: JPDFont read;
    property Name: string read;
    property SpaceWidth: Single read;
    property Standard14: Boolean read;
    property SubType: string read;
    property &Type: string read;
    property Vertical: Boolean read;

    function TextWidth(const Text: string): Single;
    function ToUnicode(Code: Integer): string;

    class property Courier: TPdfFont read;
    class property CourierBold: TPdfFont read;
    class property CourierBoldOblique: TPdfFont read;
    class property CourierOblique: TPdfFont read;
    class property Helvetica: TPdfFont read;
    class property HelveticaBold: TPdfFont read;
    class property HelveticaBoldOblique: TPdfFont read;
    class property HelveticaOblique: TPdfFont read;
    class property Symbol: TPdfFont read;
    class property TimesBold: TPdfFont read;
    class property TimesBoldItalic: TPdfFont read;
    class property TimesItalic: TPdfFont read;
    class property TimesRoman: TPdfFont read;
    class property ZapfDingbats: TPdfFont read;
  end;

  TPdfPageSize = class
    class property A0: TRectF read;
    class property A1: TRectF read;
    class property A2: TRectF read;
    class property A3: TRectF read;
    class property A4: TRectF read;
    class property A5: TRectF read;
    class property A6: TRectF read;
    class property Legal: TRectF read;
    class property Letter: TRectF read;
  end;								

Download and order

FAQ

Why "java.io.FileNotFoundException: open failed: EACCESS (Permission denied)" exception is raised when opening existing PDF file?
You have to request permissions to external storage before using the PDF file:
uses System.Permissions, Androidapi.JNI.Os, ...
...
  PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE)],
    procedure(const APermissions: TArray; const AGrantResults: TArray)
    var PermissionGranted: Boolean;
    begin
      PermissionGranted := (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted);
      ...
    end);
...
							
How can I use unicode fonts?
var
  Font: TPdfFont;
...
  Font := Document.LoadFontAsset('com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf');
...
  Content.BeginText;
  Content.Font(Font, 16);
  Content.NewLineAtOffset(20, PageHeight - 20);
  Content.Text('Unicode русский язык Tiếng Việt');
  Content.EndText;
...
							

Related links