Delphi API Interface (litePDF.pas)

    1 unit litePDF;
    2 
    3 {*
    4  * (c) 2013-2017 http://www.litePDF.cz
    5  *
    6  * This software is provided by the litePDF project ``as is'' and any
    7  * expressed or implied warranties, including, but not limited to, the
    8  * implied warranties of merchantability and fitness for a particular
    9  * purpose are disclaimed. In no event shall the litePDF project or
   10  * its contributors be liable for any direct, indirect, incidental,
   11  * special, exemplary, or consequential damages (including, but
   12  * not limited to, procurement of substitute goods or services;
   13  * loss of use, data, or profits; or business interruption)
   14  * however caused and on any theory of liability, whether in contract,
   15  * strict liability, or tort (including negligence or otherwise)
   16  * arising in any way out of the use of this software, even if advised
   17  * of the possibility of such damage.
   18  *
   19  * Permission is granted to anyone to use this software for any purpose,
   20  * including commercial applications, and to alter it and redistribute it
   21  * freely. The origin of this software must not be misrepresented; you must
   22  * not claim that you wrote the original software. If you use this software
   23  * in a product, an acknowledgment in the product documentation would be
   24  * appreciated but is not required.
   25  * }
   26 
   27 interface
   28 
   29 uses Winapi.Windows, System.Classes, System.DateUtils, System.SysUtils, Vcl.Graphics;
   30 
   31 var
   32    LitePDFAuthorizationKey : AnsiString = ''; {**< assign your litePDF authorization key to this variable }
   33 
   34 const
   35    LitePDF_API_Major : Integer = 1; {**< LitePDF API version Major part; @see LitePDF_API_Minor }
   36    LitePDF_API_Minor : Integer = 3; {**< LitePDF API version Minor part; @see LitePDF_API_Major }
   37 
   38    LitePDFDocumentInfo_Author : PAnsiChar           = 'Author';       {**< an Author of the document *}
   39    LitePDFDocumentInfo_Creator : PAnsiChar          = 'Creator';      {**< a Creator of the document }
   40    LitePDFDocumentInfo_Keywords : PAnsiChar         = 'Keywords';     {**< the Keywords of the document }
   41    LitePDFDocumentInfo_Subject : PAnsiChar          = 'Subject';      {**< a Subject of the document }
   42    LitePDFDocumentInfo_Title : PAnsiChar            = 'Title';        {**< a Title of the document }
   43    LitePDFDocumentInfo_Producer : PAnsiChar         = 'Producer';     {**< a Producer of the document; this key is read-only }
   44    LitePDFDocumentInfo_Trapped : PAnsiChar          = 'Trapped';      {**< a trapping state of the document }
   45    LitePDFDocumentInfo_CreationDate : PAnsiChar     = 'CreationDate'; {**< a date of the creation of the document }
   46    LitePDFDocumentInfo_ModificationDate : PAnsiChar = 'ModDate';      {**< a date of the last modification of the document }
   47 
   48 type
   49    TLitePDFErrorEvent = procedure(code : LongWord;
   50                                   const msg : PAnsiChar;
   51                                   user_data : Pointer); stdcall;
   52    TLitePDFAppendSignatureDataFunc = procedure (bytes : PByte;
   53                                                 bytes_len : LongWord;
   54                                                 user_data : Pointer); stdcall;
   55    TLitePDFFinishSignatureFunc = procedure (signature : PByte;
   56                                             signature_len : PLongWord;
   57                                             user_data : Pointer); stdcall;
   58 //---------------------------------------------------------------------------
   59 
   60    TLitePDFUnit = (
   61                   LitePDFUnit_Unknown     = 0, {**< Unknown unit; usually used to indicate an error *}
   62                   LitePDFUnit_mm          = 1, {**< Millimeters unit *}
   63                   LitePDFUnit_10th_mm     = 2, {**< 1/10th of a millimeter unit; 5 mm is value 50 *}
   64                   LitePDFUnit_100th_mm    = 3, {**< 1/100th of a millimeter unit; 5 mm is value 500 *}
   65                   LitePDFUnit_1000th_mm   = 4, {**< 1/1000th of a millimeter unit; 5 mm is value 5000 *}
   66                   LitePDFUnit_inch        = 5, {**< Inch unit *}
   67                   LitePDFUnit_10th_inch   = 6, {**< 1/10th of an inch unit; 5" is value 50 *}
   68                   LitePDFUnit_100th_inch  = 7, {**< 1/100th of an inch unit; 5" is value 500 *}
   69                   LitePDFUnit_1000th_inch = 8  {**< 1/1000th of an inch unit; 5" is value 5000 *}
   70                );
   71 
   72 //---------------------------------------------------------------------------
   73 
   74    TLitePDFDrawFlags = (
   75                   LitePDFDrawFlag_None                   = 0,  {**< None draw flags }
   76                   LitePDFDrawFlag_EmbedFontsNone         = 16, {**< Do not embed any fonts into resulting PDF.
   77                        @note: Fonts' subset is embeded by default, if none of the @ref LitePDFDrawFlag_EmbedFontsNone, @ref LitePDFDrawFlag_EmbedFontsComplete, @ref LitePDFDrawFlag_EmbedFontsSubset,
   78                        flags is defined; the @ref LitePDFDrawFlag_EmbedFontsNone is to override default font embedding. The reason for this default embedding is
   79                        due to PDF readers not showing correct fonts when they are not part of the PDF file.
   80                        @see LitePDFDrawFlag_EmbedFontsComplete, LitePDFDrawFlag_EmbedFontsSubset; }
   81                   LitePDFDrawFlag_EmbedFontsComplete     = 1, {**< Embed complete fonts into resulting PDF; @see LitePDFDrawFlag_EmbedFontsSubset, LitePDFDrawFlag_EmbedFontsNone }
   82                   LitePDFDrawFlag_EmbedFontsSubset       = 2, {**< Embed only subset of the fonts, aka used letters; this flag is used before @ref LitePDFDrawFlag_EmbedFontsComplete; @see LitePDFDrawFlag_EmbedFontsNone }
   83                   LitePDFDrawFlag_SubstituteFonts        = 4, {**< Substitute fonts with base PDF fonts, if possible }
   84                   LitePDFDrawFlag_CompressImagesWithJPEG = 8  {**< Compress images with JPEG compression, to get smaller PDF document; this is used only for RGB images }
   85                );
   86 //---------------------------------------------------------------------------
   87 
   88    TLitePDFEncryptPermission = (
   89                   LitePDFEncryptPermission_None        = $0,        {**< Nothing from the rest is allowed }
   90                   LitePDFEncryptPermission_Print       = $00000004, {**< Allow printing the document }
   91                   LitePDFEncryptPermission_Edit        = $00000008, {**< Allow modifying the document besides annotations, form fields or changing pages }
   92                   LitePDFEncryptPermission_Copy        = $00000010, {**< Allow text and graphic extraction }
   93                   LitePDFEncryptPermission_EditNotes   = $00000020, {**< Add or modify text annotations or form fields (if ePdfPermissions_Edit is set also allow to create interactive form fields including signature) }
   94                   LitePDFEncryptPermission_FillAndSign = $00000100, {**< Fill in existing form or signature fields }
   95                   LitePDFEncryptPermission_Accessible  = $00000200, {**< Extract text and graphics to support user with disabilities }
   96                   LitePDFEncryptPermission_DocAssembly = $00000400, {**< Assemble the document: insert, create, rotate delete pages or add bookmarks }
   97                   LitePDFEncryptPermission_HighPrint   = $00000800, {**< Print a high resolution version of the document }
   98                   LitePDFEncryptPermission_All         = $00000F3C  {**< Shortcut for all permissions }
   99                );
  100 //---------------------------------------------------------------------------
  101 
  102    TLitePDFEncryptAlgorithm = (
  103                   LitePDFEncryptAlgorithm_None  = 0, {**< No encryption algorithm; it can be used only when unsetting prepared encryption }
  104                   LitePDFEncryptAlgorithm_RC4V1 = 1, {**< RC4 Version 1 encryption using a 40bit key }
  105                   LitePDFEncryptAlgorithm_RC4V2 = 2, {**< RC4 Version 2 encryption using a 128bit key }
  106                   LitePDFEncryptAlgorithm_AESV2 = 4, {**< AES encryption with a 128 bit key (PDF1.6) }
  107                   LitePDFEncryptAlgorithm_AESV3 = 8  {**< AES encryption with a 256 bit key (PDF1.7 extension 3) }
  108                );
  109 //---------------------------------------------------------------------------
  110 
  111    TLitePDFAnnotationFlags = (
  112                   LitePDFAnnotationFlag_None           = $0000, {**< Default annotation flags *}
  113                   LitePDFAnnotationFlag_Invisible      = $0001, {**< Do not show nonstandard annotation if there is no annotation handler available *}
  114                   LitePDFAnnotationFlag_Hidden         = $0002, {**< Do not allow show, print or interact with the annotation *}
  115                   LitePDFAnnotationFlag_Print          = $0004, {**< Print the annotation *}
  116                   LitePDFAnnotationFlag_NoZoom         = $0008, {**< Do not scale the annotation's appearance to match the magnification of the page *}
  117                   LitePDFAnnotationFlag_NoRotate       = $0010, {**< Do not rotate the annotation's appearance to match the rotation of the page *}
  118                   LitePDFAnnotationFlag_NoView         = $0020, {**< Do not display the annotation on the screen or allow it to interact with the user *}
  119                   LitePDFAnnotationFlag_Readonly       = $0040, {**< Do not allow the annotation to interact with the user *}
  120                   LitePDFAnnotationFlag_Locked         = $0080, {**< Do not allow the annotation to be deleted or its properties (including position and size) to be modified by the user *}
  121                   LitePDFAnnotationFlag_ToggleNoView   = $0100, {**< Invert the interpretation of the NoView flag for certain events *}
  122                   LitePDFAnnotationFlag_LockedContents = $0200  {**< Do not allow the contents of the annotation to be modified by the user *}
  123                );
  124 //---------------------------------------------------------------------------
  125 
  126    TLitePDFAppearance = (
  127                   LitePDFAppearance_Normal             = $0000, {**< Normal appearance *}
  128                   LitePDFAppearance_Rollover           = $0001, {**< Rollover appearance; the default is the normal appearance *}
  129                   LitePDFAppearance_Down               = $0002  {**< Down appearance; the default is the normal appearance *}
  130    );
  131 
  132 //---------------------------------------------------------------------------
  133 
  134    TLitePDFBookmarkFlags = (
  135                   LitePDFBookmarkFlag_None           = $0000, {**< Default bookmark flags *}
  136                   LitePDFBookmarkFlag_Italic         = $0001, {**< Show bookmark title as an italic text *}
  137                   LitePDFBookmarkFlag_Bold           = $0002  {**< Show bookmark title as a bold text *}
  138    );
  139 
  140 //---------------------------------------------------------------------------
  141 
  142    TLitePDFException = class(Exception)
  143    private
  144       code : DWORD;
  145       msg : AnsiString;
  146    public
  147       constructor Create(pCode : DWORD;
  148                          const pMsg : AnsiString) overload;
  149       constructor Create(const src: TLitePDFException) overload;
  150 
  151       function getCode : DWORD;
  152       {**<
  153          @return Error code.
  154       }
  155 
  156       function getMessage : AnsiString;
  157       {**<
  158          @return Error message.
  159       }
  160    end;
  161 //---------------------------------------------------------------------------
  162 
  163    TLitePDF = class(TObject)
  164    private
  165       lib : THandle;
  166       context : Pointer;
  167       onError : TLitePDFErrorEvent;
  168       onErrorUserData : Pointer;
  169       lastErrorCode : DWORD;
  170       lastErrorMessage : AnsiString;
  171 
  172       function GetProc(const pProcIdent : PAnsiChar) : FARPROC;
  173       function checkAPIVersion(major : LongWord;
  174                                minor : LongWord) : Boolean;
  175       {*< returns whether DLL's version is the correct API version, as expected by this class }
  176 
  177       procedure ensureLibraryLoaded(const _func : PAnsiChar);
  178       procedure unloadLibrary;
  179       procedure freeLastError;
  180       procedure setLastError(code : DWORD;
  181                              const msg : PAnsiChar);
  182 
  183    public
  184       constructor Create;
  185       destructor Destroy; override;
  186 
  187       procedure setOnError(pOnError: TLitePDFErrorEvent;
  188                            pOnErrorUserData : Pointer);
  189       {**<
  190          Sets a custom callback for errors notified by the litePDF library. It's not
  191          necessary to be set. The errors are those returned during any function calls,
  192          but also during drawing, for example when some draw operation is not supported.
  193          Most of the object calls usually throw a @ref LitePDFException on errors.
  194 
  195          @param pOnError A callback to call.
  196          @param pOnErrorUserData user data for the callback.
  197 
  198          @see getLastErrorCode, getLastErrorMessage
  199       }
  200 
  201       function getLastErrorCode: LongWord;
  202       {**<
  203          Returns the last error code, which was notified by the litePDF library, if any.
  204          Most of the object calls usually throw a @ref LitePDFException on errors.
  205 
  206          @return The last error code, or 0, if there was none.
  207 
  208          @see setOnError, getLastErrorMessage
  209       }
  210 
  211       function getLastErrorMessage : AnsiString;
  212       {**<
  213          Returns the last error message, which was notified by the litePDF library, if any.
  214          Most of the object calls usually throw a @ref LitePDFException on errors.
  215 
  216          @return The last error message, or NULL, if there was none.
  217 
  218          @see setOnError, getLastErrorCode
  219       }
  220 
  221       procedure SetUnit(unitValue : TLitePDFUnit);
  222       {**<
  223          Sets a unit to use in functions which expect non-pixel size and position values.
  224          It can be one of TLitePDFUnit values. The default is @ref LitePDFUnit_mm.
  225 
  226          @param unitValue One of TLitePDFUnit values, to set as a unit.
  227 
  228          @see GetUnit, MMToUnit, UnitToMM, InchToUnit, UnitToInch
  229       }
  230 
  231       function GetUnit : TLitePDFUnit;
  232       {**<
  233          Gets the currently set unit, which is used in functions which expect
  234          non-pixel size and position values. It can be one of TLitePDFUnit values.
  235          The default is @ref LitePDFUnit_mm.
  236 
  237          @return One of TLitePDFUnit values, which is set as the current unit.
  238 
  239          @see SetUnit, MMToUnit, UnitToMM, InchToUnit, UnitToInch
  240       }
  241 
  242       function MMToUnitEx(useUnit : TLitePDFUnit;
  243                           mmValue : Double) : Double;
  244       {**<
  245          Converts a value from millimeters to @a useUnit. The caller does
  246          the rounding as needed.
  247 
  248          @param useUnit The @ref TLitePDFUnit unit to convert the value to.
  249          @param mmValue The value in millimeters to convert.
  250          @return The @a mmValue converted to @a useUnit unit.
  251 
  252          @see UnitToMMEx, InchToUnitEx, UnitToInchEx, MMToUnit
  253       *}
  254 
  255       function UnitToMMEx(useUnit : TLitePDFUnit;
  256                           unitValue : Double) : Double;
  257       {**<
  258          Converts a value from @a useUnit to millimeters. The caller does
  259          the rounding as needed.
  260 
  261          @param useUnit The @ref TLitePDFUnit unit to convert the value from.
  262          @param unitValue The value in @a useUnit to convert.
  263          @return The @a unitValue in @a useUnit converted to millimeters.
  264 
  265          @see MMToUnitEx, InchToUnitEx, UnitToInchEx, UnitToMM
  266       *}
  267 
  268       function InchToUnitEx(useUnit : TLitePDFUnit;
  269                             inchValue : Double) : Double;
  270       {**<
  271          Converts a value from inches to @a useUnit. The caller does
  272          the rounding as needed.
  273 
  274          @param useUnit The @ref TLitePDFUnit unit to convert the value to.
  275          @param inchValue The value in inches to convert.
  276          @return The @a inchValue converted to @a useUnit unit.
  277 
  278          @see UnitToInchEx, MMToUnitEx, UnitToMMEx, InchToUnit
  279       *}
  280 
  281       function UnitToInchEx(useUnit : TLitePDFUnit;
  282                             unitValue : Double) : Double;
  283       {**<
  284          Converts a value from @a useUnit to inches. The caller does
  285          the rounding as needed.
  286 
  287          @param useUnit The @ref TLitePDFUnit unit to convert the value from.
  288          @param unitValue The value in @a useUnit to convert.
  289          @return The @a unitValue in @a useUnit converted to inches.
  290 
  291          @see InchToUnitEx, MMToUnitEx, UnitToMMEx, UnitToInch
  292       *}
  293 
  294       function MMToUnit(mmValue : Double) : Double;
  295       {**<
  296          Converts a value from millimeters to current unit. The caller does
  297          the rounding as needed.
  298 
  299          @param mmValue A value in millimeters to convert to the current unit.
  300          @returns The @a mmValue converted to the current unit.
  301 
  302          @see GetUnit, UnitToMM, InchToUnit, UnitToInch, MMToUnitEx
  303       *}
  304 
  305       function UnitToMM(unitValue : Double) : Double;
  306       {**<
  307          Converts a value from the current unit to millimeters. The caller does
  308          the rounding as needed.
  309 
  310          @param unitValue A value in the current unit to convert to millimeters.
  311          @returns The @a unitValue converted to millimeters.
  312 
  313          @see GetUnit, MMToUnit, InchToUnit, UnitToInch, UnitToMMEx
  314       *}
  315 
  316       function InchToUnit(inchValue : Double) : Double;
  317       {**<
  318          Converts a value from inches to the current unit. The caller does
  319          the rounding as needed.
  320 
  321          @param inchValue A value in inches to convert to the current unit.
  322          @returns The @a inchValue converted to the current unit.
  323 
  324          @see GetUnit, UnitToInch, MMToUnit, UnitToMM, InchToUnitEx
  325       *}
  326 
  327       function UnitToInch(unitValue : Double) : Double;
  328       {**<
  329          Converts a value from the current unit to inches. The caller does
  330          the rounding as needed.
  331 
  332          @param unitValue A value in the current unit to convert to inches.
  333          @returns The @a unitValue converted to inches.
  334 
  335          @see GetUnit, InchToUnit, MMToUnit, UnitToMM, UnitToInchEx
  336       *}
  337 
  338       procedure PrepareEncryption(userPassword : AnsiString;
  339                                   ownerPassword : AnsiString;
  340                                   permissions : LongWord;
  341                                   algorithm : LongWord);
  342       {**<
  343          Prepares encryption for newly created documents. The LitePDF
  344          object should be empty. The encryption is used only with
  345          @ref CreateFileDocument and @ref CreateMemDocument, other
  346          functions ignore it. Use an empty @a ownerPassword to unset
  347          any previously set encryption properties.
  348          Loading an encrypted document lefts it encrypted on save too.
  349 
  350          @param userPassword User's password, can be an empty string,
  351             then the user doesn't need to write any password.
  352          @param ownerPassword Owner's password. Can be an emptry string, to unset encryption properties.
  353          @param permissions Bit-or of LitePDFEncryptPermission flags, to set user's
  354             permissions for the document.
  355          @param algorithm One of LitePDFEncryptAlgorithm constants, an algorithm
  356             to be used to encrypt the document.
  357 
  358          @see CreateFileDocument, CreateMemDocument
  359       }
  360 
  361       procedure CreateFileDocument(const fileName : AnsiString);
  362       {**<
  363          Makes the LitePDF object hold a new PDF, which writes directly to a file.
  364          The object should not have opened any other PDF data. Call @ref Close,
  365          to close the file, and possibly save changes to it. Most of the operations
  366          require memory-based PDF, which can be created with @ref CreateMemDocument.
  367 
  368          @param fileName File name to write the PDF result to.
  369 
  370          @note PoDoFo doesn't support creation of file-based documents with AES encryption,
  371             thus use for it memory-based documents instead (@ref CreateMemDocument).
  372 
  373          @see PrepareEncryption, CreateFileDocumentW, CreateMemDocument, LoadFromFile
  374       }
  375 
  376       procedure CreateFileDocumentW(const fileName : WideString);
  377       {**<
  378          This is the same as @ref CreateFileDocument, the only difference is that
  379          the @a fileName is a wide string.
  380       }
  381 
  382       procedure CreateMemDocument;
  383       {**<
  384          Makes the litePDF object hold a memory-based PDF. Such PDF can be
  385          saved with @ref SaveToFile or  @ref SaveToData.
  386          The PDF should be closed with @ref Close.
  387 
  388          @see PrepareEncryption, CreateFileDocument, LoadFromFile
  389       }
  390 
  391       procedure LoadFromFile(const fileName : AnsiString;
  392                              const password : AnsiString;
  393                              loadCompletely : Boolean;
  394                              forUpdate : Boolean = False);
  395       {**<
  396          Makes the LitePDF object hold a memory-based PDF, which is loaded
  397          from a disk file. This should be closed with @ref Close.
  398          The @a loadCompletely parameter is used to determine whether the file
  399          should be loaded into memory completely, or when the file can be read
  400          on demand. Using complete load requires more memory, but the disk
  401          file can be changed freely in the background, while incomplete load
  402          requires left the file without changes while being opened.
  403          The @a forUpdate parameter specifies whether the file is being opened
  404          for incremental update. In that case saving such document will result
  405          in the changes being appended to the end of the original document,
  406          instead of resaving whole document.
  407 
  408          @param fileName File name to load the PDF from.
  409          @param password Password to use for encrypted documents.
  410          @param loadCompletely Set to true when the file should be loaded completely
  411             into memory, or false to keep the disk file in use while working with it.
  412          @param forUpdate Set to True to open the file for incremental update,
  413             or set to False otherwise.
  414 
  415          @see LoadFromFileW
  416       }
  417 
  418       procedure LoadFromFileW(const fileName : WideString;
  419                               const password : AnsiString;
  420                               loadCompletely : Boolean;
  421                               forUpdate : Boolean = False);
  422       {**<
  423          This is the same as @ref LoadFromFile, the only difference is that
  424          the @a fileName is a wide string.
  425       *}
  426 
  427       procedure LoadFromData(data : PByte;
  428                              dataLength : LongWord;
  429                              const password : AnsiString;
  430                              forUpdate : Boolean = False);
  431       {**<
  432          Makes the LitePDF object hold a memory-based PDF, which is loaded
  433          with a PDF data. This should be closed with @ref Close.
  434          The @a forUpdate parameter specifies whether the file is being opened
  435          for incremental update. In that case saving such document will result
  436          in the changes being appended to the end of the original document,
  437          instead of resaving whole document.
  438 
  439          @param data PDF data to load.
  440          @param dataLength Length of PDF data.
  441          @param password Password to use for encrypted documents.
  442          @param forUpdate Set to True to open the file for incremental update,
  443             or set to False otherwise.
  444 
  445          @see CreateMemDocument, SaveToFile, SaveToData
  446       }
  447 
  448       procedure SaveToFile(const fileName : AnsiString);
  449       {**<
  450          Saves memory-based PDF into a file. The object should hold PDF created only
  451          with @ref CreateMemDocument, @ref LoadFromFile or @ref LoadFromData.
  452          Using any other object results in an error.
  453 
  454          In case the PDF document had been loaded with @ref LoadFromFile,
  455          @ref LoadFromFileW or @ref LoadFromData with its @a forUpdate
  456          parameter being true, the resulting document will contain the changes as
  457          an incremental update (appended at the end of the original document), otherwise
  458          the whole document is completely rewritten.
  459 
  460          @param fileName File name to which save the memory-based PDF.
  461 
  462          @note The only valid operation after this is either close the document
  463             with @ref Close, or free the @ref TLitePDF object.
  464 
  465          @see SaveToFileW, SaveToData, SaveToFileWithSign, Close
  466       }
  467 
  468       procedure SaveToFileW(const fileName : WideString);
  469       {**<
  470          This is the same as @ref SaveToFile, the only difference is that
  471          the @a fileName is a wide string.
  472       }
  473 
  474       function SaveToData(data : PByte;
  475                           var dataLength : LongWord) : Boolean;
  476       {**<
  477          Saves memory-based PDF into a data. The object should hold PDF created only
  478          with @ref CreateMemDocument, @ref LoadFromFile or @ref LoadFromData.
  479          Using any other object results in an error.
  480 
  481          In case the PDF document had been loaded with @ref LoadFromFile,
  482          @ref LoadFromFileW or @ref LoadFromData with its @a forUpdate
  483          parameter being true, the resulting document will contain the changes as
  484          an incremental update (appended at the end of the original document), otherwise
  485          the whole document is completely rewritten.
  486 
  487          @param data [out] Actual data to store the PDF content to. It can be NULL, in which case
  488             the @a dataLength is populated with large-enough value to hold the whole data.
  489          @param dataLength [in/out] Tells how many bytes can be stored in @a data. If @a data
  490             is NULL, then it is set to large-enough value. Passing non-NULL @a data with no enough
  491             large buffer results in a failure with no change on @a dataLength.
  492          @return Whether succeeded.
  493 
  494          @note The only valid operation after this is either call of @ref SaveToData again,
  495             to get information about necessary buffer size or data itself, close the document
  496             with @ref Close, or free the @ref TLitePDF object.
  497 
  498          @see SaveToFile, SaveToDataWithSign, Close
  499       }
  500 
  501       procedure Close;
  502       {**<
  503          Closes PDF data in a LitePDF object, thus the object doesn't hold anything afterward,
  504          aka it's like a newly created object.  The function does nothing, if the object doesn't
  505          hold any data. In case of any drawing in progress, the drawing is discarded, same as any
  506          unsaved changes to the memory-based PDF documents. It also unsets any encryption properties,
  507          previously set by @ref PrepareEncryption.
  508 
  509          @see AddPage, InsertPage, UpdatePage, FinishPage
  510       }
  511 
  512       function GetPageCount : LongWord;
  513       {**<
  514          Returns count of pages in an opened PDF document.
  515 
  516          @return Count of pages.
  517       }
  518 
  519       procedure GetPageSize(pageIndex : LongWord;
  520                             var width_u : LongWord;
  521                             var height_u : LongWord);
  522       {**<
  523          Gets size of an existing page, in the current unit.
  524 
  525          @param pageIndex Page index for which get the page size; counts from 0.
  526          @param width_u [out] Width of the page in the current unit.
  527          @param height_u [out] Height of the page in the current unit.
  528 
  529          @see GetUnit
  530       }
  531 
  532       function AddPage(width_u : LongWord;
  533                        height_u : LongWord;
  534                        width_px : LongWord;
  535                        height_px : LongWord;
  536                        drawFlags : LongWord) : HDC;
  537       {**<
  538          Begins drawing into a new page into the PDF document of the given size.
  539          Newly created page is added as the last page of the PDF document.
  540          This cannot be called when other drawing is in progress.
  541 
  542          @param width_u Width of the new page in the current unit.
  543          @param height_u Height of the new page in the current unit.
  544          @param width_px Width of the new page in pixels.
  545          @param height_px Height of the new page in pixels.
  546          @param drawFlags Flags for drawing functions. This is a bit-or of LitePDFDrawFlags values
  547             and influences only @ref AddPage, @ref InsertPage, @ref UpdatePage
  548             and @ref AddResource functions.
  549          @return Device context into which can be drawn with standard GDI functions.
  550             Finish the drawing, and the page addition, with @ref FinishPage.
  551 
  552          @note Larger pixel page size produces more precise font mapping.
  553 
  554          @see GetUnit, InsertPage, UpdatePage, FinishPage, AddResource
  555       }
  556 
  557       function InsertPage(pageIndex : LongWord;
  558                           width_u : LongWord;
  559                           height_u : LongWord;
  560                           width_px : LongWord;
  561                           height_px : LongWord;
  562                           drawFlags : LongWord) : HDC;
  563       {**<
  564          Begins drawing into a new page into the  PDF document of the given size.
  565          Newly created page is inserted at the given position of the PDF document.
  566          This cannot be called when other drawing is in progress.
  567          If the index is out of bounds, then the page is added ad the end, like with
  568          @ref AddPage.
  569 
  570          @param pageIndex Page index where to insert the page; counts from 0.
  571          @param width_u Width of the new page in the current unit.
  572          @param height_u Height of the new page in the current unit.
  573          @param width_px Width of the new page in pixels.
  574          @param height_px Height of the new page in pixels.
  575          @param drawFlags Flags for drawing functions. This is a bit-or of LitePDFDrawFlags values
  576             and influences only @ref AddPage, @ref InsertPage, @ref UpdatePage
  577             and @ref AddResource functions.
  578          @return Device context into which can be drawn with standard GDI functions.
  579             Finish the drawing, and the page insertion, with @ref FinishPage.
  580 
  581          @note Larger pixel page size produces more precise font mapping.
  582 
  583          @see GetUnit, GetPageCount, AddPage, UpdatePage, FinishPage, AddResource
  584       }
  585 
  586       function UpdatePage(pageIndex : LongWord;
  587                           width_px : LongWord;
  588                           height_px : LongWord;
  589                           drawFlags : LongWord) : HDC;
  590       {**<
  591          Begins drawing into an already existing page. The page size in the current unit can
  592          be found by @ref GetPageSize. The function fails, and returns NULL,
  593          if the @a pageIndex is out of bounds.
  594 
  595          @param pageIndex Page index which to update; counts from 0.
  596          @param width_px Width of the new page in pixels.
  597          @param height_px Height of the new page in pixels.
  598          @param drawFlags Flags for drawing functions. This is a bit-or of LitePDFDrawFlags values
  599             and influences only @ref AddPage, @ref InsertPage, @ref UpdatePage
  600             and @ref AddResource functions.
  601          @return Device context into which can be drawn with standard GDI functions.
  602             Finish the drawing, and the page update, with @ref FinishPage.
  603 
  604          @see GetUnit, GetPageCount, AddPage, InsertPage, FinishPage, AddResource
  605       }
  606 
  607       procedure FinishPage(dc : HDC);
  608       {**<
  609          Tells litePDF that drawing into the page is finished and that it can
  610          be processed into PDF. The @a dc is not valid after this call any more.
  611 
  612          @param hDC Device context previously returned by @ref AddPage,
  613             @ref InsertPage or @ref UpdatePage.
  614       }
  615 
  616       function AddResource(width_u : LongWord;
  617                            height_u : LongWord;
  618                            width_px : LongWord;
  619                            height_px : LongWord;
  620                            drawFlags : LongWord) : HDC;
  621       {**<
  622          Begins drawing into a new resource into the PDF document of the given size.
  623          This cannot be called when other drawing is in progress.
  624 
  625          @param width_u Width of the new page in the current unit.
  626          @param height_u Height of the new page in the current unit.
  627          @param width_px Width of the new page in pixels.
  628          @param height_px Height of the new page in pixels.
  629          @param drawFlags Flags for drawing functions. This is a bit-or of LitePDFDrawFlags values
  630             and influences only @ref AddPage, @ref InsertPage, @ref UpdatePage
  631             and @ref AddResource functions.
  632          @return Device context into which can be drawn with standard GDI functions.
  633             Finish the drawing, and the resource addition, with @ref FinishResource.
  634 
  635          @note Larger pixel resource size produces more precise font mapping.
  636 
  637          @see GetUnit, AddPage, InsertPage, UpdatePage, FinishResource, DrawResource
  638       }
  639 
  640       function FinishResource(dc : HDC) : LongWord;
  641       {**<
  642          Tells litePDF that drawing into the resource is finished and that it can
  643          be processed into PDF. The @a dc is not valid after this call any more.
  644 
  645          @param hDC Device context previously returned by @ref AddResource.
  646          @return Newly created resource ID, or 0 on error.
  647 
  648          @see AddResource, AddPageFromAsResource, DrawResource
  649       }
  650 
  651       procedure DeletePage(pageIndex : LongWord);
  652       {**<
  653          Deletes page at given index. It doesn't delete page resources, because these can
  654          be used by other pages.
  655 
  656          @param pageIndex Page index which to update; counts from 0.
  657          @return Whether succeeded.
  658 
  659          @see GetPageCount, PageToResource
  660       }
  661 
  662       procedure AddPagesFrom(from : TLitePDF;
  663                              pageIndex : LongWord;
  664                              pageCount : LongWord);
  665       {**<
  666          Adds existing pages as the last pages from another PDF. Both objects should
  667          hold memory-based documents.
  668 
  669          @param from a LitePDF object from which add the pages.
  670          @param pageIndex Page index which to add from @a from; counts from 0.
  671          @param pageCount How many pages to add; 0 means whole document.
  672 
  673          @note The two objects cannot be the same.
  674 
  675          @see GetPageCount, InsertPageFrom, PageToResource
  676       }
  677 
  678       procedure InsertPageFrom(pageIndexTo : LongWord;
  679                                from : TLitePDF;
  680                                pageIndexFrom : LongWord);
  681       {**<
  682          Inserts an existing page at the given index from another PDF. Both objects should
  683          hold memory-based documents.
  684 
  685          @param pageIndexTo Page index where to add the page; counts from 0. Adds page
  686             at the end, if out of bounds.
  687          @param from a LitePDF object, from which add the page.
  688          @param pageIndexFrom Page index which to add from @a from; counts from 0.
  689 
  690          @note The two objects cannot be the same.
  691 
  692          @see GetPageCount, AddPagesFrom, PageToResource
  693       }
  694 
  695       function AddPageFromAsResource(from : TLitePDF;
  696                                      pageIndex : LongWord;
  697                                      useTrimBox : Boolean = False) : LongWord;
  698       {**<
  699          Adds an existing page as a resource of a given PDF. This resource can be
  700          referenced multiple times by its identifier. Both objects should
  701          hold memory-based documents.
  702 
  703          @param from a LitePDF object, from which add the page.
  704          @param pageIndex Page index which to add from @a from; counts from 0.
  705          @param useTrimBox If true, try to use trimbox for size of the resource (XObject)
  706          @return Resource identifier, or 0 on error.
  707 
  708          @note The two objects cannot be the same.
  709 
  710          @see GetPageCount, AddPagesFrom, PageToResource, GetResourceSize, DrawResource
  711       }
  712 
  713       function PageToResource(pageIndex : LongWord) : LongWord;
  714       {**<
  715          Creates a resource, which will reference an existing page.
  716          The page itself is not deleted after call of this.
  717 
  718          @param pageIndex Page index for which create the resource reference; counts from 0.
  719          @return Resource identifier, or 0 on error.
  720 
  721          @see GetPageCount, AddPagesFrom, AddPageFromAsResource, GetResourceSize,
  722             DrawResource
  723       }
  724 
  725       procedure GetResourceSize(resourceID : LongWord;
  726                                 var width_u : LongWord;
  727                                 var height_u : LongWord);
  728       {**<
  729          Gets size of an existing resource, in the current unit. The resource ID
  730          was returned from @ref AddPageFromAsResource or @ref FinishResource.
  731 
  732          @param resourceID Resource ID for which get the size.
  733          @param width_u [out] Width of the resource, in the current unit.
  734          @param height_u [out] Height of the resource, in the current unit.
  735 
  736          @see GetUnit, AddPageFromAsResource, DrawResource
  737       }
  738 
  739       procedure DrawResource(resourceID : LongWord;
  740                              pageIndex : LongWord;
  741                              unitValue : TLitePDFUnit;
  742                              x : Integer;
  743                              y : Integer;
  744                              scaleX : Integer;
  745                              scaleY : Integer);
  746       {**<
  747          Draws an existing resource at given position. The resource ID
  748          was returned from @ref AddPageFromAsResource, @ref PageToResource or @ref FinishResource.
  749          The @a unitValue is used for both the position and the scale. In case
  750          of the scale, it defines only the ratio to the base unit.
  751          For example, if the @a unitValue is either @ref LitePDFUnit_1000th_mm or
  752          @ref LitePDFUnit_1000th_inch, then the ratio for the @a scaleX and @a scaleY
  753          is used 1/1000 (where 1000 means the same size as the resource is in this case).
  754 
  755          @param resourceID Resource ID to draw.
  756          @param pageIndex Page index to which draw; counts from 0.
  757          @param unitValue A unit to use for the @a x and @a y, and a ratio for the @a scaleX and @a scaleY.
  758          @param x Where to draw on the page, X axes, in the given @a unitValue unit,
  759             with left-top corner being [0,0].
  760          @param y Where to draw on the page, Y axes, in the given @a unitValue unit,
  761             with left-top corner being [0,0].
  762          @param scaleX Scale factor of the page, X axes, using the @a unitValue ratio.
  763          @param scaleY Scale factor of the page, Y axes, using the @a unitValue ratio.
  764 
  765          @see GetPageCount, AddPageFromAsResource, PageToResource, FinishResource,
  766             GetResourceSize, DrawResourceWithMatrix
  767       }
  768 
  769       procedure DrawResourceWithMatrix(resourceID : LongWord;
  770                                        pageIndex : LongWord;
  771                                        a : Double;
  772                                        b : Double;
  773                                        c : Double;
  774                                        d : Double;
  775                                        e : Double;
  776                                        f : Double);
  777       {**<
  778          Draws an existing resource with given transformation matrix. All
  779          the transformation values are passed into PDF directly, without any
  780          conversion. The resource ID was returned from @ref AddPageFromAsResource
  781          or @ref FinishResource. The constructed transformation matrix
  782          is a standard 3x3 matrix:<BR>
  783          <CODE>   | a b 0 |</CODE><BR>
  784          <CODE>   | c d 0 |</CODE><BR>
  785          <CODE>   | e f 1 |</CODE>
  786 
  787          @param resourceID Resource ID to draw.
  788          @param pageIndex Page index to which draw; counts from 0.
  789          @param a Transformation matrix [ a b c d e f ] parameter 'a', in PDF units.
  790          @param b Transformation matrix [ a b c d e f ] parameter 'b', in PDF units.
  791          @param c Transformation matrix [ a b c d e f ] parameter 'c', in PDF units.
  792          @param d Transformation matrix [ a b c d e f ] parameter 'd', in PDF units.
  793          @param e Transformation matrix [ a b c d e f ] parameter 'e', in PDF units.
  794          @param f Transformation matrix [ a b c d e f ] parameter 'f', in PDF units.
  795 
  796          @note Each of a, b, c, d, e, f is rounded down to nearest 1/1000th of PDF units.
  797 
  798          @see GetPageCount, AddPageFromAsResource, FinishResource,
  799             GetResourceSize, DrawResource
  800       }
  801 
  802       procedure SetDocumentInfo(const name : AnsiString;
  803                                 const value : WideString);
  804       {**<
  805          Sets information about the document. The name can be one
  806          of the LitePDFDocumentInfo predefined constants.
  807 
  808          @param name Document info property name to set.
  809          @param value Null-terminated Unicode value to set.
  810       }
  811 
  812       function GetDocumentInfoExists(const name : AnsiString) : Boolean;
  813       {**<
  814          Checks whether information about the document of the given name exists.
  815          The name can be one of the LitePDFDocumentInfo predefined constants.
  816 
  817          @param name Document info property name to test.
  818          @return Whether succeeded and the document information is set.
  819       }
  820 
  821       function GetDocumentInfo(const name : AnsiString) : WideString;
  822       {**<
  823          Gets information about the document. The name can be one
  824          of the LitePDFDocumentInfo predefined constants.
  825 
  826          @param name Document info property name to get.
  827          @return Unicode value.
  828       }
  829 
  830       function GetDocumentIsSigned : Boolean;
  831       {**<
  832          Checks whether currently opened document is already signed. Signing already
  833          signed document can cause breakage of previous signatures, thus it's good
  834          to test whether the loaded document is signed, before signing it.
  835 
  836          @return Whether the opened document is already signed.
  837 
  838          @see GetSignatureCount, SaveToFileWithSign, SaveToDataWithSign
  839       }
  840 
  841       function GetSignatureCount : LongWord;
  842       {**<
  843          Provides how many signature fields the currently opened document contains.
  844          It returns the count of the all fields, not only those already signed.
  845 
  846          @return How many signatures the currently opened document contains.
  847 
  848          @note The litePDF caches the list of the existing signature fields for performance
  849             reasons and it rebuilds it whenever this function is called or when the
  850             @ref CreateSignature is called, thus if there are made any changes
  851             directly with the PoDoFo API after the cache had been created, then make sure
  852             you call this function again to avoid a use-after-free or an outdated information
  853             being used. The litePDF will try to keep the cache up to date as needed, but
  854             it cannot cover every case, especially not the one when the PoDoFo API is used.
  855 
  856          @see GetDocumentIsSigned, GetSignatureHasData, GetSignatureData
  857       }
  858 
  859       function GetSignatureName(index : LongWord) : AnsiString;
  860       {**<
  861          Gets the signature field name at the given @a index.
  862 
  863          @param index Which signature field name to get; counts from 0. This might be less
  864             than @ref GetSignatureCount.
  865          @return An ASCII name of the field.
  866       }
  867 
  868       function CreateSignature(name : AnsiString;
  869                                annotationPageIndex : LongWord;
  870                                annotationPosition_u : TRect;
  871                                annotationFlags : LongWord) : LongWord;
  872       {**<
  873          Creates a new signature field named @a name. The field is created completely empty.
  874          Use @ref SetSignatureDate, @ref SetSignatureReason,
  875          @ref SetSignatureLocation, @ref SetSignatureCreator,
  876          @ref SetSignatureAppearance and such to populated it with required values.
  877          Finally, to sign the signature field use @ref SaveToFileWithSign family
  878          functions.
  879 
  880          @param name Signature field name to use. This should be unique.
  881          @param annotationPageIndex Page index where to place the signature annotation.
  882          @param annotationPosition_u Where to place the annotation on the page, in the current unit.
  883          @param annotationFlags Bit-or of @ref TLitePDFAnnotationFlags flags.
  884          @return The index of the added signature field.
  885 
  886          @see GetSignatureCount, GetSignatureName
  887        }
  888 
  889       function GetSignatureHasData(index : LongWord) : Boolean;
  890       {**<
  891          Checks whether the given signature field contains any data, which
  892          means whether the signature field is signed.
  893 
  894          @param index Which signature data to get; counts from 0. This might be less
  895             than @ref GetSignatureCount.
  896          @return Whether the given signature contains any data.
  897 
  898          @see GetSignatureData
  899       }
  900 
  901       function GetSignatureData(index : LongWord;
  902                                 data : PByte;
  903                                 var dataLength : LongWord) : Boolean;
  904       {**<
  905          Gathers raw signature data for the given signature in the currently opened document.
  906          Use @ref GetSignatureHasData to check whether the given signature field
  907          is signed or not.
  908 
  909          @param index Which signature data to get; counts from 0. This might be less
  910             than @ref GetSignatureCount.
  911          @param data [out] Actual data to store the signature content to. It can be NULL, in which case
  912             the @a dataLength is populated with large-enough value to hold the whole data.
  913          @param dataLength [in/out] Tells how many bytes can be stored in @a data. If @a data
  914             is NULL, then it is set to large-enough value. Passing non-NULL @a data with no enough
  915             large buffer results in a failure with no change on @a dataLength.
  916          @return Whether succeeded.
  917 
  918          @see GetDocumentIsSigned, GetSignatureCount, GetSignatureRanges
  919       }
  920 
  921       function GetSignatureRanges(index : LongWord;
  922                                   pRangesArray : PUInt64;
  923                                   var pRangesArrayLength : LongWord) : Boolean;
  924       {**<
  925          Gathers signature ranges, that is the actual offsets into the opened file
  926          which had been used to create the signature data (@ref GetSignatureData).
  927          The array is a pair of numbers, where the first number is an offset into the file
  928          from its beginning and the second number is the number of bytes being used for
  929          the signature from this offset.
  930 
  931          @param index Which signature ranges to get; counts from 0. This might be less
  932             than @ref GetSignatureCount.
  933          @param pRangesArray [out] Actual array to store the signature ranges to. It can be NULL,
  934             in which case the @a pRangesArrayLength is populated with large-enough value to hold
  935             the whole array.
  936          @param pRangesArrayLength [in/out] Tells how many items can be stored in @a pRangesArray.
  937             If @a pRangesArray is NULL, then it is set to large-enough value. Passing non-NULL
  938             @a pRangesArray with no enough large array results in a failure with no change
  939             on @a pRangesArrayLength.
  940          @return Whether succeeded.
  941 
  942          @note This function works only for the signatures which use this kind of signature method.
  943 
  944          @see GetDocumentIsSigned, GetSignatureCount, GetSignatureData
  945       }
  946 
  947       procedure SetSignatureDate(index : LongWord;
  948                                  const dateOfSign : TDateTime);
  949       {**<
  950          Sets signature field date of sign.
  951 
  952          @param index Which signature to use; counts from 0. This might be less
  953             than @ref GetSignatureCount.
  954          @param dateOfSign Date of sign, when the signature was created; less than
  955             or equal to 1.1.1970 means today. The value is converted into Unix time_t,
  956             which can be clamp on 32-bit systems.
  957 
  958          @see GetSignatureDate, GetSignatureCount
  959       }
  960 
  961       function GetSignatureDate(index : LongWord) : TDateTime;
  962       {**<
  963          Gets signature field date of sign.
  964 
  965          @param index Which signature to use; counts from 0. This might be less
  966             than @ref GetSignatureCount.
  967          @return The date of sign. It's like Unix time_t, as set by the signature field creator.
  968             The value can be clamp on 32-bit systems.
  969 
  970          @see SetSignatureDate, GetSignatureCount
  971       }
  972 
  973       procedure SetSignatureReason(index : LongWord;
  974                                    const reason : WideString);
  975       {**<
  976          Sets signature reason.
  977 
  978          @param index Which signature to use; counts from 0. This might be less
  979             than @ref GetSignatureCount.
  980          @param reason The value to set.
  981 
  982          @see GetSignatureReason, GetSignatureCount
  983       *}
  984 
  985       function GetSignatureReason(index : LongWord) : WideString;
  986       {**<
  987          Gets signature reason.
  988 
  989          @param index Which signature to use; counts from 0. This might be less
  990             than @ref GetSignatureCount.
  991          @return A Unicode string containing the value.
  992 
  993          @see SetSignatureReason, GetSignatureCount
  994       }
  995 
  996       procedure SetSignatureLocation(index : LongWord;
  997                                      const location : WideString);
  998       {**<
  999          Sets signature location, aka where the signature had been made. This can be left unset.
 1000 
 1001          @param index Which signature to use; counts from 0. This might be less
 1002             than @ref GetSignatureCount.
 1003          @param location The value to set.
 1004 
 1005          @see GetSignatureLocation, GetSignatureCount
 1006       }
 1007 
 1008       function GetSignatureLocation(index : LongWord) : WideString;
 1009       {**<
 1010          Gets signature location.
 1011 
 1012          @param index Which signature to use; counts from 0. This might be less
 1013             than @ref GetSignatureCount.
 1014          @return A Unicode string containing the value.
 1015 
 1016          @see SetSignatureLocation, GetSignatureCount
 1017       }
 1018 
 1019       procedure SetSignatureCreator(index : LongWord;
 1020                                     const creator : AnsiString);
 1021       {**<
 1022          Sets signature creator. This can be left unset.
 1023 
 1024          @param index Which signature to use; counts from 0. This might be less
 1025             than @ref GetSignatureCount.
 1026          @param creator The value to set.
 1027 
 1028          @see GetSignatureCreator, GetSignatureCount
 1029       }
 1030 
 1031       function GetSignatureCreator(index : LongWord) : AnsiString;
 1032       {**<
 1033          Gets signature creator.
 1034 
 1035          @param index Which signature to use; counts from 0. This might be less
 1036             than @ref GetSignatureCount.
 1037          @return An ASCII string containing the value.
 1038 
 1039          @see SetSignatureCreator, GetSignatureCount
 1040       }
 1041 
 1042       procedure SetSignatureAppearance(index : LongWord;
 1043                                        appearanceType : TLitePDFAppearance;
 1044                                        resourceID : LongWord;
 1045                                        offsetX_u : Integer;
 1046                                        offsetY_u : Integer);
 1047       {**<
 1048          Sets the signature appearance.
 1049 
 1050          @param index Which signature to use; counts from 0. This might be less
 1051             than @ref GetSignatureCount.
 1052          @param appearanceType One of the @ref LitePDFAppearance_Normal, @ref LitePDFAppearance_Rollover
 1053             and @ref LitePDFAppearance_Down contacts. At least the @ref LitePDFAppearance_Normal type
 1054             should be set, if the appearance of the signature is requested.
 1055          @param resourceID An existing resource ID of the annotation content, as shown to the user.
 1056          @param offsetX_u X-offset of the resource inside the annotation of the signature, in the current unit.
 1057          @param offsetY_u Y-offset of the resource inside the annotation of the signature, in the current unit.
 1058 
 1059          @note The resoruce position offset is from [left, top] corner of the annotation rectangle.
 1060 
 1061          @see GetUnit, AddResource, GetSignatureCount, CreateSignature
 1062       }
 1063 
 1064       procedure SetSignatureSize(requestBytes : LongWord);
 1065       {**<
 1066          Sets how many bytes the signature may require. The default value is 2048 bytes
 1067          and it is automatically adjusted when the @ref SaveToFileWithSign or
 1068          @ref SaveToDataWithSign are used. The manual signing functions
 1069          require this value to be set before signing, if the final hash with the certificate
 1070          exceeds the default size.
 1071 
 1072          This value is remembered in general, not for any signature in particular.
 1073 
 1074          @param requestBytes How many bytes the signature will require.
 1075 
 1076          @see SaveToFileWithSignManual, SaveToFileWithSignManualW, SaveToDataWithSignManual
 1077       }
 1078 
 1079       procedure AddSignerPFX(pfxData : PByte;
 1080                              pfxDataLength : LongWord;
 1081                              const pfxPassword : AnsiString);
 1082       {**<
 1083          Adds a signer to be used when digitally signing the document with
 1084          @ref SaveToFileWithSign or @ref SaveToDataWithSign.
 1085          The passed-in certificate is in the PFX format and should include
 1086          the private key.
 1087 
 1088          @param pfxData A certificate with private key in the PFX format.
 1089          @param pfxDataLength A length of the @a pfxData.
 1090          @param pfxPassword A password to use to open the PFX certificate; can be NULL.
 1091 
 1092          @see AddSignerPEM
 1093       }
 1094 
 1095       procedure AddSignerPEM(pemData : PByte;
 1096                              pemDataLength : LongWord;
 1097                              pkeyData : PByte;
 1098                              pkeyDataLength : LongWord;
 1099                              const pkeyPassword : AnsiString);
 1100       {**<
 1101          Adds a signer to be used when digitally signing the document with
 1102          @ref SaveToFileWithSign or @ref SaveToDataWithSign.
 1103          The passed-in certificate and private key are in the PEM format.
 1104 
 1105          @param pemData A certificate in the PEM format.
 1106          @param pemDataLength A length of the @a pemData.
 1107          @param pkeyData A private key for the certificate, in the PEM format.
 1108          @param pkeyDataLength A length of the @a pkeyData.
 1109          @param pkeyPassword A password to use to open the private key; can be NULL.
 1110 
 1111          @see AddSignerPFX
 1112       *}
 1113 
 1114       procedure SaveToFileWithSign(const fileName : AnsiString;
 1115                                    signatureIndex : LongWord);
 1116       {**<
 1117          Digitally signs a PDF document opened at the LitePDF object. The caller is
 1118          responsible to set at least one signer with either @ref AddSignerPFX
 1119          or @ref AddSignerPEM first. An alternative @ref SaveToFileWithSignManual
 1120          is provided when it's required to compute the signature hash manually by the caller.
 1121 
 1122          In case the document had been loaded with @ref LoadFromFile,
 1123          @ref LoadFromFileW or @ref LoadFromData with its @a forUpdate
 1124          parameter being true, the resulting document will contain the changes as
 1125          an incremental update (appended at the end of the original document), otherwise
 1126          the whole document is completely rewritten.
 1127 
 1128          @param fileName A file name where to save signed PDF document.
 1129          @param signatureIndex Which signature to use; counts from 0. This might be less
 1130             than @ref GetSignatureCount.
 1131 
 1132          @note The only valid operation after this is either close the document
 1133             with @ref Close, or free the @ref TLitePDF object.
 1134 
 1135          @note Signing already signed document can cause breakage of previous signatures, thus
 1136             check whether the loaded document is already signed with @ref GetDocumentIsSigned.
 1137             Load the document with its @a forUpdate parameter set to true, to sign an existing document.
 1138 
 1139          @see SaveToFileWithSignW, SaveToDataWithSign
 1140       }
 1141 
 1142       procedure SaveToFileWithSignW(const fileName : WideString;
 1143                                     signatureIndex : LongWord);
 1144       {**<
 1145          This is the same as @ref SaveToFileWithSign, the only difference is that
 1146          the @a fileName is a wide string.
 1147       }
 1148 
 1149       function SaveToDataWithSign(signatureIndex : LongWord;
 1150                                   data : PByte;
 1151                                   var dataLength : LongWord) : Boolean;
 1152       {**<
 1153          Digitally signs a PDF document opened at the LitePDF object. The caller is
 1154          responsible to set at least one signer with either @ref AddSignerPFX
 1155          or @ref AddSignerPEM first. An alternative @ref SaveToDataWithSignManual
 1156          is provided when it's required to compute the signature hash manually by the caller.
 1157 
 1158          In case the document had been loaded with @ref LoadFromFile,
 1159          @ref LoadFromFileW or @ref LoadFromData with its @a forUpdate
 1160          parameter being true, the resulting document will contain the changes as
 1161          an incremental update (appended at the end of the original document), otherwise
 1162          the whole document is completely rewritten.
 1163 
 1164          @param signatureIndex Which signature to use; counts from 0. This might be less
 1165             than @ref GetSignatureCount.
 1166          @param data [out] Actual data to store the PDF content to. It can be NULL, in which case
 1167             the @a dataLength is populated with large-enough value to hold the whole data.
 1168          @param dataLength [in/out] Tells how many bytes can be stored in @a data. If @a data
 1169             is NULL, then it is set to large-enough value. Passing non-NULL @a data with no enough
 1170             large buffer results in a failure with no change on @a dataLength.
 1171          @return Whether succeeded.
 1172 
 1173          @note The only valid operation after this is either call of @ref SaveToDataWithSign again,
 1174             to get information about necessary buffer size or data itself, close the document
 1175             with @ref Close, or free the @ref TLitePDF object.
 1176 
 1177          @note Signing already signed document can cause breakage of previous signatures, thus
 1178             check whether the loaded document is already signed with @ref GetDocumentIsSigned.
 1179             Load the document with its @a forUpdate parameter set to true, to sign an existing document.
 1180 
 1181          @see SaveToFileWithSign
 1182       }
 1183 
 1184       procedure SaveToFileWithSignManual(const fileName : AnsiString;
 1185                                          signatureIndex : LongWord;
 1186                                          appendSignatureData : TLitePDFAppendSignatureDataFunc;
 1187                                          append_user_data : Pointer;
 1188                                          finishSignature : TLitePDFFinishSignatureFunc;
 1189                                          finish_user_data : Pointer);
 1190       {**<
 1191          Digitally signs a PDF document opened at the LitePDF object. The caller is
 1192          responsible for a detached hash computations and related certificate management.
 1193 
 1194          In case the document had been loaded with @ref LoadFromFile,
 1195          @ref LoadFromFileW or @ref LoadFromData with its @a forUpdate
 1196          parameter being true, the resulting document will contain the changes as
 1197          an incremental update (appended at the end of the original document), otherwise
 1198          the whole document is completely rewritten.
 1199 
 1200          @param fileName A file name where to save signed PDF document.
 1201          @param signatureIndex Which signature to use; counts from 0. This might be less
 1202             than @ref GetSignatureCount.
 1203          @param appendSignatureData Called when more data should be added to hash computation.
 1204             The function cannot be NULL, even when called the second time, to get actual data.
 1205          @param append_user_data User data value for the @a appendSignatureData callback.
 1206          @param finishSignature Called when all the data are processed, and the signature
 1207             value is required. The @a signature_len contains size of the @a signature buffer.
 1208             The callback is responsible to populate @a signature and @a signature_len with
 1209             correct values. Set @a signature_len to zero on any error.
 1210             Note the callback is called only once.
 1211             The function cannot be NULL, even when called the second time, to get actual data.
 1212          @param finish_user_data User data value for the @a finishSignature callback.
 1213 
 1214          @note The only valid operation after this is either close the document
 1215             with @ref Close, or free the @ref TLitePDF object.
 1216 
 1217          @note Signing already signed document can cause breakage of previous signatures, thus
 1218             check whether the loaded document is already signed with @ref GetDocumentIsSigned.
 1219             Load the document with its @a forUpdate parameter set to true, to sign an existing document.
 1220 
 1221          @see SaveToFileWithSign, SaveToFileWithSignManualW, SaveToDataWithSignManual
 1222       }
 1223 
 1224       procedure SaveToFileWithSignManualW(const fileName : WideString;
 1225                                           signatureIndex : LongWord;
 1226                                           appendSignatureData : TLitePDFAppendSignatureDataFunc;
 1227                                           append_user_data : Pointer;
 1228                                           finishSignature : TLitePDFFinishSignatureFunc;
 1229                                           finish_user_data : Pointer);
 1230       {**<
 1231          This is the same as @ref SaveToFileWithSignManual, the only difference is that
 1232          the @a fileName is a wide string.
 1233       }
 1234 
 1235       function SaveToDataWithSignManual(signatureIndex : LongWord;
 1236                                         appendSignatureData : TLitePDFAppendSignatureDataFunc;
 1237                                         append_user_data : Pointer;
 1238                                         finishSignature : TLitePDFFinishSignatureFunc;
 1239                                         finish_user_data : Pointer;
 1240                                         data : PByte;
 1241                                         var dataLength : LongWord) : Boolean;
 1242       {**<
 1243          Digitally signs a PDF document opened at the LitePDF object. The caller is
 1244          responsible for a detached hash computations and related certificate management.
 1245 
 1246          In case the document had been loaded with @ref LoadFromFile,
 1247          @ref LoadFromFileW or @ref LoadFromData with its @a forUpdate
 1248          parameter being true, the resulting document will contain the changes as
 1249          an incremental update (appended at the end of the original document), otherwise
 1250          the whole document is completely rewritten.
 1251 
 1252          @param signatureIndex Which signature to use; counts from 0. This might be less
 1253             than @ref GetSignatureCount.
 1254          @param appendSignatureData Called when more data should be added to hash computation.
 1255             The function cannot be NULL, even when called the second time, to get actual data.
 1256          @param append_user_data User data value for the @a appendSignatureData callback.
 1257          @param finishSignature Called when all the data are processed, and the signature
 1258             value is required. The @a signature_len contains size of the @a signature buffer.
 1259             The callback is responsible to populate @a signature and @a signature_len with
 1260             correct values. Set @a signature_len to zero on any error.
 1261             Note the callback is called only once.
 1262             The function cannot be NULL, even when called the second time, to get actual data.
 1263          @param finish_user_data User data value for the @a finishSignature callback.
 1264          @param data [out] Actual data to store the PDF content to. It can be NULL, in which case
 1265             the @a dataLength is populated with large-enough value to hold the whole data.
 1266          @param dataLength [in/out] Tells how many bytes can be stored in @a data. If @a data
 1267             is NULL, then it is set to large-enough value. Passing non-NULL @a data with no enough
 1268             large buffer results in a failure with no change on @a dataLength.
 1269          @return Whether succeeded.
 1270 
 1271          @note The only valid operation after this is either call of @ref SaveToDataWithSignManual again,
 1272             to get information about necessary buffer size or data itself, close the document
 1273             with @ref Close, or free the @ref TLitePDF object.
 1274 
 1275          @note Signing already signed document can cause breakage of previous signatures, thus
 1276             check whether the loaded document is already signed with @ref GetDocumentIsSigned.
 1277             Load the document with its @a forUpdate parameter set to true, to sign an existing document.
 1278 
 1279          @see SaveToFileWithSignManual, SaveToFileWithSign
 1280       }
 1281 
 1282       procedure EmbedFile(const fileName : AnsiString);
 1283       {**<
 1284          Embeds a file into a PDF document.
 1285 
 1286          @param fileName File name of the file to be attached.
 1287          @return Whether succeeded.
 1288 
 1289          @note Files can be embed only to memory-based documents.
 1290 
 1291          @note The path is stripped from the @a fileName. The @a fileName is used as a key,
 1292             aka it's not possible to embed two files of the same name into a PDF document.
 1293 
 1294          @see EmbedFileW, EmbedData, CreateMemDocument
 1295       }
 1296 
 1297       procedure EmbedFileW(const fileName : WideString);
 1298       {**<
 1299          This is the same as @ref EmbedFile, the only difference is that
 1300          the @a fileName is a wide string.
 1301       }
 1302 
 1303       procedure EmbedData(const fileName : AnsiString;
 1304                           data : PByte;
 1305                           dataLength : LongWord);
 1306       {**<
 1307          Embeds a data (file) into a PDF document.
 1308 
 1309          @param fileName File name to be used for the data identification.
 1310          @param data Actual data to be attached.
 1311          @param dataLength Length of the data.
 1312 
 1313          @note Data can be embed only to memory-based documents.
 1314 
 1315          @note The path is stripped from the @a fileName. The @a fileName is used as a key,
 1316             aka it's not possible to embed two files of the same name into a PDF document.
 1317 
 1318          @see EmbedDataW, EmbedFile, CreateMemDocument
 1319       }
 1320 
 1321       procedure EmbedDataW(const fileName : WideString;
 1322                            data : PByte;
 1323                            dataLength : LongWord);
 1324       {**<
 1325          This is the same as @ref EmbedData, the only difference is that
 1326          the @a fileName is a wide string.
 1327       }
 1328 
 1329       function GetEmbeddedFileCount : Integer;
 1330       {**<
 1331          Gets count of embedded files stored in a PDF document.
 1332 
 1333          @return Count of found embedded files, or -1 on error.
 1334 
 1335          @see EmbedFile, EmbedData, GetEmbeddedFileName, GetEmbeddedFileData
 1336       }
 1337 
 1338       function GetEmbeddedFileName(index : LongWord) : AnsiString;
 1339       {**<
 1340          Gets embedded file's name, as stored in a PDF document.
 1341 
 1342          @param index Index of the embedded file; returns failure, if out of range.
 1343          @return File's name, as stored in a PDF document.
 1344 
 1345          @see GetEmbeddedFileNameW, EmbedFile, EmbedData, GetEmbeddedFileCount, GetEmbeddedFileData
 1346       }
 1347 
 1348       function GetEmbeddedFileNameW(index : LongWord) : WideString;
 1349       {**<
 1350          This is the same as @ref GetEmbeddedFileName, the only difference is that
 1351          the @a fileName is a wide string.
 1352       }
 1353 
 1354       function GetEmbeddedFileData(index : LongWord;
 1355                                    data : PByte;
 1356                                    var dataLength : LongWord) : Boolean;
 1357       {**<
 1358          Gets embedded file's data, as stored in a PDF document. There are no data returned,
 1359          if the file was not embed.
 1360 
 1361          @param index Index of the embedded file; returns failure, if out of range.
 1362          @param data [out] Actual embedded file's data, as stored in the PDF. It can be NULL, in which case
 1363             the @a dataLength is populated with large-enough value to hold the whole data.
 1364          @param dataLength [in/out] Tells how many bytes can be stored in @a data. If @a data
 1365             is NULL, then it is set to large-enough value. Passing non-NULL @a data with no enough
 1366             large buffer results in a failure with no change on @a dataLength.
 1367          @return Whether succeeded.
 1368 
 1369          @see EmbedFile, EmbedData, GetEmbeddedFileCount, GetEmbeddedFileName
 1370       }
 1371 
 1372       function GetPoDoFoDocument : Pointer;
 1373       {**<
 1374          Gets a pointer to PoDoFo::PdfDocument document, which is currently opened.
 1375          The returned pointer is owned by litePDF, do not free it. It is valid until
 1376          the document is closed.
 1377 
 1378          @return Pointer to currently opened PoDoFo::PdfDocument.
 1379 
 1380          @see Close
 1381       }
 1382 
 1383       procedure DrawDebugPage(const filename : AnsiString);
 1384       {**<
 1385          Draws saved debugPage as a new page into the PDF file. There should not be
 1386          running any drawing when calling this function (like no page can be opened
 1387          for drawing).
 1388 
 1389          @param filename File name with full path for litePDF debug page.
 1390       }
 1391 
 1392       procedure CreateLinkAnnotation(annotationPageIndex : LongWord;
 1393                                      annotationPosition_u : TRect;
 1394                                      annotationFlags : LongWord;
 1395                                      annotationResourceID : LongWord;
 1396                                      destinationPageIndex : LongWord;
 1397                                      destinationX_u : LongWord;
 1398                                      destinationY_u : LongWord;
 1399                                      const destinationDescription : WideString);
 1400       {**<
 1401          Creates a link annotation at the given page and position, which will target the given
 1402          destination page and the position in it. The object should hold a memory-based document.
 1403          Note, the link annotation can be created only when the document is not drawing, to
 1404          have all the document pages available.
 1405 
 1406          @param annotationPageIndex Page index where to place the link annotation.
 1407          @param annotationPosition_u Where to place the annotation on the page, in the current unit.
 1408          @param annotationFlags Bit-or of @ref TLitePDFAnnotationFlags flags.
 1409          @param annotationResourceID Optional resource ID of the annotation content, as shown
 1410             to a user. 0 means do not add additional visualization on the page, but the annotation
 1411             can be still clicked.
 1412          @param destinationPageIndex Page index where the link points to.
 1413          @param destinationX_u X-origin of the destination on the page, in the current unit.
 1414          @param destinationY_u Y-origin of the destination on the page, in the current unit.
 1415          @param destinationDescription Optional destination description, which can be used
 1416             for accessibility reasons by the viewer.
 1417 
 1418          @see GetPageCount, AddResource, CreateBookmarkRoot
 1419       *}
 1420 
 1421       function CreateBookmarkRoot(const title : WideString;
 1422                                   flags : LongWord;
 1423                                   titleColor : TColor;
 1424                                   destinationPageIndex : LongWord;
 1425                                   destinationX_u : LongWord;
 1426                                   destinationY_u : LongWord) : LongWord;
 1427       {**<
 1428          Creates a new root (top-level) bookmark, which will target the given destination
 1429          page and the position in it. The object should hold a memory-based document.
 1430          Note, the bookmarks can be created only when the document is not drawing, to
 1431          have all the document pages available.
 1432 
 1433          @param title Title of the bookmark.
 1434          @param flags Bit-or of @ref TLitePDFBookmarkFlags flags.
 1435          @param titleColor Title text color.
 1436          @param destinationPageIndex Page index where the link points to.
 1437          @param destinationX_u X-origin of the destination on the page, in the current unit.
 1438          @param destinationY_u Y-origin of the destination on the page, in the current unit.
 1439          @return Created bookmark ID or 0, when the bookmark could not be created.
 1440 
 1441          @see CreateBookmarkChild, CreateBookmarkSibling, CreateLinkAnnotation
 1442       *}
 1443 
 1444       function CreateBookmarkChild(parentBookmarkID : LongWord;
 1445                                    const title : WideString;
 1446                                    flags : LongWord;
 1447                                    titleColor : TColor;
 1448                                    destinationPageIndex : LongWord;
 1449                                    destinationX_u : LongWord;
 1450                                    destinationY_u : LongWord) : LongWord;
 1451       {**<
 1452          Creates a new child bookmark, which will target the given destination
 1453          page and the position in it. The object should hold a memory-based document.
 1454          Note, the bookmarks can be created only when the document is not drawing, to
 1455          have all the document pages available.
 1456 
 1457          @param parentBookmarkID Bookmark ID of the parent bookmark. The child will be
 1458             created under this bookmark.
 1459          @param title Title of the bookmark.
 1460          @param flags Bit-or of @ref TLitePDFBookmarkFlags flags.
 1461          @param titleColor Title text color.
 1462          @param destinationPageIndex Page index where the link points to.
 1463          @param destinationX_u X-origin of the destination on the page, in the current unit.
 1464          @param destinationY_u Y-origin of the destination on the page, in the current unit.
 1465          @return Created bookmark ID or 0, when the bookmark could not be created.
 1466 
 1467          @see CreateBookmarkRoot, CreateBookmarkSibling, CreateLinkAnnotation
 1468       *}
 1469 
 1470       function CreateBookmarkSibling(previousBookmarkID: LongWord;
 1471                                      const title : WideString;
 1472                                      flags : LongWord;
 1473                                      titleColor : TColor;
 1474                                      destinationPageIndex : LongWord;
 1475                                      destinationX_u : LongWord;
 1476                                      destinationY_u : LongWord) : LongWord;
 1477       {**<
 1478          Creates a new sibling (next) bookmark, which will target the given destination
 1479          page and the position in it. The object should hold a memory-based document.
 1480          Note, the bookmarks can be created only when the document is not drawing, to
 1481          have all the document pages available.
 1482 
 1483          @param previousBookmarkID Bookmark ID of the previous bookmark. The sibling will be
 1484             created as the next of this bookmark.
 1485          @param title Title of the bookmark.
 1486          @param flags Bit-or of @ref TLitePDFBookmarkFlags flags.
 1487          @param titleColor Title text color.
 1488          @param destinationPageIndex Page index where the link points to.
 1489          @param destinationX_u X-origin of the destination on the page, in the current unit.
 1490          @param destinationY_u Y-origin of the destination on the page, in the current unit.
 1491          @return Created bookmark ID or 0, when the bookmark could not be created.
 1492 
 1493          @see CreateBookmarkRoot, CreateBookmarkChild, CreateLinkAnnotation
 1494       *}
 1495    end;
 1496 //---------------------------------------------------------------------------
 1497 
 1498 implementation
 1499 
 1500 const
 1501    LITEPDF_DLL_NAME = 'litePDF.dll';
 1502 
 1503 //----------------------------------------------------------------------------
 1504 
 1505 procedure ThrowIfFail(expr : Boolean;
 1506                       const exprStr : AnsiString;
 1507                       const _func : AnsiString);
 1508 var exmsg : AnsiString;
 1509 begin
 1510    if not expr then
 1511    begin
 1512       exmsg := _func + ': Assertion ''' + exprStr + ''' failed';
 1513       raise TLitePDFException.Create(ERROR_INVALID_PARAMETER, exmsg);
 1514    end;
 1515 end;
 1516 
 1517 procedure ThrowMessageIfFail(expr : Boolean;
 1518                              const msg : AnsiString;
 1519                              const _func : AnsiString);
 1520 var exmsg : AnsiString;
 1521 begin
 1522    if not expr then
 1523    begin
 1524       exmsg := _func + ': ' + msg;
 1525       raise TLitePDFException.Create(ERROR_INVALID_PARAMETER, exmsg);
 1526    end;
 1527 end;
 1528 
 1529 procedure ThrowLastErrorIfFail(expr : Boolean;
 1530                                lpdf : TLitePDF;
 1531                                const _func : AnsiString);
 1532 var exmsg : AnsiString;
 1533 begin
 1534    if not expr then
 1535    begin
 1536       exmsg := 'Failed to call ''' + _func + '''';
 1537       if Length(lpdf.getLastErrorMessage) <> 0 then
 1538       begin
 1539          exmsg := lpdf.getLastErrorMessage;
 1540       end;
 1541       raise TLitePDFException.Create(lpdf.getLastErrorCode, exmsg);
 1542    end;
 1543 end;
 1544 
 1545 procedure litePDFError(code : LongWord;
 1546                        const msg : PAnsiChar;
 1547                        user_data : Pointer); stdcall;
 1548 const _func = 'LitePDF::litePDFError';
 1549 var lpdf : TLitePDF;
 1550 begin
 1551    ThrowIfFail(user_data <> nil, 'user_data <> nil', _func);
 1552 
 1553    lpdf := TLitePDF(user_data);
 1554 
 1555    lpdf.setLastError(code, msg);
 1556    if Assigned(lpdf.onError) then begin
 1557       lpdf.onError(code, msg, lpdf.onErrorUserData);
 1558    end;
 1559 end;
 1560 
 1561 //----------------------------------------------------------------------------
 1562 
 1563 constructor TLitePDFException.Create(pCode : DWORD;
 1564                                      const pMsg : AnsiString);
 1565 begin
 1566    inherited Create(string(pMsg));
 1567    code := pCode;
 1568    msg := pMsg;
 1569 end;
 1570 
 1571 constructor TLitePDFException.Create(const src: TLitePDFException);
 1572 begin
 1573    inherited Create(string(src.getMessage));
 1574    code := src.getCode;
 1575    msg := src.getMessage;
 1576 end;
 1577 
 1578 function TLitePDFException.getCode : DWORD;
 1579 begin
 1580    Result := code;
 1581 end;
 1582 
 1583 function TLitePDFException.getMessage : AnsiString;
 1584 begin
 1585    Result := msg;
 1586 end;
 1587 
 1588 //----------------------------------------------------------------------------
 1589 
 1590 constructor TLitePDF.Create;
 1591 begin
 1592    inherited Create;
 1593 
 1594    lib := THandle(0);
 1595    context := nil;
 1596    lastErrorCode := 0;
 1597    lastErrorMessage := '';
 1598    onError := nil;
 1599    onErrorUserData := nil;
 1600 end;
 1601 
 1602 destructor TLitePDF.Destroy;
 1603 begin
 1604    unloadLibrary;
 1605 
 1606    inherited;
 1607 end;
 1608 
 1609 function TLitePDF.GetProc(const pProcIdent : PAnsiChar) : FARPROC;
 1610 const _func = 'TLitePDF.GetProc';
 1611 var err : AnsiString;
 1612 begin
 1613    ensureLibraryLoaded(_func);
 1614 
 1615    ThrowIfFail(Assigned(pProcIdent), 'Assigned(pProcIdent)', _func);
 1616    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 1617 
 1618    Result := GetProcAddress(lib, pProcIdent);
 1619 
 1620    err := 'Proc ''' + AnsiString(pProcIdent) + ''' not found';
 1621 
 1622    ThrowMessageIfFail(Result <> FARPROC(0), err, _func);
 1623 end;
 1624 
 1625 procedure TLitePDF.setOnError(pOnError: TLitePDFErrorEvent;
 1626                               pOnErrorUserData : Pointer);
 1627 begin
 1628    onError := pOnError;
 1629    onErrorUserData := pOnErrorUserData;
 1630 end;
 1631 
 1632 function TLitePDF.getLastErrorCode: LongWord;
 1633 begin
 1634    Result := lastErrorCode;
 1635 end;
 1636 
 1637 function TLitePDF.getLastErrorMessage : AnsiString;
 1638 begin
 1639    Result := lastErrorMessage
 1640 end;
 1641 
 1642 procedure TLitePDF.freeLastError;
 1643 begin
 1644    lastErrorMessage := '';
 1645    lastErrorCode := 0;
 1646 end;
 1647 
 1648 procedure TLitePDF.setLastError(code : DWORD;
 1649                                 const msg : PAnsiChar);
 1650 begin
 1651    freeLastError;
 1652 
 1653    lastErrorCode := code;
 1654    if msg <> nil then
 1655    begin
 1656       lastErrorMessage := msg;
 1657    end;
 1658 end;
 1659 
 1660 function TLitePDF.checkAPIVersion(major : LongWord;
 1661                                   minor : LongWord) : Boolean;
 1662 var fileName : array[0..2049] of AnsiChar;
 1663    fileNameLen : DWORD;
 1664    apiIsOK : Boolean;
 1665    dwVerHnd, dwVerInfoSize : DWORD;
 1666    hMem : THandle;
 1667    lpvMem : Pointer;
 1668    VersionInfo : PVSFixedFileInfo;
 1669    cchVer : UINT;
 1670    fRet : BOOL;
 1671 begin
 1672    Result := False;
 1673 
 1674    if lib = THandle(0) then
 1675    begin
 1676       Exit;
 1677    end;
 1678 
 1679    fileNameLen := GetModuleFileNameA(lib, fileName, 2048);
 1680    if fileNameLen = 0 then
 1681    begin
 1682       Exit;
 1683    end;
 1684 
 1685    fileName[fileNameLen] := Char(0);
 1686 
 1687    apiIsOK := False;
 1688 
 1689    dwVerInfoSize := GetFileVersionInfoSizeA(fileName, dwVerHnd);
 1690 
 1691    if dwVerInfoSize <> 0 then
 1692    begin
 1693       hMem := GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
 1694       lpvMem := GlobalLock(hMem);
 1695 
 1696       VersionInfo := nil;
 1697       if GetFileVersionInfoA(fileName, dwVerHnd, dwVerInfoSize, lpvMem) then
 1698       begin
 1699          fRet := VerQueryValueA(Pointer(lpvMem), '\\', Pointer(VersionInfo), cchVer);
 1700 
 1701          if (fRet) and (cchVer <> 0) then
 1702          begin
 1703             apiIsOK := (HiWord(VersionInfo.dwFileVersionMS) = major) and
 1704                        (LoWord(VersionInfo.dwFileVersionMS) = minor);
 1705          end;
 1706 
 1707          GlobalUnlock(hMem);
 1708          GlobalFree(hMem);
 1709       end;
 1710    end;
 1711 
 1712    Result := apiIsOK;
 1713 end;
 1714 
 1715 procedure TLitePDF.ensureLibraryLoaded(const _func : PAnsiChar);
 1716 type
 1717    litePDFErrorCB = procedure (code : LongWord; const msg : PAnsiChar; user_data : Pointer); stdcall;
 1718    lpfunc = function (const authKey : PAnsiChar; on_error : litePDFErrorCB; on_error_user_data : Pointer) : Pointer; stdcall;
 1719 var exmsg : AnsiString;
 1720    func : lpfunc;
 1721 begin
 1722    if lib <> THandle(0) then
 1723    begin
 1724       Exit;
 1725    end;
 1726 
 1727    ThrowIfFail(lib = THandle(0), 'lib = THandle(0)', _func);
 1728    ThrowIfFail(context = nil, 'context = nil', _func);
 1729 
 1730    lib := LoadLibrary(LITEPDF_DLL_NAME);
 1731    ThrowMessageIfFail (lib <> THandle(0), 'Failed to open litePDF.dll', _func);
 1732 
 1733    if not checkAPIVersion(LitePDF_API_Major, LitePDF_API_Minor) then
 1734    begin
 1735       FreeLibrary (lib);
 1736       lib := THandle(0);
 1737 
 1738       exmsg := AnsiString(_func) + ': ' + 'This TLitePDF class is not designed for API version of litePDF.dll';
 1739       raise TLitePDFException.Create(ERROR_INVALID_DLL, exmsg);
 1740    end;
 1741 
 1742    freeLastError;
 1743    func := lpfunc(GetProc('litePDF_CreateContext'));
 1744 
 1745    context := func (PAnsiChar(LitePDFAuthorizationKey), litePDFError, self);
 1746 
 1747    if context = nil then
 1748    begin
 1749       FreeLibrary (lib);
 1750       lib := THandle(0);
 1751       ThrowMessageIfFail (context <> nil, 'Failed to create context', _func);
 1752    end;
 1753 end;
 1754 
 1755 procedure TLitePDF.unloadLibrary;
 1756 type lpfunc = procedure(context : Pointer); stdcall;
 1757 var func : lpfunc;
 1758 begin
 1759    if (lib <> THandle(0)) and (context <> nil) then
 1760    begin
 1761       try
 1762          freeLastError;
 1763          func := lpfunc(GetProc('litePDF_FreeContext'));
 1764          func(context);
 1765       except
 1766       end;
 1767       FreeLibrary(lib);
 1768    end;
 1769 
 1770    context := nil;
 1771    lib := THandle(0);
 1772 end;
 1773 
 1774 procedure TLitePDF.SetUnit(unitValue : TLitePDFUnit);
 1775 const _func = 'TLitePDF.SetUnit';
 1776 type lpfunc = function(pctx : Pointer; unitValue : LongWord) : BOOL; stdcall;
 1777 var func : lpfunc;
 1778 begin
 1779    ensureLibraryLoaded(_func);
 1780 
 1781    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 1782 
 1783    freeLastError;
 1784    func := lpfunc(GetProc('litePDF_SetUnit'));
 1785 
 1786    ThrowLastErrorIfFail(func(context, LongWord(unitValue)), self, _func);
 1787 end;
 1788 
 1789 function TLitePDF.GetUnit : TLitePDFUnit;
 1790 const _func = 'TLitePDF.GetUnit';
 1791 type lpfunc = function(pctx : Pointer) : LongWord; stdcall;
 1792 var func : lpfunc;
 1793     currentUnit : LongWord;
 1794 begin
 1795    ensureLibraryLoaded(_func);
 1796 
 1797    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 1798 
 1799    freeLastError;
 1800    func := lpfunc(GetProc('litePDF_GetUnit'));
 1801 
 1802    currentUnit := func(context);
 1803 
 1804    ThrowLastErrorIfFail((TLitePDFUnit(currentUnit) > LitePDFUnit_Unknown) and (TLitePDFUnit(currentUnit) <= LitePDFUnit_1000th_inch), self, _func);
 1805 
 1806    Result := TLitePDFUnit(currentUnit);
 1807 end;
 1808 function TLitePDF.MMToUnitEx(useUnit : TLitePDFUnit;
 1809                              mmValue : Double) : Double;
 1810 var ratio : Double;
 1811 begin
 1812    ratio := 1.0;
 1813 
 1814    case useUnit of
 1815       LitePDFUnit_mm:
 1816          ratio := 1.0;
 1817       LitePDFUnit_10th_mm:
 1818          ratio := 10.0;
 1819       LitePDFUnit_100th_mm:
 1820          ratio := 100.0;
 1821       LitePDFUnit_1000th_mm:
 1822          ratio := 1000.0;
 1823       LitePDFUnit_inch:
 1824          ratio := 1.0 / 25.4;
 1825       LitePDFUnit_10th_inch:
 1826          ratio := 10.0 / 25.4;
 1827       LitePDFUnit_100th_inch:
 1828          ratio := 100.0 / 25.4;
 1829       LitePDFUnit_1000th_inch:
 1830          ratio := 1000.0 / 25.4;
 1831       LitePDFUnit_Unknown:
 1832          ;
 1833    end;
 1834 
 1835    Result := mmValue * ratio;
 1836 end;
 1837 
 1838 function TLitePDF.UnitToMMEx(useUnit : TLitePDFUnit;
 1839                              unitValue : Double) : Double;
 1840 var ratio : Double;
 1841 begin
 1842    ratio := 1.0;
 1843 
 1844    case useUnit of
 1845       LitePDFUnit_mm:
 1846          ratio := 1.0;
 1847       LitePDFUnit_10th_mm:
 1848          ratio := 1.0 / 10.0;
 1849       LitePDFUnit_100th_mm:
 1850          ratio := 1.0 / 100.0;
 1851       LitePDFUnit_1000th_mm:
 1852          ratio := 1.0 / 1000.0;
 1853       LitePDFUnit_inch:
 1854          ratio := 25.4;
 1855       LitePDFUnit_10th_inch:
 1856          ratio := 25.4 / 10.0;
 1857       LitePDFUnit_100th_inch:
 1858          ratio := 25.4 / 100.0;
 1859       LitePDFUnit_1000th_inch:
 1860          ratio := 25.4 / 1000.0;
 1861       LitePDFUnit_Unknown:
 1862          ;
 1863    end;
 1864 
 1865    Result := unitValue * ratio;
 1866 end;
 1867 
 1868 function TLitePDF.InchToUnitEx(useUnit : TLitePDFUnit;
 1869                                inchValue : Double) : Double;
 1870 var ratio : Double;
 1871 begin
 1872    ratio := 1.0;
 1873 
 1874    case useUnit of
 1875       LitePDFUnit_mm:
 1876          ratio := 25.4;
 1877       LitePDFUnit_10th_mm:
 1878          ratio := 10.0 * 25.4;
 1879       LitePDFUnit_100th_mm:
 1880          ratio := 100.0 * 25.4;
 1881       LitePDFUnit_1000th_mm:
 1882          ratio := 1000.0 * 25.4;
 1883       LitePDFUnit_inch:
 1884          ratio := 1.0;
 1885       LitePDFUnit_10th_inch:
 1886          ratio := 10.0;
 1887       LitePDFUnit_100th_inch:
 1888          ratio := 100.0;
 1889       LitePDFUnit_1000th_inch:
 1890          ratio := 1000.0;
 1891       LitePDFUnit_Unknown:
 1892          ;
 1893    end;
 1894 
 1895    Result := inchValue * ratio;
 1896 end;
 1897 
 1898 function TLitePDF.UnitToInchEx(useUnit : TLitePDFUnit;
 1899                                unitValue : Double) : Double;
 1900 var ratio : Double;
 1901 begin
 1902    ratio := 1.0;
 1903 
 1904    case useUnit of
 1905       LitePDFUnit_mm:
 1906          ratio := 1.0 / 25.4;
 1907       LitePDFUnit_10th_mm:
 1908          ratio := 1.0 / (25.4 * 10.0);
 1909       LitePDFUnit_100th_mm:
 1910          ratio := 1.0 / (25.4 * 100.0);
 1911       LitePDFUnit_1000th_mm:
 1912          ratio := 1.0 / (25.4 * 1000.0);
 1913       LitePDFUnit_inch:
 1914          ratio := 1.0;
 1915       LitePDFUnit_10th_inch:
 1916          ratio := 1.0 / 10.0;
 1917       LitePDFUnit_100th_inch:
 1918          ratio := 1.0 / 100.0;
 1919       LitePDFUnit_1000th_inch:
 1920          ratio := 1.0 / 1000.0;
 1921       LitePDFUnit_Unknown:
 1922          ;
 1923    end;
 1924 
 1925    Result := unitValue * ratio;
 1926 end;
 1927 
 1928 function TLitePDF.MMToUnit(mmValue : Double) : Double;
 1929 begin
 1930    Result := MMToUnitEx(GetUnit, mmValue);
 1931 end;
 1932 
 1933 function TLitePDF.UnitToMM(unitValue : Double) : Double;
 1934 begin
 1935    Result := UnitToMMEx(GetUnit, unitValue);
 1936 end;
 1937 
 1938 function TLitePDF.InchToUnit(inchValue : Double) : Double;
 1939 begin
 1940    Result := InchToUnitEx(GetUnit, inchValue);
 1941 end;
 1942 
 1943 function TLitePDF.UnitToInch(unitValue : Double) : Double;
 1944 begin
 1945    Result := UnitToInchEx(GetUnit, unitValue);
 1946 end;
 1947 
 1948 procedure TLitePDF.PrepareEncryption(userPassword : AnsiString;
 1949                                      ownerPassword : AnsiString;
 1950                                      permissions : LongWord;
 1951                                      algorithm : LongWord);
 1952 const _func = 'TLitePDF.PrepareEncryption';
 1953 type lpfunc = function(pctx : Pointer; const userPassword : PAnsiChar; const ownerPassword : PAnsiChar; permissions : LongWord; algorithm : LongWord) : BOOL; stdcall;
 1954 var func : lpfunc;
 1955    ownerPass : PAnsiChar;
 1956 begin
 1957    ensureLibraryLoaded(_func);
 1958 
 1959    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 1960 
 1961    freeLastError;
 1962    func := lpfunc(GetProc('litePDF_PrepareEncryption'));
 1963 
 1964    if Length(ownerPassword) <= 0 then
 1965       ownerPass := nil
 1966    else
 1967       ownerPass := PAnsiChar(ownerPassword);
 1968 
 1969    ThrowLastErrorIfFail(func(context, PAnsiChar(userPassword), ownerPass, permissions, algorithm), self, _func);
 1970 end;
 1971 
 1972 procedure TLitePDF.CreateFileDocument(const fileName : AnsiString);
 1973 const _func = 'TLitePDF.CreateFileDocument';
 1974 type lpfunc = function(pctx : Pointer; const fileName : PAnsiChar) : BOOL; stdcall;
 1975 var func : lpfunc;
 1976 begin
 1977    ensureLibraryLoaded(_func);
 1978 
 1979    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 1980 
 1981    freeLastError;
 1982    func := lpfunc(GetProc('litePDF_CreateFileDocument'));
 1983 
 1984    ThrowLastErrorIfFail(func(context, PAnsiChar(fileName)), self, _func);
 1985 end;
 1986 
 1987 procedure TLitePDF.CreateFileDocumentW(const fileName : WideString);
 1988 const _func = 'TLitePDF.CreateFileDocumentW';
 1989 type lpfunc = function(pctx : Pointer; const fileName : PWideChar) : BOOL; stdcall;
 1990 var func : lpfunc;
 1991 begin
 1992    ensureLibraryLoaded(_func);
 1993 
 1994    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 1995 
 1996    freeLastError;
 1997    func := lpfunc(GetProc('litePDF_CreateFileDocumentW'));
 1998 
 1999    ThrowLastErrorIfFail(func(context, PWideChar(fileName)), self, _func);
 2000 end;
 2001 
 2002 procedure TLitePDF.CreateMemDocument;
 2003 const _func = 'TLitePDF.CreateMemDocument';
 2004 type lpfunc = function(pctx : Pointer) : BOOL; stdcall;
 2005 var func : lpfunc;
 2006 begin
 2007    ensureLibraryLoaded(_func);
 2008 
 2009    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2010 
 2011    freeLastError;
 2012    func := lpfunc(GetProc('litePDF_CreateMemDocument'));
 2013 
 2014    ThrowLastErrorIfFail(func(context), self, _func);
 2015 end;
 2016 
 2017 procedure TLitePDF.LoadFromFile(const fileName : AnsiString;
 2018                                 const password : AnsiString;
 2019                                 loadCompletely : Boolean;
 2020                                 forUpdate : Boolean);
 2021 const _func = 'TLitePDF.LoadFromFile';
 2022 type lpfunc = function(pctx : Pointer; const fileName : PAnsiChar; const password : PAnsiChar; loadCompletely : BOOL; forUpdate : BOOL) : BOOL; stdcall;
 2023 var func : lpfunc;
 2024    lc : BOOL;
 2025    fu : BOOL;
 2026 begin
 2027    ensureLibraryLoaded(_func);
 2028 
 2029    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2030    ThrowIfFail(Length(fileName) > 0, 'Length(fileName) > 0', _func);
 2031 
 2032    freeLastError;
 2033    func := lpfunc(GetProc('litePDF_LoadFromFile'));
 2034 
 2035    if loadCompletely then
 2036       lc := TRUE
 2037    else
 2038       lc := FALSE;
 2039 
 2040    if forUpdate then
 2041       fu := TRUE
 2042    else
 2043       fu := FALSE;
 2044 
 2045    ThrowLastErrorIfFail(func(context, PAnsiChar(fileName), PAnsiChar(password), lc, fu), self, _func);
 2046 end;
 2047 
 2048 procedure TLitePDF.LoadFromFileW(const fileName : WideString;
 2049                                  const password : AnsiString;
 2050                                  loadCompletely : Boolean;
 2051                                  forUpdate : Boolean);
 2052 const _func = 'TLitePDF.LoadFromFileW';
 2053 type lpfunc = function(pctx : Pointer; const fileName : PWideChar; const password : PAnsiChar; loadCompletely : BOOL; forUpdate : BOOL) : BOOL; stdcall;
 2054 var func : lpfunc;
 2055    lc : BOOL;
 2056    fu : BOOL;
 2057 begin
 2058    ensureLibraryLoaded(_func);
 2059 
 2060    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2061    ThrowIfFail(Length(fileName) > 0, 'Length(fileName) > 0', _func);
 2062 
 2063    freeLastError;
 2064    func := lpfunc(GetProc('litePDF_LoadFromFileW'));
 2065 
 2066    if loadCompletely then
 2067       lc := TRUE
 2068    else
 2069       lc := FALSE;
 2070 
 2071    if forUpdate then
 2072       fu := TRUE
 2073    else
 2074       fu := FALSE;
 2075 
 2076    ThrowLastErrorIfFail(func(context, PWideChar(fileName), PAnsiChar(password), lc, fu), self, _func);
 2077 end;
 2078 
 2079 procedure TLitePDF.LoadFromData(data : PByte;
 2080                                 dataLength : LongWord;
 2081                                 const password : AnsiString;
 2082                                 forUpdate : Boolean);
 2083 const _func = 'TLitePDF.LoadFromData';
 2084 type lpfunc = function(pctx : Pointer; const data : PByte; dataLength : LongWord; const password : PAnsiChar; forUpdate : BOOL) : BOOL; stdcall;
 2085 var func : lpfunc;
 2086     fu : BOOL;
 2087 begin
 2088    ensureLibraryLoaded(_func);
 2089 
 2090    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2091    ThrowIfFail(data <> nil, 'data <> nil', _func);
 2092 
 2093    freeLastError;
 2094    func := lpfunc(GetProc('litePDF_LoadFromData'));
 2095 
 2096    if forUpdate then
 2097       fu := TRUE
 2098    else
 2099       fu := FALSE;
 2100 
 2101    ThrowLastErrorIfFail(func(context, data, dataLength, PAnsiChar(password), fu), self, _func);
 2102 end;
 2103 
 2104 procedure TLitePDF.SaveToFile(const fileName : AnsiString);
 2105 const _func = 'TLitePDF.SaveToFile';
 2106 type lpfunc = function(pctx : Pointer; const fileName : PAnsiChar) : BOOL; stdcall;
 2107 var func : lpfunc;
 2108 begin
 2109    ensureLibraryLoaded(_func);
 2110 
 2111    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2112 
 2113    freeLastError;
 2114    func := lpfunc(GetProc('litePDF_SaveToFile'));
 2115 
 2116    ThrowLastErrorIfFail(func(context, PAnsiChar(fileName)), self, _func);
 2117 end;
 2118 
 2119 procedure TLitePDF.SaveToFileW(const fileName : WideString);
 2120 const _func = 'TLitePDF.SaveToFileW';
 2121 type lpfunc = function(pctx : Pointer; const fileName : PWideChar) : BOOL; stdcall;
 2122 var func : lpfunc;
 2123 begin
 2124    ensureLibraryLoaded(_func);
 2125 
 2126    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2127 
 2128    freeLastError;
 2129    func := lpfunc(GetProc('litePDF_SaveToFileW'));
 2130 
 2131    ThrowLastErrorIfFail(func(context, PWideChar(fileName)), self, _func);
 2132 end;
 2133 
 2134 function TLitePDF.SaveToData(data : PByte;
 2135                              var dataLength : LongWord) : Boolean;
 2136 const _func = 'TLitePDF.SaveToData';
 2137 type lpfunc = function(pctx: Pointer; data : PByte; dataLength : PLongWord) : BOOL; stdcall;
 2138 var func : lpfunc;
 2139 begin
 2140    ensureLibraryLoaded(_func);
 2141 
 2142    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2143 
 2144    freeLastError;
 2145    func := lpfunc(GetProc('litePDF_SaveToData'));
 2146 
 2147    if func(context, data, @dataLength) then
 2148       Result := True
 2149    else
 2150       Result := False;
 2151 end;
 2152 
 2153 procedure TLitePDF.Close;
 2154 const _func = 'TLitePDF.Close';
 2155 type lpfunc = procedure(pctx : Pointer); stdcall;
 2156 var func : lpfunc;
 2157 begin
 2158    ensureLibraryLoaded(_func);
 2159 
 2160    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2161 
 2162    freeLastError;
 2163    func := lpfunc(GetProc('litePDF_Close'));
 2164 
 2165    func(context);
 2166 end;
 2167 
 2168 function TLitePDF.GetPageCount : LongWord;
 2169 const _func = 'TLitePDF.GetPageCount';
 2170 type lpfunc = function(pctx : Pointer; pageCount : PLongWord) : BOOL; stdcall;
 2171 var func : lpfunc;
 2172 begin
 2173    ensureLibraryLoaded(_func);
 2174 
 2175    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2176 
 2177    freeLastError;
 2178    func := lpfunc(GetProc('litePDF_GetPageCount'));
 2179 
 2180    Result := 0;
 2181    ThrowLastErrorIfFail(func(context, @Result), self, _func);
 2182 end;
 2183 
 2184 procedure TLitePDF.GetPageSize(pageIndex : LongWord;
 2185                                var width_u : LongWord;
 2186                                var height_u : LongWord);
 2187 const _func = 'TLitePDF.GetPageSize';
 2188 type lpfunc = function(pctx : Pointer; pageIndex : LongWord; width_u : PLongWord; height_u : PLongWord) : BOOL; stdcall;
 2189 var func : lpfunc;
 2190 begin
 2191    ensureLibraryLoaded(_func);
 2192 
 2193    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2194 
 2195    freeLastError;
 2196    func := lpfunc(GetProc('litePDF_GetPageSize'));
 2197 
 2198    ThrowLastErrorIfFail(func(context, pageIndex, @width_u, @height_u), self, _func);
 2199 end;
 2200 
 2201 function TLitePDF.AddPage(width_u : LongWord;
 2202                           height_u : LongWord;
 2203                           width_px : LongWord;
 2204                           height_px : LongWord;
 2205                           drawFlags : LongWord) : HDC;
 2206 const _func = 'TLitePDF.AddPage';
 2207 type lpfunc = function(pctx : Pointer; width_u : LongWord; height_u : LongWord; width_px : LongWord; height_px : LongWord; drawFlags : LongWord) : HDC; stdcall;
 2208 var func : lpfunc;
 2209 begin
 2210    ensureLibraryLoaded(_func);
 2211 
 2212    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2213 
 2214    freeLastError;
 2215    func := lpfunc(GetProc('litePDF_AddPage'));
 2216 
 2217    Result := func (context, width_u, height_u, width_px, height_px, drawFlags);
 2218    ThrowLastErrorIfFail(Result <> HDC(0), self, _func);
 2219 end;
 2220 
 2221 function TLitePDF.InsertPage(pageIndex : LongWord;
 2222                              width_u : LongWord;
 2223                              height_u : LongWord;
 2224                              width_px : LongWord;
 2225                              height_px : LongWord;
 2226                              drawFlags : LongWord) : HDC;
 2227 const _func = 'TLitePDF.InsertPage';
 2228 type lpfunc = function(pctx : Pointer; pageIndex : LongWord; width_u : LongWord; height_u : LongWord; width_px : LongWord; height_px : LongWord; drawFlags : LongWord) : HDC; stdcall;
 2229 var func : lpfunc;
 2230 begin
 2231    ensureLibraryLoaded(_func);
 2232 
 2233    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2234 
 2235    freeLastError;
 2236    func := lpfunc(GetProc('litePDF_InsertPage'));
 2237 
 2238    Result := func(context, pageIndex, width_u, height_u, width_px, height_px, drawFlags);
 2239    ThrowLastErrorIfFail(Result <> HDC(0), self, _func);
 2240 end;
 2241 
 2242 function TLitePDF.UpdatePage(pageIndex : LongWord;
 2243                              width_px : LongWord;
 2244                              height_px : LongWord;
 2245                              drawFlags : LongWord) : HDC;
 2246 const _func = 'TLitePDF.UpdatePage';
 2247 type lpfunc = function(pctx : Pointer; pageIndex : LongWord; width_px : LongWord; height_px : LongWord; drawFlags : LongWord) : HDC; stdcall;
 2248 var func : lpfunc;
 2249 begin
 2250    ensureLibraryLoaded(_func);
 2251 
 2252    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2253 
 2254    freeLastError;
 2255    func := lpfunc(GetProc('litePDF_UpdatePage'));
 2256 
 2257    Result := func(context, pageIndex, width_px, height_px, drawFlags);
 2258    ThrowLastErrorIfFail(Result <> HDC(0), self, _func);
 2259 end;
 2260 
 2261 procedure TLitePDF.FinishPage(dc : HDC);
 2262 const _func = 'TLitePDF.FinishPage';
 2263 type lpfunc = function(pctx : Pointer; dc : HDC) : BOOL; stdcall;
 2264 var func : lpfunc;
 2265 begin
 2266    ensureLibraryLoaded(_func);
 2267 
 2268    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2269    ThrowIfFail(dc <> HDC(0), 'dc <> HDC(0)', _func);
 2270 
 2271    freeLastError;
 2272    func := lpfunc(GetProc('litePDF_FinishPage'));
 2273 
 2274    ThrowLastErrorIfFail(func(context, dc), self, _func);
 2275 end;
 2276 
 2277 function TLitePDF.AddResource(width_u : LongWord;
 2278                               height_u : LongWord;
 2279                               width_px : LongWord;
 2280                               height_px : LongWord;
 2281                               drawFlags : LongWord) : HDC;
 2282 const _func = 'TLitePDF.AddResource';
 2283 type lpfunc = function(pctx : Pointer; width_u : LongWord; height_u : LongWord; width_px : LongWord; height_px : LongWord; drawFlags : LongWord) : HDC; stdcall;
 2284 var func : lpfunc;
 2285 begin
 2286    ensureLibraryLoaded(_func);
 2287 
 2288    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2289 
 2290    freeLastError;
 2291    func := lpfunc(GetProc('litePDF_AddResource'));
 2292 
 2293    Result := func(context, width_u, height_u, width_px, height_px, drawFlags);
 2294    ThrowLastErrorIfFail(Result <> HDC(0), self, _func);
 2295 end;
 2296 
 2297 function TLitePDF.FinishResource(dc : HDC) : LongWord;
 2298 const _func = 'TLitePDF.FinishResource';
 2299 type lpfunc = function(pctx : Pointer; dc : HDC) : LongWord; stdcall;
 2300 var func : lpfunc;
 2301 begin
 2302    ensureLibraryLoaded(_func);
 2303 
 2304    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2305    ThrowIfFail(dc <> HDC(0), 'dc <> HDC(0)', _func);
 2306 
 2307    freeLastError;
 2308    func := lpfunc(GetProc('litePDF_FinishResource'));
 2309 
 2310    Result := func(context, dc);
 2311    ThrowLastErrorIfFail(Result <> 0, self, _func);
 2312 end;
 2313 
 2314 procedure TLitePDF.DeletePage(pageIndex : LongWord);
 2315 const _func = 'TLitePDF.DeletePage';
 2316 type lpfunc = function(pctx : Pointer; pageIndex : LongWord) : BOOL; stdcall;
 2317 var func : lpfunc;
 2318 begin
 2319    ensureLibraryLoaded(_func);
 2320 
 2321    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2322 
 2323    freeLastError;
 2324    func := lpfunc(GetProc('litePDF_DeletePage'));
 2325 
 2326    ThrowLastErrorIfFail(func(context, pageIndex), self, _func);
 2327 end;
 2328 
 2329 procedure TLitePDF.AddPagesFrom(from : TLitePDF;
 2330                                 pageIndex : LongWord;
 2331                                 pageCount : LongWord);
 2332 const _func = 'TLitePDF.AddPagesFrom';
 2333 type lpfunc = function(pctx : Pointer; pctx_from : Pointer; pageIndex : LongWord; pageCount : LongWord) : BOOL; stdcall;
 2334 var func : lpfunc;
 2335 begin
 2336    ensureLibraryLoaded(_func);
 2337 
 2338    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2339    ThrowIfFail(from <> nil, 'from <> nil', _func);
 2340    ThrowIfFail(from <> self, 'from != self', _func);
 2341    ThrowIfFail(from.context <> nil, 'from.context <> nil', _func);
 2342 
 2343    freeLastError;
 2344    func := lpfunc(GetProc('litePDF_AddPagesFrom'));
 2345 
 2346    ThrowLastErrorIfFail(func(context, from.context, pageIndex, pageCount), self, _func);
 2347 end;
 2348 
 2349 procedure TLitePDF.InsertPageFrom(pageIndexTo : LongWord;
 2350                                   from : TLitePDF;
 2351                                   pageIndexFrom : LongWord);
 2352 const _func = 'TLitePDF.InsertPageFrom';
 2353 type lpfunc = function(pctx : Pointer; pageIndexTo : LongWord; pctx_from : Pointer; pageIndexFrom : LongWord) : BOOL; stdcall;
 2354 var func : lpfunc;
 2355 begin
 2356    ensureLibraryLoaded(_func);
 2357 
 2358    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2359    ThrowIfFail(from <> nil, 'from <> nil', _func);
 2360    ThrowIfFail(from <> self, 'from != self', _func);
 2361    ThrowIfFail(from.context <> nil, 'from.context <> nil', _func);
 2362 
 2363    freeLastError;
 2364    func := lpfunc(GetProc('litePDF_InsertPageFrom'));
 2365 
 2366    ThrowLastErrorIfFail(func(context, pageIndexTo, from.context, pageIndexFrom), self, _func);
 2367 end;
 2368 
 2369 function TLitePDF.AddPageFromAsResource(from : TLitePDF;
 2370                                         pageIndex : LongWord;
 2371                                         useTrimBox : Boolean) : LongWord;
 2372 const _func = 'TLitePDF.AddPageFromAsResource';
 2373 type lpfunc = function(pctx : Pointer; pctx_from : Pointer; pageIndex : LongWord; useTrimBox : BOOL) : LongWord; stdcall;
 2374 var func : lpfunc;
 2375    utb : BOOL;
 2376 begin
 2377    ensureLibraryLoaded(_func);
 2378 
 2379    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2380    ThrowIfFail(from <> nil, 'from <> nil', _func);
 2381    ThrowIfFail(from <> self, 'from != self', _func);
 2382    ThrowIfFail(from.context <> nil, 'from.context <> nil', _func);
 2383 
 2384    freeLastError;
 2385    func := lpfunc(GetProc('litePDF_AddPageFromAsResource'));
 2386 
 2387    if useTrimBox then
 2388       utb := TRUE
 2389    else
 2390       utb := FALSE;
 2391 
 2392    Result := func(context, from.context, pageIndex, utb);
 2393    ThrowLastErrorIfFail(Result <> 0, self, _func);
 2394 end;
 2395 
 2396 function TLitePDF.PageToResource(pageIndex : LongWord) : LongWord;
 2397 const _func = 'TLitePDF.PageToResource';
 2398 type lpfunc = function(pctx : Pointer; pageIndex : LongWord) : LongWord; stdcall;
 2399 var func : lpfunc;
 2400 begin
 2401    ensureLibraryLoaded(_func);
 2402 
 2403    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2404 
 2405    freeLastError;
 2406    func := lpfunc(GetProc('litePDF_PageToResource'));
 2407 
 2408    Result := func(context, pageIndex);
 2409    ThrowLastErrorIfFail(Result <> 0, self, _func);
 2410 end;
 2411 
 2412 procedure TLitePDF.GetResourceSize(resourceID : LongWord;
 2413                                    var width_u : LongWord;
 2414                                    var height_u : LongWord);
 2415 const _func = 'TLitePDF.GetResourceSize';
 2416 type lpfunc = function(pctx : Pointer; resourceID : LongWord; width_u : PLongWord; height_u : PLongWord) : BOOL; stdcall;
 2417 var func : lpfunc;
 2418 begin
 2419    ensureLibraryLoaded(_func);
 2420 
 2421    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2422 
 2423    freeLastError;
 2424    func := lpfunc(GetProc('litePDF_GetResourceSize'));
 2425 
 2426    ThrowLastErrorIfFail(func(context, resourceID, @width_u, @height_u), self, _func);
 2427 end;
 2428 
 2429 procedure TLitePDF.DrawResource(resourceID : LongWord;
 2430                                 pageIndex : LongWord;
 2431                         unitValue : TLitePDFUnit;
 2432                                 x : Integer;
 2433                                 y : Integer;
 2434                                 scaleX : Integer;
 2435                                 scaleY : Integer);
 2436 const _func = 'TLitePDF.DrawResource';
 2437 type lpfunc = function(pctx : Pointer; resourceID : LongWord; pageIndex : LongWord; unitValue : LongWord; x : Integer; y : Integer; scaleX : Integer; scaleY : Integer) : BOOL; stdcall;
 2438 var func : lpfunc;
 2439 begin
 2440    ensureLibraryLoaded(_func);
 2441 
 2442    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2443 
 2444    freeLastError;
 2445    func := lpfunc(GetProc('litePDF_DrawResource'));
 2446 
 2447    ThrowLastErrorIfFail(func(context, resourceID, pageIndex, LongWord(unitValue), x, y, scaleX, scaleY), self, _func);
 2448 end;
 2449 
 2450 procedure TLitePDF.DrawResourceWithMatrix(resourceID : LongWord;
 2451                                           pageIndex : LongWord;
 2452                                           a : Double;
 2453                                           b : Double;
 2454                                           c : Double;
 2455                                           d : Double;
 2456                                           e : Double;
 2457                                           f : Double);
 2458 const _func = 'TLitePDF.DrawResourceWithMatrix';
 2459 type lpfunc = function(pctx : Pointer; resourceID : LongWord; pageIndex : LongWord; a : Integer; b : Integer; c : Integer; d : Integer; e : Integer; f : Integer) : BOOL; stdcall;
 2460 var func : lpfunc;
 2461    i_a, i_b, i_c, i_d, i_e, i_f : Integer;
 2462 begin
 2463    ensureLibraryLoaded(_func);
 2464 
 2465    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2466 
 2467    freeLastError;
 2468    func := lpfunc(GetProc('litePDF_DrawResourceWithMatrix'));
 2469 
 2470    i_a := Trunc(a * 1000.0);
 2471    i_b := Trunc(b * 1000.0);
 2472    i_c := Trunc(c * 1000.0);
 2473    i_d := Trunc(d * 1000.0);
 2474    i_e := Trunc(e * 1000.0);
 2475    i_f := Trunc(f * 1000.0);
 2476 
 2477    ThrowLastErrorIfFail(func(context, resourceID, pageIndex, i_a, i_b, i_c, i_d, i_e, i_f), self, _func);
 2478 end;
 2479 
 2480 procedure TLitePDF.SetDocumentInfo(const name : AnsiString;
 2481                                    const value : WideString);
 2482 const _func = 'TLitePDF.SetDocumentInfo';
 2483 type lpfunc = function(pctx : Pointer; const name : PAnsiChar; const value : PWideChar) : BOOL; stdcall;
 2484 var func : lpfunc;
 2485 begin
 2486    ensureLibraryLoaded(_func);
 2487 
 2488    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2489 
 2490    freeLastError;
 2491    func := lpfunc(GetProc('litePDF_SetDocumentInfo'));
 2492 
 2493    ThrowLastErrorIfFail(func(context, PAnsiChar(name), PWideChar(value)), self, _func);
 2494 end;
 2495 
 2496 function TLitePDF.GetDocumentInfoExists(const name : AnsiString) : Boolean;
 2497 const _func = 'TLitePDF.GetDocumentInfoExists';
 2498 type lpfunc = function(pctx : Pointer; const name : PAnsiChar; pExists : PBOOL) : BOOL; stdcall;
 2499 var func : lpfunc;
 2500    exists : BOOL;
 2501 begin
 2502    ensureLibraryLoaded(_func);
 2503 
 2504    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2505 
 2506    freeLastError;
 2507    func := lpfunc(GetProc('litePDF_GetDocumentInfoExists'));
 2508 
 2509    exists := FALSE;
 2510 
 2511    ThrowLastErrorIfFail(func(context, PAnsiChar(name), @exists), self, _func);
 2512 
 2513    if exists then
 2514       Result := True
 2515    else
 2516       Result := False;
 2517 end;
 2518 
 2519 function TLitePDF.GetDocumentInfo(const name : AnsiString) : WideString;
 2520 const _func = 'TLitePDF.GetDocumentInfo';
 2521 type lpfunc = function(pctx : Pointer; const name : PAnsiChar; value : PWideChar; valueLength : PLongWord) : BOOL; stdcall;
 2522 var func : lpfunc;
 2523    valueLength : LongWord;
 2524    buff : PWideChar;
 2525 begin
 2526    ensureLibraryLoaded(_func);
 2527 
 2528    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2529 
 2530    freeLastError;
 2531    func := lpfunc(GetProc('litePDF_GetDocumentInfo'));
 2532 
 2533    valueLength := 0;
 2534    ThrowLastErrorIfFail(func(context, PAnsiChar(name), nil, @valueLength), self, _func);
 2535 
 2536    buff := AllocMem(SizeOf(WideChar) * (valueLength + 1));
 2537    ThrowMessageIfFail(buff <> nil, 'Out of memory!', _func);
 2538 
 2539    if func(context, PAnsiChar(name), buff, @valueLength) then
 2540    begin
 2541       buff[valueLength] := Char(0);
 2542       Result := buff;
 2543       FreeMem (buff);
 2544    end
 2545    else
 2546    begin
 2547       FreeMem (buff);
 2548 
 2549       // always false
 2550       ThrowLastErrorIfFail(buff = nil, self, _func);
 2551    end;
 2552 end;
 2553 
 2554 function TLitePDF.GetDocumentIsSigned : Boolean;
 2555 const _func = 'TLitePDF.GetDocumentIsSigned';
 2556 type lpfunc = function(pctx : Pointer; pIsSigned : PBOOL) : BOOL; stdcall;
 2557 var func : lpfunc;
 2558    isSigned : BOOL;
 2559 begin
 2560    ensureLibraryLoaded(_func);
 2561 
 2562    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2563 
 2564    freeLastError;
 2565    func := lpfunc(GetProc('litePDF_GetDocumentIsSigned'));
 2566 
 2567    isSigned := FALSE;
 2568 
 2569    ThrowLastErrorIfFail(func(context, @isSigned), self, _func);
 2570 
 2571    if isSigned then
 2572       Result := True
 2573    else
 2574       Result := False;
 2575 end;
 2576 
 2577 function TLitePDF.GetSignatureCount : LongWord;
 2578 const _func = 'TLitePDF.GetSignatureCount';
 2579 type lpfunc = function(pctx : Pointer; pCount : PLongWord) : BOOL; stdcall;
 2580 var func : lpfunc;
 2581    count : LongWord;
 2582 begin
 2583    ensureLibraryLoaded(_func);
 2584 
 2585    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2586 
 2587    freeLastError;
 2588    func := lpfunc(GetProc('litePDF_GetSignatureCount'));
 2589 
 2590    count := 0;
 2591 
 2592    ThrowLastErrorIfFail(func(context, @count), self, _func);
 2593 
 2594    Result := count;
 2595 end;
 2596 
 2597 function TLitePDF.GetSignatureName(index : LongWord) : AnsiString;
 2598 const _func = 'TLitePDF.GetSignatureName';
 2599 type lpfunc = function(pctx : Pointer; index : LongWord; name : PAnsiChar; nameLength : PLongWord) : BOOL; stdcall;
 2600 var func : lpfunc;
 2601    nameLength : LongWord;
 2602    buff : PAnsiChar;
 2603 begin
 2604    ensureLibraryLoaded(_func);
 2605 
 2606    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2607 
 2608    freeLastError;
 2609    func := lpfunc(GetProc('litePDF_GetSignatureName'));
 2610 
 2611    nameLength := 0;
 2612    ThrowLastErrorIfFail(func(context, index, nil, @nameLength), self, _func);
 2613 
 2614    Inc(nameLength);
 2615 
 2616    buff := AllocMem(SizeOf(AnsiChar) * (nameLength));
 2617    ThrowMessageIfFail(buff <> nil, 'Out of memory!', _func);
 2618 
 2619    if func(context, index, buff, @nameLength) then
 2620    begin
 2621       buff[nameLength] := Char(0);
 2622       Result := buff;
 2623       FreeMem (buff);
 2624    end
 2625    else
 2626    begin
 2627       FreeMem (buff);
 2628 
 2629       // always false
 2630       ThrowLastErrorIfFail(buff <> nil, self, _func);
 2631    end;
 2632 end;
 2633 
 2634 function TLitePDF.CreateSignature(name : AnsiString;
 2635                                   annotationPageIndex : LongWord;
 2636                                   annotationPosition_u : TRect;
 2637                                   annotationFlags : LongWord) : LongWord;
 2638 const _func = 'TLitePDF.CreateSignature';
 2639 type lpfunc = function(pctx : Pointer;
 2640                        const name : PAnsiChar;
 2641                        annotationPageIndex : LongWord;
 2642                        annotationX_u : Integer;
 2643                        annotationY_u : Integer;
 2644                        annotationWidth_u : Integer;
 2645                        annotationHeight_u : Integer;
 2646                        annotationFlags : LongWord;
 2647                        pAddedIndex : PLongWord) : BOOL; stdcall;
 2648 var func : lpfunc;
 2649 begin
 2650    ensureLibraryLoaded(_func);
 2651 
 2652    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2653 
 2654    freeLastError;
 2655    func := lpfunc(GetProc('litePDF_CreateSignature'));
 2656 
 2657    ThrowLastErrorIfFail(func(context,
 2658                PAnsiChar(name),
 2659                annotationPageIndex,
 2660                annotationPosition_u.left,
 2661                annotationPosition_u.top,
 2662                annotationPosition_u.right - annotationPosition_u.left,
 2663                annotationPosition_u.bottom - annotationPosition_u.top,
 2664                annotationFlags,
 2665                @Result), self, _func);
 2666 end;
 2667 
 2668 function TLitePDF.GetSignatureHasData(index : LongWord) : Boolean;
 2669 const _func = 'TLitePDF.GetSignatureHasData';
 2670 type lpfunc = function(pctx : Pointer;
 2671                        index : LongWord;
 2672                        pHasData : PBOOL) : BOOL; stdcall;
 2673 var func : lpfunc;
 2674     hasData : BOOL;
 2675 begin
 2676    ensureLibraryLoaded(_func);
 2677 
 2678    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2679 
 2680    freeLastError;
 2681    func := lpfunc(GetProc('litePDF_GetSignatureHasData'));
 2682 
 2683    ThrowLastErrorIfFail(func(context, index, @hasData), self, _func);
 2684 
 2685    if hasData then
 2686       Result := True
 2687    else
 2688       Result := False;
 2689 end;
 2690 
 2691 function TLitePDF.GetSignatureData(index : LongWord;
 2692                                        data : PByte;
 2693                                        var dataLength : LongWord) : Boolean;
 2694 const _func = 'TLitePDF.GetSignatureData';
 2695 type lpfunc = function(pctx : Pointer; index : LongWord; data : PByte; dataLength : PLongWord) : BOOL; stdcall;
 2696 var func : lpfunc;
 2697 begin
 2698    ensureLibraryLoaded(_func);
 2699 
 2700    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2701 
 2702    freeLastError;
 2703    func := lpfunc(GetProc('litePDF_GetSignatureData'));
 2704 
 2705    if func(context, index, data, @dataLength) then
 2706       Result := True
 2707    else
 2708       Result := False;
 2709 end;
 2710 
 2711 function TLitePDF.GetSignatureRanges(index : LongWord;
 2712                                      pRangesArray : PUInt64;
 2713                                      var pRangesArrayLength : LongWord) : Boolean;
 2714 const _func = 'TLitePDF.GetSignatureRanges';
 2715 type lpfunc = function(pctx : Pointer; index : LongWord; pRangesArray : PUInt64; pRangesArrayLength : PLongWord) : BOOL; stdcall;
 2716 var func : lpfunc;
 2717 begin
 2718    ensureLibraryLoaded(_func);
 2719 
 2720    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2721 
 2722    freeLastError;
 2723    func := lpfunc(GetProc('litePDF_GetSignatureRanges'));
 2724 
 2725    if func(context, index, pRangesArray, @pRangesArrayLength) then
 2726       Result := True
 2727    else
 2728       Result := False;
 2729 end;
 2730 
 2731 procedure TLitePDF.SetSignatureDate(index : LongWord;
 2732                                     const dateOfSign : TDateTime);
 2733 const _func = 'TLitePDF.SetSignatureDate';
 2734 type lpfunc = function(pctx : Pointer; index : LongWord; dateOfSign : Int64) : BOOL; stdcall;
 2735 var func : lpfunc;
 2736 begin
 2737    ensureLibraryLoaded(_func);
 2738 
 2739    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2740 
 2741    freeLastError;
 2742    func := lpfunc(GetProc('litePDF_SetSignatureDate'));
 2743 
 2744    ThrowLastErrorIfFail(func(context, index, System.DateUtils.DateTimeToUnix(dateOfSign)), self, _func);
 2745 end;
 2746 
 2747 function TLitePDF.GetSignatureDate(index : LongWord) : TDateTime;
 2748 const _func = 'TLitePDF.GetSignatureDate';
 2749 type lpfunc = function(pctx : Pointer; index : LongWord; pDateOfSign : PInt64) : BOOL; stdcall;
 2750 var func : lpfunc;
 2751     dateOfSign : Int64;
 2752 begin
 2753    ensureLibraryLoaded(_func);
 2754 
 2755    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2756 
 2757    freeLastError;
 2758    func := lpfunc(GetProc('litePDF_GetSignatureDate'));
 2759 
 2760    ThrowLastErrorIfFail(func(context, index, @dateOfSign), self, _func);
 2761 
 2762    Result := System.DateUtils.UnixToDateTime(dateOfSign);
 2763 end;
 2764 
 2765 procedure TLitePDF.SetSignatureReason(index : LongWord;
 2766                                       const reason : WideString);
 2767 const _func = 'TLitePDF.SetSignatureReason';
 2768 type lpfunc = function(pctx : Pointer; index : LongWord; const reason : PWideChar) : BOOL; stdcall;
 2769 var func : lpfunc;
 2770 begin
 2771    ensureLibraryLoaded(_func);
 2772 
 2773    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2774 
 2775    freeLastError;
 2776    func := lpfunc(GetProc('litePDF_SetSignatureReason'));
 2777 
 2778    ThrowLastErrorIfFail(func(context, index, PWideChar(reason)), self, _func);
 2779 end;
 2780 
 2781 function TLitePDF.GetSignatureReason(index : LongWord) : WideString;
 2782 const _func = 'TLitePDF.GetSignatureReason';
 2783 type lpfunc = function(pctx : Pointer; index : LongWord; value : PWideChar; valueLength : PLongWord) : BOOL; stdcall;
 2784 var func : lpfunc;
 2785    valueLength : LongWord;
 2786    buff : PWideChar;
 2787 begin
 2788    ensureLibraryLoaded(_func);
 2789 
 2790    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2791 
 2792    freeLastError;
 2793    func := lpfunc(GetProc('litePDF_GetSignatureReason'));
 2794 
 2795    valueLength := 0;
 2796    ThrowLastErrorIfFail(func(context, index, nil, @valueLength), self, _func);
 2797 
 2798    Inc(valueLength);
 2799 
 2800    buff := AllocMem(SizeOf(WideChar) * (valueLength));
 2801    ThrowMessageIfFail(buff <> nil, 'Out of memory!', _func);
 2802 
 2803    if func(context, index, buff, @valueLength) then
 2804    begin
 2805       buff[valueLength] := WideChar(0);
 2806       Result := buff;
 2807       FreeMem (buff);
 2808    end
 2809    else
 2810    begin
 2811       FreeMem (buff);
 2812 
 2813       // always false
 2814       ThrowLastErrorIfFail(buff <> nil, self, _func);
 2815    end;
 2816 end;
 2817 
 2818 procedure TLitePDF.SetSignatureLocation(index : LongWord;
 2819                                         const location : WideString);
 2820 const _func = 'TLitePDF.SetSignatureLocation';
 2821 type lpfunc = function(pctx : Pointer; index : LongWord; const location : PWideChar) : BOOL; stdcall;
 2822 var func : lpfunc;
 2823 begin
 2824    ensureLibraryLoaded(_func);
 2825 
 2826    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2827 
 2828    freeLastError;
 2829    func := lpfunc(GetProc('litePDF_SetSignatureLocation'));
 2830 
 2831    ThrowLastErrorIfFail(func(context, index, PWideChar(location)), self, _func);
 2832 end;
 2833 
 2834 function TLitePDF.GetSignatureLocation(index : LongWord) : WideString;
 2835 const _func = 'TLitePDF.GetSignatureLocation';
 2836 type lpfunc = function(pctx : Pointer; index : LongWord; value : PWideChar; valueLength : PLongWord) : BOOL; stdcall;
 2837 var func : lpfunc;
 2838    valueLength : LongWord;
 2839    buff : PWideChar;
 2840 begin
 2841    ensureLibraryLoaded(_func);
 2842 
 2843    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2844 
 2845    freeLastError;
 2846    func := lpfunc(GetProc('litePDF_GetSignatureLocation'));
 2847 
 2848    valueLength := 0;
 2849    ThrowLastErrorIfFail(func(context, index, nil, @valueLength), self, _func);
 2850 
 2851    Inc(valueLength);
 2852 
 2853    buff := AllocMem(SizeOf(WideChar) * (valueLength));
 2854    ThrowMessageIfFail(buff <> nil, 'Out of memory!', _func);
 2855 
 2856    if func(context, index, buff, @valueLength) then
 2857    begin
 2858       buff[valueLength] := WideChar(0);
 2859       Result := buff;
 2860       FreeMem (buff);
 2861    end
 2862    else
 2863    begin
 2864       FreeMem (buff);
 2865 
 2866       // always false
 2867       ThrowLastErrorIfFail(buff <> nil, self, _func);
 2868    end;
 2869 end;
 2870 
 2871 procedure TLitePDF.SetSignatureCreator(index : LongWord;
 2872                                        const creator : AnsiString);
 2873 const _func = 'TLitePDF.SetSignatureCreator';
 2874 type lpfunc = function(pctx : Pointer; index : LongWord; const creator : PAnsiChar) : BOOL; stdcall;
 2875 var func : lpfunc;
 2876 begin
 2877    ensureLibraryLoaded(_func);
 2878 
 2879    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2880 
 2881    freeLastError;
 2882    func := lpfunc(GetProc('litePDF_SetSignatureCreator'));
 2883 
 2884    ThrowLastErrorIfFail(func(context, index, PAnsiChar(creator)), self, _func);
 2885 end;
 2886 
 2887 function TLitePDF.GetSignatureCreator(index : LongWord) : AnsiString;
 2888 const _func = 'TLitePDF.GetSignatureCreator';
 2889 type lpfunc = function(pctx : Pointer; index : LongWord; value : PAnsiChar; valueLength : PLongWord) : BOOL; stdcall;
 2890 var func : lpfunc;
 2891    valueLength : LongWord;
 2892    buff : PAnsiChar;
 2893 begin
 2894    ensureLibraryLoaded(_func);
 2895 
 2896    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2897 
 2898    freeLastError;
 2899    func := lpfunc(GetProc('litePDF_GetSignatureCreator'));
 2900 
 2901    valueLength := 0;
 2902    ThrowLastErrorIfFail(func(context, index, nil, @valueLength), self, _func);
 2903 
 2904    Inc(valueLength);
 2905 
 2906    buff := AllocMem(SizeOf(AnsiChar) * (valueLength));
 2907    ThrowMessageIfFail(buff <> nil, 'Out of memory!', _func);
 2908 
 2909    if func(context, index, buff, @valueLength) then
 2910    begin
 2911       buff[valueLength] := AnsiChar(0);
 2912       Result := buff;
 2913       FreeMem (buff);
 2914    end
 2915    else
 2916    begin
 2917       FreeMem (buff);
 2918 
 2919       // always false
 2920       ThrowLastErrorIfFail(buff <> nil, self, _func);
 2921    end;
 2922 end;
 2923 
 2924 procedure TLitePDF.SetSignatureAppearance(index : LongWord;
 2925                                           appearanceType : TLitePDFAppearance;
 2926                                           resourceID : LongWord;
 2927                                           offsetX_u : Integer;
 2928                                           offsetY_u : Integer);
 2929 const _func = 'TLitePDF.SetSignatureAppearance';
 2930 type lpfunc = function(pctx : Pointer;
 2931                        index : LongWord;
 2932                        appearanceType : LongWord;
 2933                        resourceID : LongWord;
 2934                        offsetX_u : Integer;
 2935                        offsetY_u : Integer) : BOOL; stdcall;
 2936 var func : lpfunc;
 2937 begin
 2938    ensureLibraryLoaded(_func);
 2939 
 2940    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2941 
 2942    freeLastError;
 2943    func := lpfunc(GetProc('litePDF_SetSignatureAppearance'));
 2944 
 2945    ThrowLastErrorIfFail(func(context, index, LongWord(appearanceType), resourceID, offsetX_u, offsetY_u), self, _func);
 2946 end;
 2947 
 2948 procedure TLitePDF.SetSignatureSize(requestBytes : LongWord);
 2949 const _func = 'TLitePDF.SetSignatureSize';
 2950 type lpfunc = function(pctx : Pointer; requestBytes : LongWord) : BOOL; stdcall;
 2951 var func : lpfunc;
 2952 begin
 2953    ensureLibraryLoaded(_func);
 2954 
 2955    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2956 
 2957    freeLastError;
 2958    func := lpfunc(GetProc('litePDF_SetSignatureSize'));
 2959 
 2960    ThrowLastErrorIfFail(func(context, requestBytes), self, _func);
 2961 end;
 2962 
 2963 procedure TLitePDF.AddSignerPFX(pfxData : PByte;
 2964                                 pfxDataLength : LongWord;
 2965                                 const pfxPassword : AnsiString);
 2966 const _func = 'TLitePDF.AddSignerPFX';
 2967 type lpfunc = function(pctx : Pointer;
 2968                        const pfxData : PByte;
 2969                        pfxDataLength : LongWord;
 2970                        const pfxPassword : PAnsiChar) : BOOL; stdcall;
 2971 var func : lpfunc;
 2972 begin
 2973    ensureLibraryLoaded(_func);
 2974 
 2975    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 2976 
 2977    freeLastError;
 2978    func := lpfunc(GetProc('litePDF_AddSignerPFX'));
 2979 
 2980    ThrowLastErrorIfFail(func(context, pfxData, pfxDataLength, PAnsiChar(pfxPassword)), self, _func);
 2981 end;
 2982 
 2983 procedure TLitePDF.AddSignerPEM(pemData : PByte;
 2984                                 pemDataLength : LongWord;
 2985                                 pkeyData : PByte;
 2986                                 pkeyDataLength : LongWord;
 2987                                 const pkeyPassword : AnsiString);
 2988 const _func = 'TLitePDF.AddSignerPEM';
 2989 type lpfunc = function(pctx : Pointer;
 2990                        const pemData : PByte;
 2991                        pemDataLength : LongWord;
 2992                        const pkeyData : PByte;
 2993                        pkeyDataLength : LongWord;
 2994                        const pkeyPassword : PAnsiChar) : BOOL; stdcall;
 2995 var func : lpfunc;
 2996 begin
 2997    ensureLibraryLoaded(_func);
 2998 
 2999    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3000 
 3001    freeLastError;
 3002    func := lpfunc(GetProc('litePDF_AddSignerPEM'));
 3003 
 3004    ThrowLastErrorIfFail(func(context,
 3005       pemData,
 3006       pemDataLength,
 3007       pkeyData,
 3008       pkeyDataLength,
 3009       PAnsiChar(pkeyPassword)), self, _func);
 3010 end;
 3011 
 3012 procedure TLitePDF.SaveToFileWithSign(const fileName : AnsiString;
 3013                                       signatureIndex : LongWord);
 3014 const _func = 'TLitePDF.SaveToFileWithSign';
 3015 type lpfunc = function(pctx : Pointer; const fileName : PAnsiChar; signatureIndex : LongWord) : BOOL; stdcall;
 3016 var func : lpfunc;
 3017 begin
 3018    ensureLibraryLoaded(_func);
 3019 
 3020    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3021 
 3022    freeLastError;
 3023    func := lpfunc(GetProc('litePDF_SaveToFileWithSign'));
 3024 
 3025    ThrowLastErrorIfFail(func(context, PAnsiChar(fileName), signatureIndex), self, _func);
 3026 end;
 3027 
 3028 procedure TLitePDF.SaveToFileWithSignW(const fileName : WideString;
 3029                                        signatureIndex : LongWord);
 3030 const _func = 'TLitePDF.SaveToFileWithSignW';
 3031 type lpfunc = function(pctx : Pointer; const fileName : PWideChar; signatureIndex : LongWord) : BOOL; stdcall;
 3032 var func : lpfunc;
 3033 begin
 3034    ensureLibraryLoaded(_func);
 3035 
 3036    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3037 
 3038    freeLastError;
 3039    func := lpfunc(GetProc('litePDF_SaveToFileWithSignW'));
 3040 
 3041    ThrowLastErrorIfFail(func(context, PWideChar(fileName), signatureIndex), self, _func);
 3042 end;
 3043 
 3044 function TLitePDF.SaveToDataWithSign(signatureIndex : LongWord;
 3045                                      data : PByte;
 3046                                      var dataLength : LongWord) : Boolean;
 3047 const _func = 'TLitePDF.SaveToDataWithSign';
 3048 type lpfunc = function(pctx : Pointer;
 3049                        signatureIndex : LongWord;
 3050                        data : PByte;
 3051                        dataLength : PLongWord) : BOOL; stdcall;
 3052 var func : lpfunc;
 3053    succeeded : BOOL;
 3054 begin
 3055    ensureLibraryLoaded(_func);
 3056 
 3057    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3058 
 3059    freeLastError;
 3060    func := lpfunc(GetProc('litePDF_SaveToDataWithSign'));
 3061 
 3062    succeeded := func(context, signatureIndex, data, @dataLength);
 3063 
 3064    if succeeded then
 3065       Result := True
 3066    else
 3067       Result := False;
 3068 end;
 3069 
 3070 procedure TLitePDF.SaveToFileWithSignManual(const fileName : AnsiString;
 3071                                             signatureIndex : LongWord;
 3072                                             appendSignatureData : TLitePDFAppendSignatureDataFunc;
 3073                                             append_user_data : Pointer;
 3074                                             finishSignature : TLitePDFFinishSignatureFunc;
 3075                                             finish_user_data : Pointer);
 3076 const _func = 'TLitePDF.SaveToFileWithSignManual';
 3077 type lpfunc = function(pctx : Pointer;
 3078                        const fileName : PAnsiChar;
 3079                        signatureIndex : LongWord;
 3080                        appendSignatureData : TLitePDFAppendSignatureDataFunc;
 3081                        append_user_data : Pointer;
 3082                        finishSignature : TLitePDFFinishSignatureFunc;
 3083                        finish_user_data : Pointer) : BOOL; stdcall;
 3084 var func : lpfunc;
 3085 begin
 3086    ensureLibraryLoaded(_func);
 3087 
 3088    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3089    ThrowIfFail(Assigned(appendSignatureData), 'Assigned(appendSignatureData)', _func);
 3090    ThrowIfFail(Assigned(finishSignature), 'Assigned(finishSignature)', _func);
 3091 
 3092    freeLastError;
 3093    func := lpfunc(GetProc('litePDF_SaveToFileWithSignManual'));
 3094 
 3095    ThrowLastErrorIfFail(func(context,
 3096                PAnsiChar(fileName),
 3097                signatureIndex,
 3098                appendSignatureData,
 3099                append_user_data,
 3100                finishSignature,
 3101                finish_user_data), self, _func);
 3102 end;
 3103 
 3104 procedure TLitePDF.SaveToFileWithSignManualW(const fileName : WideString;
 3105                                              signatureIndex : LongWord;
 3106                                              appendSignatureData : TLitePDFAppendSignatureDataFunc;
 3107                                              append_user_data : Pointer;
 3108                                              finishSignature : TLitePDFFinishSignatureFunc;
 3109                                              finish_user_data : Pointer);
 3110 const _func = 'TLitePDF.SaveToFileWithSignManualW';
 3111 type lpfunc = function(pctx : Pointer;
 3112                        const fileName : PWideChar;
 3113                        signatureIndex : LongWord;
 3114                        appendSignatureData : TLitePDFAppendSignatureDataFunc;
 3115                        append_user_data : Pointer;
 3116                        finishSignature : TLitePDFFinishSignatureFunc;
 3117                        finish_user_data : Pointer) : BOOL; stdcall;
 3118 var func : lpfunc;
 3119 begin
 3120    ensureLibraryLoaded(_func);
 3121 
 3122    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3123    ThrowIfFail(Assigned(appendSignatureData), 'Assigned(appendSignatureData)', _func);
 3124    ThrowIfFail(Assigned(finishSignature), 'Assigned(finishSignature)', _func);
 3125 
 3126    freeLastError;
 3127    func := lpfunc(GetProc('litePDF_SaveToFileWithSignManualW'));
 3128 
 3129    ThrowLastErrorIfFail(func(context,
 3130                PWideChar(fileName),
 3131                signatureIndex,
 3132                appendSignatureData,
 3133                append_user_data,
 3134                finishSignature,
 3135                finish_user_data), self, _func);
 3136 end;
 3137 
 3138 function TLitePDF.SaveToDataWithSignManual(signatureIndex : LongWord;
 3139                                            appendSignatureData : TLitePDFAppendSignatureDataFunc;
 3140                                            append_user_data : Pointer;
 3141                                            finishSignature : TLitePDFFinishSignatureFunc;
 3142                                            finish_user_data : Pointer;
 3143                                            data : PByte;
 3144                                            var dataLength : LongWord) : Boolean;
 3145 const _func = 'TLitePDF.SaveToDataWithSignManual';
 3146 type lpfunc = function(pctx : Pointer;
 3147                        signatureIndex : LongWord;
 3148                        appendSignatureData : TLitePDFAppendSignatureDataFunc;
 3149                        append_user_data : Pointer;
 3150                        finishSignature : TLitePDFFinishSignatureFunc;
 3151                        finish_user_data : Pointer;
 3152                        data : PByte;
 3153                        dataLength : PLongWord) : BOOL; stdcall;
 3154 var func : lpfunc;
 3155    succeeded : BOOL;
 3156 begin
 3157    ensureLibraryLoaded(_func);
 3158 
 3159    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3160    ThrowIfFail(Assigned(appendSignatureData), 'Assigned(appendSignatureData)', _func);
 3161    ThrowIfFail(Assigned(finishSignature), 'Assigned(finishSignature)', _func);
 3162 
 3163    freeLastError;
 3164    func := lpfunc(GetProc('litePDF_SaveToDataWithSignManual'));
 3165 
 3166    succeeded := func(context,
 3167                signatureIndex,
 3168                appendSignatureData,
 3169                append_user_data,
 3170                finishSignature,
 3171                finish_user_data,
 3172                data,
 3173                @dataLength);
 3174 
 3175    if succeeded then
 3176       Result := True
 3177    else
 3178       Result := False;
 3179 end;
 3180 
 3181 procedure TLitePDF.EmbedFile(const fileName : AnsiString);
 3182 const _func = 'TLitePDF.EmbedFile';
 3183 type lpfunc = function(pctx : Pointer; const fileName : PAnsiChar) : BOOL; stdcall;
 3184 var func : lpfunc;
 3185 begin
 3186    ensureLibraryLoaded(_func);
 3187 
 3188    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3189 
 3190    freeLastError;
 3191    func := lpfunc(GetProc('litePDF_EmbedFile'));
 3192 
 3193    ThrowLastErrorIfFail(func(context, PAnsiChar(fileName)), self, _func);
 3194 end;
 3195 
 3196 procedure TLitePDF.EmbedFileW(const fileName : WideString);
 3197 const _func = 'TLitePDF.EmbedFileW';
 3198 type lpfunc = function(pctx : Pointer; const fileName : PWideChar) : BOOL; stdcall;
 3199 var func : lpfunc;
 3200 begin
 3201    ensureLibraryLoaded(_func);
 3202 
 3203    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3204 
 3205    freeLastError;
 3206    func := lpfunc(GetProc('litePDF_EmbedFileW'));
 3207 
 3208    ThrowLastErrorIfFail(func(context, PWideChar(fileName)), self, _func);
 3209 end;
 3210 
 3211 procedure TLitePDF.EmbedData(const fileName : AnsiString;
 3212                              data : PByte;
 3213                              dataLength : LongWord);
 3214 const _func = 'TLitePDF.EmbedData';
 3215 type lpfunc = function(pctx : Pointer; const fileName : PAnsiChar; const data : PByte; dataLength : LongWord) : BOOL; stdcall;
 3216 var func : lpfunc;
 3217 begin
 3218    ensureLibraryLoaded(_func);
 3219 
 3220    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3221    ThrowIfFail(data <> nil, 'data <> nil', _func);
 3222 
 3223    freeLastError;
 3224    func := lpfunc(GetProc('litePDF_EmbedData'));
 3225 
 3226    ThrowLastErrorIfFail(func(context, PAnsiChar(fileName), data, dataLength), self, _func);
 3227 end;
 3228 
 3229 procedure TLitePDF.EmbedDataW(const fileName : WideString;
 3230                               data : PByte;
 3231                               dataLength : LongWord);
 3232 const _func = 'TLitePDF.EmbedDataW';
 3233 type lpfunc = function(pctx : Pointer; const fileName : PWideChar; const data : PByte; dataLength : LongWord) : BOOL; stdcall;
 3234 var func : lpfunc;
 3235 begin
 3236    ensureLibraryLoaded(_func);
 3237 
 3238    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3239    ThrowIfFail(data <> nil, 'data <> nil', _func);
 3240 
 3241    freeLastError;
 3242    func := lpfunc(GetProc('litePDF_EmbedDataW'));
 3243 
 3244    ThrowLastErrorIfFail(func(context, PWideChar(fileName), data, dataLength), self, _func);
 3245 end;
 3246 
 3247 function TLitePDF.GetEmbeddedFileCount : Integer;
 3248 const _func = 'TLitePDF.GetEmbeddedFileCount';
 3249 type lpfunc = function(pctx : Pointer) : Integer; stdcall;
 3250 var func : lpfunc;
 3251 begin
 3252    ensureLibraryLoaded(_func);
 3253 
 3254    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3255 
 3256    freeLastError;
 3257    func := lpfunc(GetProc('litePDF_GetEmbeddedFileCount'));
 3258 
 3259    Result := func(context);
 3260 end;
 3261 
 3262 function TLitePDF.GetEmbeddedFileName(index : LongWord) : AnsiString;
 3263 const _func = 'TLitePDF.GetEmbeddedFileName';
 3264 type lpfunc = function(pctx : Pointer; index : LongWord; fileName : PAnsiChar; fileNameLength : PLongWord) : BOOL; stdcall;
 3265 var func : lpfunc;
 3266    fileNameLength : LongWord;
 3267    buff : PAnsiChar;
 3268 begin
 3269    ensureLibraryLoaded(_func);
 3270 
 3271    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3272 
 3273    freeLastError;
 3274    func := lpfunc(GetProc('litePDF_GetEmbeddedFileName'));
 3275 
 3276    fileNameLength := 0;
 3277    ThrowLastErrorIfFail(func(context, index, nil, @fileNameLength), self, _func);
 3278 
 3279    Inc(fileNameLength);
 3280 
 3281    buff := AllocMem(SizeOf(AnsiChar) * (fileNameLength));
 3282    ThrowMessageIfFail(buff <> nil, 'Out of memory!', _func);
 3283 
 3284    if func(context, index, buff, @fileNameLength) then
 3285    begin
 3286       buff[fileNameLength] := Char(0);
 3287       Result := buff;
 3288       FreeMem (buff);
 3289    end
 3290    else
 3291    begin
 3292       FreeMem (buff);
 3293 
 3294       // always false
 3295       ThrowLastErrorIfFail(buff <> nil, self, _func);
 3296    end;
 3297 end;
 3298 
 3299 function TLitePDF.GetEmbeddedFileNameW(index : LongWord) : WideString;
 3300 const _func = 'TLitePDF.GetEmbeddedFileNameW';
 3301 type lpfunc = function(pctx : Pointer; index : LongWord; fileName : PWideChar; fileNameLength : PLongWord) : BOOL; stdcall;
 3302 var func : lpfunc;
 3303    fileNameLength : LongWord;
 3304    buff : PWideChar;
 3305 begin
 3306    ensureLibraryLoaded(_func);
 3307 
 3308    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3309 
 3310    freeLastError;
 3311    func := lpfunc(GetProc('litePDF_GetEmbeddedFileNameW'));
 3312 
 3313    fileNameLength := 0;
 3314    ThrowLastErrorIfFail(func(context, index, nil, @fileNameLength), self, _func);
 3315 
 3316    Inc(fileNameLength);
 3317 
 3318    buff := AllocMem(SizeOf(WideChar) * (fileNameLength));
 3319    ThrowMessageIfFail(buff <> nil, 'Out of memory!', _func);
 3320 
 3321    if func(context, index, buff, @fileNameLength) then
 3322    begin
 3323       buff[fileNameLength] := WideChar(0);
 3324       Result := buff;
 3325       FreeMem (buff);
 3326    end
 3327    else
 3328    begin
 3329       FreeMem (buff);
 3330 
 3331       // always false
 3332       ThrowLastErrorIfFail(buff <> nil, self, _func);
 3333    end;
 3334 end;
 3335 
 3336 function TLitePDF.GetEmbeddedFileData(index : LongWord;
 3337                                       data : PByte;
 3338                                       var dataLength : LongWord) : Boolean;
 3339 const _func = 'TLitePDF.GetEmbeddedFileData';
 3340 type lpfunc = function(pctx : Pointer; index : LongWord; data : PByte; dataLength : PLongWord) : BOOL; stdcall;
 3341 var func : lpfunc;
 3342 begin
 3343    ensureLibraryLoaded(_func);
 3344 
 3345    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3346 
 3347    freeLastError;
 3348    func := lpfunc(GetProc('litePDF_GetEmbeddedFileData'));
 3349 
 3350    if func(context, index, data, @dataLength) then
 3351       Result := True
 3352    else
 3353       Result := False;
 3354 end;
 3355 
 3356 function TLitePDF.GetPoDoFoDocument : Pointer;
 3357 const _func = 'TLitePDF.GetPoDoFoDocument';
 3358 type lpfunc = function(pctx : Pointer) : Pointer; stdcall;
 3359 var func : lpfunc;
 3360 begin
 3361    ensureLibraryLoaded(_func);
 3362 
 3363    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3364 
 3365    freeLastError;
 3366    func := lpfunc(GetProc('litePDF_GetPoDoFoDocument'));
 3367 
 3368    Result := func(context);
 3369 
 3370    ThrowLastErrorIfFail(Result <> nil, self, _func);
 3371 end;
 3372 
 3373 procedure TLitePDF.DrawDebugPage(const filename : AnsiString);
 3374 const _func = 'TLitePDF.DrawDebugPage';
 3375 type lpfunc = function(pctx : Pointer; const filename : PAnsiChar) : BOOL; stdcall;
 3376 var func : lpfunc;
 3377 begin
 3378    ensureLibraryLoaded(_func);
 3379 
 3380    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3381 
 3382    freeLastError;
 3383    func := lpfunc(GetProc('litePDF_DrawDebugPage'));
 3384 
 3385    ThrowLastErrorIfFail(func(context, PAnsiChar(filename)), self, _func);
 3386 end;
 3387 
 3388 procedure TLitePDF.CreateLinkAnnotation(annotationPageIndex : LongWord;
 3389                                         annotationPosition_u : TRect;
 3390                                         annotationFlags : LongWord;
 3391                                         annotationResourceID : LongWord;
 3392                                         destinationPageIndex : LongWord;
 3393                                         destinationX_u : LongWord;
 3394                                         destinationY_u : LongWord;
 3395                                         const destinationDescription : WideString);
 3396 const _func = 'TLitePDF.CreateLinkAnnotation';
 3397 type lpfunc = function(pctx : Pointer;
 3398                        annotationPageIndex : LongWord;
 3399                        annotationX_u : Integer;
 3400                        annotationY_u : Integer;
 3401                        annotationWidth_u : Integer;
 3402                        annotationHeight_u : Integer;
 3403                        annotationFlags : LongWord;
 3404                        annotationResourceID : LongWord;
 3405                        destinationPageIndex : LongWord;
 3406                        destinationX_u : LongWord;
 3407                        destinationY_u : LongWord;
 3408                        const destinationDescription : PWideChar) : BOOL; stdcall;
 3409 var func : lpfunc;
 3410 begin
 3411    ensureLibraryLoaded(_func);
 3412 
 3413    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3414 
 3415    freeLastError;
 3416    func := lpfunc(GetProc('litePDF_CreateLinkAnnotation'));
 3417 
 3418    ThrowLastErrorIfFail(func(context,
 3419                              annotationPageIndex,
 3420                              annotationPosition_u.left,
 3421                              annotationPosition_u.top,
 3422                              annotationPosition_u.right - annotationPosition_u.left,
 3423                              annotationPosition_u.bottom - annotationPosition_u.top,
 3424                              annotationFlags,
 3425                              annotationResourceID,
 3426                              destinationPageIndex,
 3427                              destinationX_u,
 3428                              destinationY_u,
 3429                              PWideChar(destinationDescription)), self, _func);
 3430 end;
 3431 
 3432 function TLitePDF.CreateBookmarkRoot(const title : WideString;
 3433                                      flags : LongWord;
 3434                                      titleColor : TColor;
 3435                                      destinationPageIndex : LongWord;
 3436                                      destinationX_u : LongWord;
 3437                                      destinationY_u : LongWord) : LongWord;
 3438 const _func = 'TLitePDF.CreateBookmarkRoot';
 3439 type lpfunc = function(pctx : Pointer;
 3440                        const title : PWideChar;
 3441                        flags : LongWord;
 3442                        titleColor_red : Byte;
 3443                        titleColor_green : Byte;
 3444                        titleColor_blue : Byte;
 3445                        destinationPageIndex : LongWord;
 3446                        destinationX_u : LongWord;
 3447                        destinationY_u : LongWord) : LongWord; stdcall;
 3448 var func : lpfunc;
 3449     rgb : Integer;
 3450 begin
 3451    ensureLibraryLoaded(_func);
 3452 
 3453    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3454 
 3455    freeLastError;
 3456    func := lpfunc(GetProc('litePDF_CreateBookmarkRoot'));
 3457 
 3458    rgb := ColorToRGB(titleColor);
 3459 
 3460    Result := func(context,
 3461                   PWideChar(title),
 3462                   flags,
 3463                   GetRValue(rgb),
 3464                   GetGValue(rgb),
 3465                   GetBValue(rgb),
 3466                   destinationPageIndex,
 3467                   destinationX_u,
 3468                   destinationY_u);
 3469    ThrowLastErrorIfFail(Result <> 0, self, _func);
 3470 end;
 3471 
 3472 function TLitePDF.CreateBookmarkChild(parentBookmarkID : LongWord;
 3473                                       const title : WideString;
 3474                                       flags : LongWord;
 3475                                       titleColor : TColor;
 3476                                       destinationPageIndex : LongWord;
 3477                                       destinationX_u : LongWord;
 3478                                       destinationY_u : LongWord) : LongWord;
 3479 const _func = 'TLitePDF.CreateBookmarkChild';
 3480 type lpfunc = function(pctx : Pointer;
 3481                        parentBookmarkID : LongWord;
 3482                        const title : PWideChar;
 3483                        flags : LongWord;
 3484                        titleColor_red : Byte;
 3485                        titleColor_green : Byte;
 3486                        titleColor_blue : Byte;
 3487                        destinationPageIndex : LongWord;
 3488                        destinationX_u : LongWord;
 3489                        destinationY_u : LongWord) : LongWord; stdcall;
 3490 var func : lpfunc;
 3491     rgb : Integer;
 3492 begin
 3493    ensureLibraryLoaded(_func);
 3494 
 3495    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3496 
 3497    freeLastError;
 3498    func := lpfunc(GetProc('litePDF_CreateBookmarkChild'));
 3499 
 3500    rgb := ColorToRGB(titleColor);
 3501 
 3502    Result := func(context,
 3503                   parentBookmarkID,
 3504                   PWideChar(title),
 3505                   flags,
 3506                   GetRValue(rgb),
 3507                   GetGValue(rgb),
 3508                   GetBValue(rgb),
 3509                   destinationPageIndex,
 3510                   destinationX_u,
 3511                   destinationY_u);
 3512    ThrowLastErrorIfFail(Result <> 0, self, _func);
 3513 end;
 3514 
 3515 function TLitePDF.CreateBookmarkSibling(previousBookmarkID: LongWord;
 3516                                         const title : WideString;
 3517                                         flags : LongWord;
 3518                                         titleColor : TColor;
 3519                                         destinationPageIndex : LongWord;
 3520                                         destinationX_u : LongWord;
 3521                                         destinationY_u : LongWord) : LongWord;
 3522 const _func = 'TLitePDF.CreateBookmarkSibling';
 3523 type lpfunc = function(pctx : Pointer;
 3524                        previousBookmarkID : LongWord;
 3525                        const title : PWideChar;
 3526                        flags : LongWord;
 3527                        titleColor_red : Byte;
 3528                        titleColor_green : Byte;
 3529                        titleColor_blue : Byte;
 3530                        destinationPageIndex : LongWord;
 3531                        destinationX_u : LongWord;
 3532                        destinationY_u : LongWord) : LongWord; stdcall;
 3533 var func : lpfunc;
 3534     rgb : Integer;
 3535 begin
 3536    ensureLibraryLoaded(_func);
 3537 
 3538    ThrowIfFail(lib <> THandle(0), 'lib <> THandle(0)', _func);
 3539 
 3540    freeLastError;
 3541    func := lpfunc(GetProc('litePDF_CreateBookmarkSibling'));
 3542 
 3543    rgb := ColorToRGB(titleColor);
 3544 
 3545    Result := func(context,
 3546                   previousBookmarkID,
 3547                   PWideChar(title),
 3548                   flags,
 3549                   GetRValue(rgb),
 3550                   GetGValue(rgb),
 3551                   GetBValue(rgb),
 3552                   destinationPageIndex,
 3553                   destinationX_u,
 3554                   destinationY_u);
 3555    ThrowLastErrorIfFail(Result <> 0, self, _func);
 3556 end;
 3557 
 3558 //----------------------------------------------------------------------------
 3559 
 3560 end.
 3561 
TOPlist