-
Notifications
You must be signed in to change notification settings - Fork 9
/
PDFUtil.java
executable file
·459 lines (403 loc) · 18.8 KB
/
PDFUtil.java
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
*
* Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
* Copyright (c) 2015, Lucee Assosication Switzerland
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
**/
package org.lucee.extension.pdf.util;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;
import org.lucee.extension.pdf.PDFStruct;
import org.lucee.extension.pdf.img.PDF2ImageICEpdf;
import org.lucee.extension.pdf.tag.PDF;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SimpleBookmark;
import lucee.commons.io.res.Resource;
import lucee.loader.engine.CFMLEngine;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.loader.util.Util;
import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;
import lucee.runtime.util.ListUtil;
public class PDFUtil {
public static final int ENCRYPT_RC4_40 = PdfWriter.STANDARD_ENCRYPTION_40;
public static final int ENCRYPT_RC4_128 = PdfWriter.STANDARD_ENCRYPTION_128;
public static final int ENCRYPT_RC4_128M = PdfWriter.STANDARD_ENCRYPTION_128;
public static final int ENCRYPT_AES_128 = PdfWriter.ENCRYPTION_AES_128;
public static final int ENCRYPT_NONE = -1;
private static final int PERMISSION_ALL = PdfWriter.ALLOW_ASSEMBLY + PdfWriter.ALLOW_COPY + PdfWriter.ALLOW_DEGRADED_PRINTING + PdfWriter.ALLOW_FILL_IN
+ PdfWriter.ALLOW_MODIFY_ANNOTATIONS + PdfWriter.ALLOW_MODIFY_CONTENTS + PdfWriter.ALLOW_PRINTING + PdfWriter.ALLOW_SCREENREADERS + PdfWriter.ALLOW_COPY;// muss 2 mal
// sein, keine
// ahnung wieso
/**
* convert a string list of permission
*
* @param strPermissions
* @return
* @throws PageException
*/
public static int toPermissions(String strPermissions) throws PageException {
if (strPermissions == null) return 0;
int permissions = 0;
strPermissions = strPermissions.trim();
ListUtil util = CFMLEngineFactory.getInstance().getListUtil();
String[] arr = util.toStringArray(util.toArrayRemoveEmpty(strPermissions, ","));
for (int i = 0; i < arr.length; i++) {
permissions = add(permissions, toPermission(arr[i]));
}
return permissions;
}
/**
* convert a string defintion of a permision in a integer Constant (PdfWriter.ALLOW_XXX)
*
* @param strPermission
* @return
* @throws PageException
*/
public static int toPermission(String strPermission) throws PageException {
strPermission = strPermission.trim().toLowerCase();
if ("allowassembly".equals(strPermission)) return PdfWriter.ALLOW_ASSEMBLY;
else if ("none".equals(strPermission)) return 0;
else if ("all".equals(strPermission)) return PERMISSION_ALL;
else if ("assembly".equals(strPermission)) return PdfWriter.ALLOW_ASSEMBLY;
else if ("documentassembly".equals(strPermission)) return PdfWriter.ALLOW_ASSEMBLY;
else if ("allowdegradedprinting".equals(strPermission)) return PdfWriter.ALLOW_DEGRADED_PRINTING;
else if ("degradedprinting".equals(strPermission)) return PdfWriter.ALLOW_DEGRADED_PRINTING;
else if ("printing".equals(strPermission)) return PdfWriter.ALLOW_DEGRADED_PRINTING;
else if ("allowfillin".equals(strPermission)) return PdfWriter.ALLOW_FILL_IN;
else if ("fillin".equals(strPermission)) return PdfWriter.ALLOW_FILL_IN;
else if ("fillingform".equals(strPermission)) return PdfWriter.ALLOW_FILL_IN;
else if ("allowmodifyannotations".equals(strPermission)) return PdfWriter.ALLOW_MODIFY_ANNOTATIONS;
else if ("modifyannotations".equals(strPermission)) return PdfWriter.ALLOW_MODIFY_ANNOTATIONS;
else if ("allowmodifycontents".equals(strPermission)) return PdfWriter.ALLOW_MODIFY_CONTENTS;
else if ("modifycontents".equals(strPermission)) return PdfWriter.ALLOW_MODIFY_CONTENTS;
else if ("allowcopy".equals(strPermission)) return PdfWriter.ALLOW_COPY;
else if ("copy".equals(strPermission)) return PdfWriter.ALLOW_COPY;
else if ("copycontent".equals(strPermission)) return PdfWriter.ALLOW_COPY;
else if ("allowprinting".equals(strPermission)) return PdfWriter.ALLOW_PRINTING;
else if ("printing".equals(strPermission)) return PdfWriter.ALLOW_PRINTING;
else if ("allowscreenreaders".equals(strPermission)) return PdfWriter.ALLOW_SCREENREADERS;
else if ("screenreaders".equals(strPermission)) return PdfWriter.ALLOW_SCREENREADERS;
else throw CFMLEngineFactory.getInstance().getExceptionUtil().createApplicationException("invalid permission [" + strPermission
+ "], valid permission values are [AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations, AllowFillIn, AllowScreenReaders, AllowAssembly, AllowDegradedPrinting]");
}
private static int add(int permissions, int permission) {
if (permission == 0 || (permissions & permission) > 0) return permissions;
return permissions + permission;
}
/**
* @param docs
* @param os
* @param removePages if true, pages defined in PDFDocument will be removed, otherwise all other
* pages will be removed
* @param version
* @throws PageException
* @throws IOException
* @throws DocumentException
*/
public static void concat(PDFStruct[] docs, OutputStream os, boolean keepBookmark, boolean removePages, boolean stopOnError, char version)
throws PageException, IOException, DocumentException {
Document document = null;
PdfCopy writer = null;
PdfReader reader;
Set pages;
boolean isInit = false;
PdfImportedPage page;
try {
int pageOffset = 0;
ArrayList master = new ArrayList();
for (int i = 0; i < docs.length; i++) {
// we create a reader for a certain document
pages = docs[i].getPages();
try {
reader = docs[i].getPdfReader();
}
catch (Throwable t) {
if (t instanceof ThreadDeath) throw (ThreadDeath) t;
if (!stopOnError) continue;
throw CFMLEngineFactory.getInstance().getCastUtil().toPageException(t);
}
reader.consolidateNamedDestinations();
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
List bookmarks = keepBookmark ? SimpleBookmark.getBookmark(reader) : null;
if (bookmarks != null) {
removeBookmarks(bookmarks, pages, removePages);
if (pageOffset != 0) SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
master.addAll(bookmarks);
}
if (!isInit) {
isInit = true;
document = new Document(reader.getPageSizeWithRotation(1));
writer = new PdfCopy(document, os);
if (version != 0) writer.setPdfVersion(version);
document.open();
}
for (int y = 1; y <= n; y++) {
if (pages != null && removePages == pages.contains(Integer.valueOf(y))) {
continue;
}
pageOffset++;
page = writer.getImportedPage(reader, y);
writer.addPage(page);
}
PRAcroForm form = reader.getAcroForm();
if (form != null) writer.copyAcroForm(reader);
}
if (master.size() > 0) writer.setOutlines(master);
}
finally {
CFMLEngineFactory.getInstance().getIOUtil().closeSilent(document);
}
}
private static void removeBookmarks(List bookmarks, Set pages, boolean removePages) {
int size = bookmarks.size();
for (int i = size - 1; i >= 0; i--) {
if (removeBookmarks((Map) bookmarks.get(i), pages, removePages)) bookmarks.remove(i);
}
}
private static boolean removeBookmarks(Map bookmark, Set pages, boolean removePages) {
List kids = (List) bookmark.get("Kids");
if (kids != null) removeBookmarks(kids, pages, removePages);
Integer page = CFMLEngineFactory.getInstance().getCastUtil().toInteger(CFMLEngineFactory.getInstance().getListUtil().first((String) bookmark.get("Page"), " ", true), -1);
return removePages == (pages != null && pages.contains(page));
}
public static Set<Integer> parsePageDefinition(String strPages, int lastPageNumber) throws PageException {
if (Util.isEmpty(strPages)) return null;
HashSet<Integer> set = new HashSet<Integer>();
parsePageDefinition(set, strPages, lastPageNumber);
return set;
}
public static void parsePageDefinition(Set<Integer> pages, String strPages, int lastPageNumber) throws PageException {
if (Util.isEmpty(strPages)) return;
CFMLEngine engine = CFMLEngineFactory.getInstance();
String[] arr = CFMLEngineFactory.getInstance().getListUtil().toStringArrayTrim(CFMLEngineFactory.getInstance().getListUtil().toArrayRemoveEmpty(strPages, ","));
int index, from, to;
String strFrom, strTo;
for (int i = 0; i < arr.length; i++) {
index = arr[i].indexOf('-');
if (index == -1) pages.add(engine.getCastUtil().toInteger(arr[i].trim()));
else {
strFrom = arr[i].substring(0, index).trim();
strTo = arr[i].substring(index + 1).trim();
if (i == 0 && Util.isEmpty(strFrom, true)) from = 1;
else from = engine.getCastUtil().toIntValue(strFrom);
if (i == (arr.length - 1) && Util.isEmpty(strTo, true)) to = lastPageNumber;
else to = engine.getCastUtil().toIntValue(strTo);
for (int y = from; y <= to; y++) {
pages.add(Integer.valueOf(y));
}
}
}
}
public static void encrypt(PDFStruct doc, OutputStream os, String newUserPassword, String newOwnerPassword, int permissions, int encryption)
throws PageException, DocumentException, IOException {
if (Util.isEmpty(newOwnerPassword)) newOwnerPassword = newUserPassword;
byte[] user = newUserPassword == null ? null : newUserPassword.getBytes();
byte[] owner = newOwnerPassword == null ? null : newOwnerPassword.getBytes();
PdfReader pr = doc.getPdfReader();
List bookmarks = SimpleBookmark.getBookmark(pr);
int n = pr.getNumberOfPages();
Document document = new Document(pr.getPageSizeWithRotation(1));
PdfCopy writer = new PdfCopy(document, os);
if (encryption != ENCRYPT_NONE) writer.setEncryption(user, owner, permissions, encryption);
document.open();
PdfImportedPage page;
for (int i = 1; i <= n; i++) {
page = writer.getImportedPage(pr, i);
writer.addPage(page);
}
PRAcroForm form = pr.getAcroForm();
if (form != null) writer.copyAcroForm(pr);
if (bookmarks != null) writer.setOutlines(bookmarks);
document.close();
}
public static Map<String, String> generateGoToBookMark(String title, int page) {
return generateGoToBookMark(title, page, 0, 731);
}
public static Map<String, String> generateGoToBookMark(String title, int page, int x, int y) {
Map<String, String> map = new HashMap<String, String>();
map.put("Title", title);
map.put("Action", "GoTo");
map.put("Page", page + " XYZ " + x + " " + y + " null");
return map;
}
public static void setChildBookmarks(Map parent, List children) {
Object kids = parent.get("Kids");
if (kids instanceof List) {
((List) kids).addAll(children);
}
else parent.put("Kids", children);
}
public static PdfReader toPdfReader(PageContext pc, Object value, String password) throws IOException, PageException {
if (value instanceof PdfReader) return (PdfReader) value;
if (value instanceof PDFStruct) return ((PDFStruct) value).getPdfReader();
CFMLEngine engine = CFMLEngineFactory.getInstance();
if (engine.getDecisionUtil().isBinary(value)) {
if (password != null) return new PdfReader(engine.getCastUtil().toBinary(value), password.getBytes());
return new PdfReader(engine.getCastUtil().toBinary(value));
}
if (value instanceof Resource) {
if (password != null) return new PdfReader(toBytes((Resource) value), password.getBytes());
return new PdfReader(toBytes((Resource) value));
}
if (value instanceof String) {
Resource res = engine.getResourceUtil().toResourceExisting(pc, (String) value);
if (password != null) return new PdfReader(toBytes(res), password.getBytes());
return new PdfReader(toBytes(res));
}
throw engine.getExceptionUtil().createCasterException(value, PdfReader.class);
}
public static byte[] toBytes(Resource res) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CFMLEngineFactory.getInstance().getIOUtil().copy(res.getInputStream(), baos, true, true);
return baos.toByteArray();
}
public static BufferedImage toImage(PDFStruct doc) throws PageException, IOException {
return new PDFRenderer(doc.toPDDocument()).renderImage(0, 1); // 0 is page number, 1 is scale
}
public static void writeImages(byte[] input, Set pages, Resource outputDirectory, String prefix, String format, int scale, boolean overwrite, boolean goodQuality,
boolean transparent) throws PageException, IOException {
// TODO PDF2Image.getInstance().writeImages(input, pages, outputDirectory, prefix, format, scale,
// overwrite, goodQuality, transparent);
}
public static Object extractText(PDFStruct doc, Set<Integer> pageNumbers, int type, Resource destination) throws IOException, InvalidPasswordException {
PDDocument pdDoc = doc.toPDDocument();
// PDDocument newDocument = new PDDocument();
// List pages = pdDoc.getDocumentCatalog().getAllPages();
// pages.
// pdDoc.getDocumentCatalog().
CFMLEngine engine = CFMLEngineFactory.getInstance();
int n = pdDoc.getNumberOfPages();
Iterator<Integer> it = pageNumbers.iterator();
// PDFTextStripper textStripper=new PDFTextStripper();
int p;
StringBuilder sb = new StringBuilder();
PDFTextStripper stripper = new PDFTextStripper();
if (destination != null) stripper.setLineSeparator(" ");
if (type == PDF.TYPE_XML) {
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<DocText>");
sb.append("<TextPerPage>");
}
while (it.hasNext()) {
PDDocument document = new PDDocument();
p = it.next();
if (type == PDF.TYPE_XML) sb.append("<page pagenumber=" + "\"" + p + "\" " + ">");
if (p > n) throw new RuntimeException("pdf page size [" + p + "] out of range, maximum page size is [" + n + "]");
document.addPage(pdDoc.getDocumentCatalog().getPages().get(p - 1));
stripper.setSortByPosition(true);
String text = stripper.getText(document);
sb.append(text);
if (type == PDF.TYPE_XML) sb.append("</page>");
}
if (type == PDF.TYPE_XML) {
sb.append("</TextPerPage>");
sb.append("</DocText>");
}
// print.o(pages);
// pdDoc.
// PDFTextStripperByArea stripper = new PDFTextStripperByArea();
// PDFHighlighter stripper = new PDFHighlighter();
// PDFText2HTML stripper = new PDFText2HTML("UDF-8");// TODO pass in encoding
// PDFTextStripper stripper = new PDFTextStripper();
// StringWriter writer = new StringWriter();
// stripper.writeText(document, writer);
if (destination != null) engine.getIOUtil().copy(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")), destination, true);
return sb.toString();
// return pdDoc.getDocumentCatalog().getAllPages().get(2);
}
public static void thumbnail(PageContext pc, PDFStruct doc, String destination, Set<Integer> pageNumbers, String format, String imagePrefix, int scale, boolean overwrite) throws IOException {
CFMLEngine engine = CFMLEngineFactory.getInstance();
PDDocument pdDoc = doc.toPDDocument();
int n = pdDoc.getNumberOfPages();
Iterator<Integer> it = pageNumbers.iterator();
int p;
PDFRenderer pdfRender = new PDFRenderer(pdDoc);
while (it.hasNext()) {
p = it.next();
if (p > n) throw new RuntimeException("pdf page size [" + p + "] out of range, maximum page size is [" + n + "]");
// thumbnail image file destination
String imageDestination = destination + "/" + imagePrefix + "_page_" + p + "." + format;
BufferedImage thumbnailImage = pdfRender.renderImageWithDPI(p - 1, scale);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(thumbnailImage, format, baos); // this one not support .tiff format
Resource res = engine.getResourceUtil().toResourceNotExisting(pc, imageDestination);
if (res.exists() && !overwrite) throw new RuntimeException("Thumbnail image file already exists [" + imageDestination + "] and overwrite was false");
engine.getIOUtil().copy(new ByteArrayInputStream(baos.toByteArray()), res, true);
}
}
public static void extractImages(PageContext pc,PDFStruct doc, Set<Integer> pageNumbers,Resource destination, String imagePrefix, String format, boolean overwrite) throws IOException, InvalidPasswordException,PageException {
PDDocument pdDoc = doc.toPDDocument();
int n = pdDoc.getNumberOfPages();
Iterator<Integer> it = pageNumbers.iterator();
int p;
PDPageTree pages= pdDoc.getPages();
int i = 1;
while (it.hasNext()) {
p = it.next();
if (p > n) throw new RuntimeException("pdf page size [" + p + "] out of range, maximum page size is [" + n + "]");
PDResources pdResources = pages.get(p - 1).getResources();
// workjaround, getXObjectNames() returns images in reverse order
ArrayList<COSName> xObjectNamesReversed = new ArrayList<>();
for (COSName name : pdResources.getXObjectNames()) {
xObjectNamesReversed.add(name);
}
Collections.reverse(xObjectNamesReversed);
for (COSName name : xObjectNamesReversed) {
PDXObject o = pdResources.getXObject(name);
if (o instanceof PDImageXObject) {
PDImageXObject image = (PDImageXObject)o;
String filename = destination + "/" + imagePrefix + "-" + i + "." + format;
CFMLEngine engine = CFMLEngineFactory.getInstance();
Resource res = engine.getResourceUtil().toResourceNotExisting(pc,filename);
if (res.exists() && !overwrite) throw new RuntimeException("image file already exists [" + filename + "] and overwrite was false");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image.getImage(), format, baos);
CFMLEngineFactory.getInstance().getIOUtil().copy(new ByteArrayInputStream(baos.toByteArray()),res.getOutputStream(),true, true);
i++;
}
}
}
}
}