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