This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UberPDFExample.Examples.AddJPEG.pas
257 lines (246 loc) · 13.1 KB
/
UberPDFExample.Examples.AddJPEG.pas
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
unit UberPDFExample.Examples.AddJPEG;
{$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;
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('Example Add JPEG: UberPDFSdk is not loaded - CreatePdfDocument() cannot proceeed');
exit;
end;
//----------------------------------------------------------------------------
// Create an UberPdfSdk instance
//----------------------------------------------------------------------------
frmMain.Log('Example Add JPEG: 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 Add JPEG: UberPdfSdk_System_Init() failed');
exit;
end;
//----------------------------------------------------------------------------
// Create an empty Pdf document
//----------------------------------------------------------------------------
frmMain.Log('Example Add JPEG: Creating an empty PDF Document');
if (UberPdfSdk_Pdf_Doc_CreateEmpty(UberPdfSdkInstanceH) <> UBER_STATUS_OK) then begin
frmMain.Log('Example Add JPEG: UberPdfSdk_Pdf_Doc_CreateEmpty() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
// Create a page by calling the CreatePdfPage() function below
//----------------------------------------------------------------------------
frmMain.Log('Example Add JPEG: Creating a page');
PageHandle := CreatePdfPage(UberPdfSdkInstanceH);
if (PageHandle = UBER_HANDLE_NULL) then begin
frmMain.Log('Example Add JPEG: CreatePdfPage failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
// Insert the page into the beginning of the document
//----------------------------------------------------------------------------
frmMain.Log('Example Add JPEG: 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 Add JPEG: UberPdfSdk_Pdf_Doc_Pages_InsertPage() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
// Free the page
//----------------------------------------------------------------------------
frmMain.Log('Example Add JPEG: Free-ing the page');
if (UberPdfSdk_Pdf_Page_Free(PageHandle) <> UBER_HANDLE_NULL) then begin
frmMain.Log('Example Add JPEG: UberPdfSdk_Pdf_Page_Free() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
// Save the document
//----------------------------------------------------------------------------
FileName := 'add-jpeg.pdf';
frmMain.Log('Example Add JPEG: 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 Add JPEG: UberPdfSdk_Pdf_Doc_SaveToFileA() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
frmMain.Log('Example Add JPEG: Opening the document with the system default viewer');
OpenDocument(FileName);
//----------------------------------------------------------------------------
// Free the UberPdfSdk instance
//----------------------------------------------------------------------------
frmMain.Log('Example Add JPEG: Free-ing the UberPdfSdk instance');
if (UberPdfSdk_System_Free(UberPdfSdkInstanceH) <> UBERPDFSDK_INSTANCE_HANDLE_NULL) then begin
frmMain.Log('Example Add JPEG: UberUberPdfSdk_System_Free() failed');
UberPdfSdk_System_Free(UberPdfSdkInstanceH);
exit;
end;
//----------------------------------------------------------------------------
frmMain.Log('Example Add JPEG: 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;
JpegFileName : AnsiString;
JpegHandle : UBERPDFSDK_PDF_JPEG_HANDLE;
XObjectDictHandle : UBERPDFSDK_PDF_DICT_HANDLE;
ContentString : AnsiString;
ContentStreamHandle : UBERPDFSDK_PDF_STREAM_HANDLE;
begin
//----------------------------------------------------------------------------
// define where the jpeg file is located for this example
//----------------------------------------------------------------------------
{$IFDEF UBER_DEFINED_OS_PERSONA_WINDOWS_LIKE}
JpegFileName := 'uber\3rdpartylibs\_testfiles\testfiles\jpg\uberdude-uberairshare-stage-board-01.jpg';
{$ELSE}
JpegFileName := './uber/3rdpartylibs/_testfiles/testfiles/jpg/uberdude-uberairshare-stage-board-01.jpg';
{$ENDIF}
//----------------------------------------------------------------------------
// 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;
//----------------------------------------------------------------------------
// Add an XObject dict to the Page's Resource Dict to hold image and other resources
//----------------------------------------------------------------------------
XObjectDictHandle := UberPdfSdk_Pdf_Dict_AddKeyNewDict(ResourceDictHandle,
'/XObject');
//----------------------------------------------------------------------------
// Create a document wide (available to the whole document) jpeg
//----------------------------------------------------------------------------
JpegHandle := UberPdfSdk_Pdf_Doc_Images_Jpeg_NewFromFileA(UberPdfSdkInstanceH,
pAnsiChar(JpegFileName),
Length(JpegFileName),
0 // maximum jpeg filesize allowed to prevent explots (zero is no checking)
);
if (JpegHandle = UBER_HANDLE_NULL) then begin
UberPdfSdk_Pdf_Dict_Free(XObjectDictHandle);
UberPdfSdk_Pdf_Dict_Free(ResourceDictHandle);
UberPdfSdk_Pdf_Array_Free(ContentsArrayHandle);
UberPdfSdk_Pdf_Page_Free(PageHandle);
result := UBER_HANDLE_NULL;
exit;
end;
//----------------------------------------------------------------------------
// Add a name entry to the jpeg in the Page's XObject Dict called ImageUberDude
//----------------------------------------------------------------------------
UberPdfSdk_Pdf_Dict_AddKey(XObjectDictHandle,
'/ImageUberDude',
JpegHandle);
//----------------------------------------------------------------------------
// Start our ContentStream with a 'gsave' (saves the graphic state)
//----------------------------------------------------------------------------
ContentString := 'q ';
//----------------------------------------------------------------------------
// Output the jpeg:
// 433 points wide (72 equals one inch)
// no Y rotation/skew
// no X rotation/skew
// 300 points high (72 equals one inch)
// 323 points from page edge left (72 equals one inch)
// 276 points from page edge bottom (72 equals one inch)
ContentString := ContentString + '433 0 0 300 323 276 cm /ImageUberDude Do ';
//----------------------------------------------------------------------------
// Restore the graphic state by sending a 'grestore'
//----------------------------------------------------------------------------
ContentString := ContentString + 'Q'; // we are done (no extra space needed)
//----------------------------------------------------------------------------
// Create a Content stream object from the 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_Doc_Images_Jpeg_Free(JpegHandle);
UberPdfSdk_Pdf_Dict_Free(XObjectDictHandle);
UberPdfSdk_Pdf_Dict_Free(ResourceDictHandle);
UberPdfSdk_Pdf_Array_Free(ContentsArrayHandle);
//----------------------------------------------------------------------------
// return the page handle
//----------------------------------------------------------------------------
result := PageHandle;
//----------------------------------------------------------------------------
end;
end.