This repository was archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUberPDFExample.Examples.HelloWorld.pas
More file actions
331 lines (319 loc) · 17.6 KB
/
UberPDFExample.Examples.HelloWorld.pas
File metadata and controls
331 lines (319 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
unit UberPDFExample.Examples.HelloWorld;
{$mode objfpc}{$H+}
interface
//------------------------------------------------------------------------------
// include the file to let uber figure out what compiler and version is used
//------------------------------------------------------------------------------
{$INCLUDE 'uber_base_unit_compiler_version_defs.pascal-inc'}
//------------------------------------------------------------------------------
// AFTER THE "USES" CLAUSE BELOW YOU MUST DEFINE THE PATH TO THE UBER_PDFSDK_SO
// using the const UBER_PDFSDK_SO_NAME = 'PATH_TO_UBER_PDFSDK_DYN_LIB';
// (see below).
//------------------------------------------------------------------------------
uses
Classes, SysUtils, LCLIntF,
UberPDFExample.Common,
{$IFDEF UBER_PDFSDK_USE_DYNAMIC_LIB_LOADER}
libuberpdfsdkdyn_pascal;
{$ELSE}
uberpdfsdk_pascal;
{$ENDIF}
//------------------------------------------------------------------------------
// Include the Uber base types
//------------------------------------------------------------------------------
{$INCLUDE 'uber_base_unit_defs.pascal-inc'}
//------------------------------------------------------------------------------
procedure CreatePdfDocument;
function CreatePdfPage(UberPdfSdkInstanceH: UBERPDFSDK_INSTANCE_HANDLE): UBERPDFSDK_PDF_PAGE_HANDLE;
procedure AddUberPDFFancyTextLine(var ContentString : AnsiString);
implementation
uses
UberPDFExample.Forms.Main;
procedure CreatePdfDocument;
var
UberPdfSdkSystemInitStruct : UBERPDFSDK_SYSTEM_INIT_STRUCT;
UberPdfSdkInstanceH : UBERPDFSDK_INSTANCE_HANDLE;
PageHandle : UBERPDFSDK_PDF_PAGE_HANDLE;
FileName : AnsiString;
begin
if (not(IsUberPDFSdkLoaded())) then begin
frmMain.Log('UberPDFSdk is not loaded - Ex_HelloWorld_CreatePdfDocument() cannot proceeed');
exit;
end;
//----------------------------------------------------------------------------
// Create an UberPdfSdk instance
//----------------------------------------------------------------------------
frmMain.Log('Example Hello World: Initialising System');
FillChar(UberPdfSdkSystemInitStruct,
sizeof(UberPdfSdkSystemInitStruct),
0);
UberPdfSdkSystemInitStruct.StructSize := sizeof(UberPdfSdkSystemInitStruct);
UberPdfSdkSystemInitStruct.StructVersion := 1;
UberPdfSdkInstanceH := UberPdfSdk_System_Init(@UberPdfSdkSystemInitStruct);
if (UberPdfSdkInstanceH = UBERPDFSDK_INSTANCE_HANDLE_NULL) then begin
frmMain.Log('Example Hello World: UberPdfSdk_System_Init() failed');
exit;
end;
//----------------------------------------------------------------------------
// Create an empty Pdf document
//----------------------------------------------------------------------------
frmMain.Log('Example Hello World: Creating an empty PDF Document');
if (UberPdfSdk_Pdf_Doc_CreateEmpty(UberPdfSdkInstanceH) <> UBER_STATUS_OK) then begin
frmMain.Log('Example Hello World: UberPdfSdk_Pdf_Doc_CreateEmpty() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
// Create a page
//----------------------------------------------------------------------------
frmMain.Log('Example Hello World: Creating a page');
PageHandle := CreatePdfPage(UberPdfSdkInstanceH);
if (PageHandle = UBER_HANDLE_NULL) then begin
frmMain.Log('Example Hello World: CreatePdfPage failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
// Insert the page into the beginning of the document
//----------------------------------------------------------------------------
frmMain.Log('Example Hello World: Inserting the page into the beginning of the document');
if (UberPdfSdk_Pdf_Doc_Pages_InsertPage(UberPdfSdkInstanceH,
PageHandle,
1) <> UBER_STATUS_OK) then begin
frmMain.Log('Example Hello World: UberPdfSdk_Pdf_Doc_Pages_InsertPage() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
// Free the page
//----------------------------------------------------------------------------
frmMain.Log('Example Hello World: Free-ing the page');
if (UberPdfSdk_Pdf_Page_Free(PageHandle) <> UBER_HANDLE_NULL) then begin
frmMain.Log('Example Hello World: UberPdfSdk_Pdf_Page_Free() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
// Save the document
//----------------------------------------------------------------------------
FileName := 'hello-world.pdf';
frmMain.Log('Example Hello World: Saving the document "'+FileName+'"');
if (UberPdfSdk_Pdf_Doc_SaveToFileA(UberPdfSdkInstanceH,
pAnsiChar(FileName),
Length(FileName),
nil, // lpOwnerPasswordA
0, // cchlpOwnerPasswordBufferSize
nil, // lpUserPasswordA
0, // cchlpUserPasswordBufferSize
nil, // lpPdfOptionalMinVersionStrA
0, // cchlpPdfOptionalMinVersionStrBufferSize
nil, // lpPdfOptionalForceVersionStrA
0, // cchlpPdfOptionalForceVersionStrBufferSize
nil, // lpPdfOptionalHeaderExtraStrA
0, // cchlpPdfOptionalHeaderExtraStrBufferSize
UBERPDFSDK_PDF_DOC_SAVE_FLAGS_USE_DEFAULT,
UBERPDFSDK_PDF_DOC_SAVE_FLAGS_ENCRYPT_USE_NONE) <> UBER_STATUS_OK) then begin
frmMain.Log('Example Hello World: UberPdfSdk_Pdf_Doc_SaveToFileA() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
frmMain.Log('Example Hello World: Opening the document with the system default viewer');
OpenDocument(FileName);
//----------------------------------------------------------------------------
// Free the UberPdfSdk instance
//----------------------------------------------------------------------------
frmMain.Log('Example Hello World: Free-ing the UberPdfSdk instance');
if (UberPdfSdk_System_Free(UberPdfSdkInstanceH) <> UBERPDFSDK_INSTANCE_HANDLE_NULL) then begin
frmMain.Log('Example Hello World: UberUberPdfSdk_System_Free() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
frmMain.Log('Example Hello World: CreatePdfDocument() succeeded');
frmMain.Log('');
end;
function CreatePdfPage(UberPdfSdkInstanceH: UBERPDFSDK_INSTANCE_HANDLE
): UBERPDFSDK_PDF_PAGE_HANDLE;
var
PageHandle : UBERPDFSDK_PDF_PAGE_HANDLE;
MediaWidth :UBER_FLOAT;
MediaHeight : UBER_FLOAT;
MediaRotation : UBER_INT;
ResourceDictHandle : UBERPDFSDK_PDF_DICT_HANDLE;
ContentsArrayHandle : UBERPDFSDK_PDF_DICT_HANDLE;
FontHandle : UBERPDFSDK_PDF_FONT_HANDLE;
FontDictHandle : UBERPDFSDK_PDF_DICT_HANDLE;
ContentString : AnsiString;
ContentStreamHandle : UBERPDFSDK_PDF_STREAM_HANDLE;
begin
//----------------------------------------------------------------------------
// Create a new letter sized page in landscape orientation from scratch using
// only minor error checking (for clairity). UberPdfSdk_Pdf_Page_New_Ex() will
// (if successfull) return a ResourceDictHandle to use to add fonts and images
// to, and a ContentsArrayHandle for us to push streams of content to.
//----------------------------------------------------------------------------
MediaWidth := 792; // in points (72 equals one inch)
MediaHeight := 612; // in points (72 equals one inch)
MediaRotation := 0; // 0, 90, 180, 270
PageHandle := UberPdfSdk_Pdf_Page_New_Ex(UberPdfSdkInstanceH,
@MediaWidth,
@MediaHeight,
@MediaRotation,
@ResourceDictHandle,
@ContentsArrayHandle);
//----------------------------------------------------------------------------
if (PageHandle = UBER_HANDLE_NULL) then begin
result := UBER_HANDLE_NULL;
exit;
end;
//----------------------------------------------------------------------------
// Create a font entry to hold the fonts we use in the Page's Resource Dict
//----------------------------------------------------------------------------
FontDictHandle := UberPdfSdk_Pdf_Dict_AddKeyNewDict(ResourceDictHandle,
'/Font');
//----------------------------------------------------------------------------
// Create a document wide (available to the whole document) font(Times Roman)
//----------------------------------------------------------------------------
FontHandle := UberPdfSdk_Pdf_Doc_Fonts_Std_New(UberPdfSdkInstanceH,
UBERPDFSDK_PDF_DOC_FONTS_STD_TYPE_TIMES_ROMAN,
UBERPDFSDK_PDF_DOC_FONTS_ENCODING_TYPE_WIN_ANSI_ENCODING,
UBERPDFSDK_PDF_DICT_HANDLE_NULL);
//----------------------------------------------------------------------------
// Add a named reference to the Page's Resource Font Dict and free the font
//----------------------------------------------------------------------------
UberPdfSdk_Pdf_Dict_AddKey(FontDictHandle,
'/FontTimesRoman',
FontHandle);
UberPdfSdk_Pdf_Doc_Fonts_Std_Free(FontHandle);
//----------------------------------------------------------------------------
// Create a document wide (available to the whole document) font (Helvetica Bold)
//----------------------------------------------------------------------------
FontHandle := UberPdfSdk_Pdf_Doc_Fonts_Std_New(UberPdfSdkInstanceH,
UBERPDFSDK_PDF_DOC_FONTS_STD_TYPE_HELVETICA_BOLD,
UBERPDFSDK_PDF_DOC_FONTS_ENCODING_TYPE_WIN_ANSI_ENCODING,
UBERPDFSDK_PDF_DICT_HANDLE_NULL);
//----------------------------------------------------------------------------
// Add a named reference to the Page's Resource Font Dict and free the font
//----------------------------------------------------------------------------
UberPdfSdk_Pdf_Dict_AddKey(FontDictHandle,
'/FontHelveticaBold',
FontHandle);
UberPdfSdk_Pdf_Doc_Fonts_Std_Free(FontHandle);
//----------------------------------------------------------------------------
// Create a document wide (available to the whole document) font (Symbol)
//----------------------------------------------------------------------------
FontHandle := UberPdfSdk_Pdf_Doc_Fonts_Std_New(UberPdfSdkInstanceH,
UBERPDFSDK_PDF_DOC_FONTS_STD_TYPE_SYMBOL,
UBERPDFSDK_PDF_DOC_FONTS_ENCODING_TYPE_BUILT_IN,
UBERPDFSDK_PDF_DICT_HANDLE_NULL);
//----------------------------------------------------------------------------
// Add a named reference to the Page's Resource Font Dict and free the font
//----------------------------------------------------------------------------
UberPdfSdk_Pdf_Dict_AddKey(FontDictHandle,
'/FontSymbol',
FontHandle);
UberPdfSdk_Pdf_Doc_Fonts_Std_Free(FontHandle);
//----------------------------------------------------------------------------
// Start our ContentStream with a 'gsave' (saves the graphic state)
//----------------------------------------------------------------------------
ContentString := 'q ';
//----------------------------------------------------------------------------
// We will output some text - Send the 'Begin Text' sequence
//----------------------------------------------------------------------------
ContentString := ContentString + 'BT ';
//----------------------------------------------------------------------------
// Use our FontTimesRoman with:
// a size of 40 points (72 equals one inch)
// normal font scaling and font rotation
// starting 36 points from page edge left (72 equals one inch)
// starting 540 points from page edge bottom (72 equals one inch)
// advance 48 points of leading if we line break (72 equals one inch)
//----------------------------------------------------------------------------
ContentString := ContentString + '/FontTimesRoman 40 Tf 1 0 0 1 36 540 Tm 48 TL ';
//----------------------------------------------------------------------------
// Set the color to RGB black for non stroking operations (like filled text)
ContentString := ContentString + '0 0 0 rg ';
//----------------------------------------------------------------------------
// Output some text
//----------------------------------------------------------------------------
ContentString := ContentString + '(Hello World) Tj ';
//----------------------------------------------------------------------------
// Add the UberPDF fancy text example with:
// normal font scaling and font rotation
// starting 36 points from page edge left (72 equals one inch)
// starting 36 points from page edge bottom (72 equals one inch)
//----------------------------------------------------------------------------
ContentString := ContentString + '1 0 0 1 36 36 Tm ';
AddUberPDFFancyTextLine(ContentString);
//----------------------------------------------------------------------------
// We have finished outputting text - Send the 'End Text' sequence
//----------------------------------------------------------------------------
ContentString := ContentString + 'ET ';
//----------------------------------------------------------------------------
// Restore the graphic state by sending a 'grestore'
//----------------------------------------------------------------------------
ContentString := ContentString + 'Q'; // we are done (no extra space needed)
//----------------------------------------------------------------------------
// Create a Content stream object from our ContentString
//----------------------------------------------------------------------------
ContentStreamHandle := UberPdfSdk_Pdf_Stream_NewFromString(UberPdfSdkInstanceH,
pAnsiChar(ContentString));
//----------------------------------------------------------------------------
// Push the ContentStream in to the ContentsArray
//----------------------------------------------------------------------------
UberPdfSdk_Pdf_Array_PushItem(ContentsArrayHandle,
ContentStreamHandle);
//----------------------------------------------------------------------------
// Clean up
//----------------------------------------------------------------------------
UberPdfSdk_Pdf_Stream_Free(ContentStreamHandle);
UberPdfSdk_Pdf_Dict_Free(FontDictHandle);
UberPdfSdk_Pdf_Dict_Free(ResourceDictHandle);
UberPdfSdk_Pdf_Array_Free(ContentsArrayHandle);
//----------------------------------------------------------------------------
// return the page handle
//----------------------------------------------------------------------------
result := PageHandle;
//----------------------------------------------------------------------------
end;
procedure AddUberPDFFancyTextLine(var ContentString: AnsiString);
begin
//----------------------------------------------------------------------------
// Output the string 'UberPDF(tm)sdk' at the current positon with:
// Uber in RGB blue (with RockDots)
// PDF(tm)sdk in RGB red
// using special glyph positioning, sizing, and a text rise on the 'tm'
//----------------------------------------------------------------------------
ContentString := ContentString + '/FontHelveticaBold 40 Tf ';
ContentString := ContentString + '0 0 1 rg ';
ContentString := ContentString + '[ ';
ContentString := ContentString + '0 (';
ContentString := ContentString + #220;
ContentString := ContentString + ')';
ContentString := ContentString + '90 (b) ';
ContentString := ContentString + '27 (e) ';
ContentString := ContentString + '40 (r) ';
ContentString := ContentString + '] TJ ';
ContentString := ContentString + '1 0 0 rg ';
ContentString := ContentString + '-1 Ts '; //TextRise
ContentString := ContentString + '[ ';
ContentString := ContentString + '35 (P) ';
ContentString := ContentString + '70 (D) ';
ContentString := ContentString + '70 (F) ';
ContentString := ContentString + '] TJ ';
ContentString := ContentString + '/FontSymbol 20 Tf ';
ContentString := ContentString + '14.125 Ts '; //TextRise
ContentString := ContentString + '[ ';
ContentString := ContentString + '10 (';
ContentString := ContentString + #228;
ContentString := ContentString + ')';
ContentString := ContentString + '] TJ ';
ContentString := ContentString + '-1 Ts '; //TextRise
ContentString := ContentString + '/FontHelveticaBold 30 Tf ';
ContentString := ContentString + '[ ';
ContentString := ContentString + '600 (s) ';
ContentString := ContentString + '50 (d) ';
ContentString := ContentString + '50 (k) ';
ContentString := ContentString + '] TJ ';
end;
end.