forked from ivanagui2/PDFjet-Open-Source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example_19.cs
216 lines (159 loc) · 6.43 KB
/
Example_19.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics;
using PDFjet.NET;
/**
* Example_19.cs
*
*/
class Example_19 {
public Example_19(String fileNumber, String fileName) {
PDF pdf = new PDF(new BufferedStream(
new FileStream("Example_19_" + fileNumber + ".pdf", FileMode.Create)));
BufferedStream bis = new BufferedStream(
new FileStream("data/testPDFs/" + fileName, FileMode.Open));
SortedDictionary<Int32, PDFobj> objects = pdf.Read(bis);
bis.Close();
Image image = new Image(
objects,
new BufferedStream(new FileStream(
"images/BARCODE.PNG", FileMode.Open, FileAccess.Read)),
ImageType.PNG);
image.SetLocation(480f, 57f);
image.ScaleBy(0.40f);
Font f1 = new Font(
objects,
new FileStream("fonts/Droid/DroidSans.ttf.stream",
FileMode.Open,
FileAccess.Read), Font.STREAM);
f1.SetSize(12f);
Font f2 = new Font(
objects,
new FileStream("fonts/Droid/DroidSans-Bold.ttf.stream",
FileMode.Open,
FileAccess.Read), Font.STREAM);
f2.SetSize(12f);
List<PDFobj> pages = pdf.GetPageObjects(objects);
Page page = null;
for (int i = 0; i < pages.Count; i++) {
page = new Page(pdf, pages[i]);
// page.InvertYAxis();
page.AddResource(image, objects);
page.AddResource(f1, objects);
page.AddResource(f2, objects);
Font f3 = page.AddResource(CoreFont.HELVETICA, objects).SetSize(12f);
image.DrawOn(page);
float x = 23f;
float y = 185f;
float dx = 15f;
float dy = 24f;
page.SetBrushColor(Color.blue);
// First Name and Initial
// page.DrawString(f1, "John", x, y);
page.DrawString(f2, "Иван", x, y);
// Last Name
page.DrawString(f3, "Jones", x + 258f, y);
// Social Insurance Number
page.DrawString(f1, StripSpacesAndDashes("243-590-129"), x + 437f, y, dx);
// Last Name at Birth
page.DrawString(f1, "Culverton", x, y += dy);
// Mailing Address
page.DrawString(f1, "10 Elm Street", x, y += dy);
// City
page.DrawString(f1, "Toronto", x, y + dy);
// Province or Territory
page.DrawString(f1, "Ontario", x + 365f, y += dy);
// Postal Code
page.DrawString(f1, StripSpacesAndDashes("L7B 2E9"), x + 482f, y, dx);
// Home Address
page.DrawString(f1, "10 Oak Road", x, y += dy);
// City
y += dy;
page.DrawString(f1, "Toronto", x, y);
// Previous Province or Territory
page.DrawString(f1, "Ontario", x + 365f, y);
// Postal Code
page.DrawString(f1, StripSpacesAndDashes("L7B 2E9"), x + 482f, y, dx);
// Home telephone number
page.DrawString(f1, "905-222-3333", x, y + dy);
// Work telephone number
page.DrawString(f1, "416-567-9903", x + 279f, y += dy);
// Previous province or territory
page.DrawString(f1, "British Columbia", x + 452f, y += dy);
// Move date from previous province or territory
y += dy;
page.DrawString(f1, StripSpacesAndDashes("2016-04-12"), x + 452f, y, dx);
// Date new maritial status began
page.DrawString(f1, StripSpacesAndDashes("2014-11-02"), x + 452f, 467f, dx);
// First name of spouse
y = 521f;
page.DrawString(f1, "Melanie", x, y);
// Last name of spouse
page.DrawString(f1, "Jones", x + 258f, y);
// Social Insurance number of spouse
page.DrawString(f1, StripSpacesAndDashes("192-760-427"), x + 437f, y, dx);
// Spouse or common-law partner's address
page.DrawString(f1, "12 Smithfield Drive", x, 554f);
// Signature Date
page.DrawString(f1, "2016-08-07", x + 475f, 615f);
// Signature Date of spouse
page.DrawString(f1, "2016-08-07", x + 475f, 651f);
// Female Checkbox 1
// xMarkCheckBox(page, 477.5f, 197.5f, 7f);
// Male Checkbox 1
XMarkCheckBox(page, 534.5f, 197.5f, 7f);
// Married
XMarkCheckBox(page, 34.5f, 424f, 7f);
// Living common-law
// XMarkCheckBox(page, 121.5f, 424f, 7f);
// Widowed
// XMarkCheckBox(page, 235.5f, 424f, 7f);
// Divorced
// XMarkCheckBox(page, 325.5f, 424f, 7f);
// Separated
// XMarkCheckBox(page, 415.5f, 424f, 7f);
// Single
// XMarkCheckBox(page, 505.5f, 424f, 7f);
// Female Checkbox 2
XMarkCheckBox(page, 478.5f, 536.5f, 7f);
// Male Checkbox 2
// XMarkCheckBox(page, 535.5f, 536.5f, 7f);
page.Complete(objects);
}
if (fileName.EndsWith("rc65-16e.pdf")) {
HashSet<Int32> set = new HashSet<Int32>();
set.Add(2);
pdf.RemovePages(set, objects);
}
pdf.AddObjects(objects);
pdf.Close();
}
private void XMarkCheckBox(Page page, float x, float y, float diagonal) {
page.SetPenColor(Color.blue);
page.SetPenWidth(diagonal / 5);
page.MoveTo(x, y);
page.LineTo(x + diagonal, y + diagonal);
page.MoveTo(x, y + diagonal);
page.LineTo(x + diagonal, y);
page.StrokePath();
}
private String StripSpacesAndDashes(String str) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < str.Length; i++) {
char ch = str[i];
if (ch != ' ' && ch != '-') {
buf.Append(ch);
}
}
return buf.ToString();
}
public static void Main(String[] args) {
Stopwatch sw = Stopwatch.StartNew();
long time0 = sw.ElapsedMilliseconds;
new Example_19("00", "rc65-16e.pdf");
long time1 = sw.ElapsedMilliseconds;
Console.WriteLine("Example_19 => " + (time1 - time0));
}
} // End of Example_19.cs