PdfPostProcessorUtilities.java
37.1 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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.fontengine.inlineformatting.css20.CSS20GenericFontFamily
* com.adobe.internal.afml.AFMLException
* com.adobe.internal.io.ByteReader
* com.adobe.internal.io.ByteWriterFactory
* com.adobe.internal.io.InputStreamByteReader
* com.adobe.internal.pdftoolkit.core.exceptions.PDFException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFFontException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet
* com.adobe.internal.pdftoolkit.core.fontset.PDFFontSetManager
* com.adobe.internal.pdftoolkit.core.permissionprovider.PermissionProvider
* com.adobe.internal.pdftoolkit.core.types.ASName
* com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog
* com.adobe.internal.pdftoolkit.pdf.document.PDFCosObjectContainer
* com.adobe.internal.pdftoolkit.pdf.document.PDFDocument
* com.adobe.internal.pdftoolkit.pdf.document.PDFOpenAction
* com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions
* com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFICCProfile
* com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectForm
* com.adobe.internal.pdftoolkit.pdf.interactive.PDFViewerPreferences
* com.adobe.internal.pdftoolkit.pdf.interactive.action.PDFAction
* com.adobe.internal.pdftoolkit.pdf.interactive.action.PDFActionGoTo
* com.adobe.internal.pdftoolkit.pdf.interactive.navigation.PDFDestination
* com.adobe.internal.pdftoolkit.pdf.interactive.navigation.PDFDestinationExplicit
* com.adobe.internal.pdftoolkit.pdf.interchange.prepress.PDFOutputIntent
* com.adobe.internal.pdftoolkit.pdf.interchange.prepress.PDFOutputIntentsList
* com.adobe.internal.pdftoolkit.pdf.page.PDFPage
* com.adobe.internal.pdftoolkit.pdf.page.PDFPageLayout
* com.adobe.internal.pdftoolkit.pdf.page.PDFPageMode
* com.adobe.internal.pdftoolkit.pdf.page.PDFPageTree
* com.adobe.internal.pdftoolkit.services.rcg.RCGAlignment
* com.adobe.internal.pdftoolkit.services.rcg.RCGOptions
* com.adobe.internal.pdftoolkit.services.rcg.RCGOverflowMode
* com.adobe.internal.pdftoolkit.services.rcg.RCGWrapOption
* com.adobe.internal.pdftoolkit.services.rcg.RichTextContentGenerator
* com.adobe.internal.pdftoolkit.services.security.SecurityLockPassword
* com.adobe.internal.pdftoolkit.services.security.StandardEncryptionPermissions
* com.adobe.internal.pdftoolkit.services.xfatext.FormatXFAResultInfo
* com.adobe.internal.pdftoolkit.services.xfatext.RegionAndAnnotInfo
* com.adobe.internal.pdftoolkit.services.xobjhandler.XObjectApplyOptions
* com.adobe.internal.pdftoolkit.services.xobjhandler.XObjectContentType
* com.adobe.internal.pdftoolkit.services.xobjhandler.XObjectUseOptions
* com.adobe.native2pdf.xml.InitialView
* com.adobe.native2pdf.xml.InitialView$DocumentOptions
* com.adobe.native2pdf.xml.InitialView$DocumentOptions$ZoomType
* com.adobe.native2pdf.xml.InitialView$UiOptions
* com.adobe.native2pdf.xml.InitialView$WindowOptions
* com.adobe.native2pdf.xml.SecuritySettings
* com.adobe.native2pdf.xml.SecuritySettings$Settings
* com.adobe.pdfg.common.Constants
* com.adobe.pdfg.common.FileUtilities
* com.adobe.pdfg.logging.PDFGLogger
* com.rsa.jsafe.provider.JsafeJCE
*/
package com.adobe.pdfg.postprocess;
import com.adobe.fontengine.inlineformatting.css20.CSS20GenericFontFamily;
import com.adobe.internal.afml.AFMLException;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriterFactory;
import com.adobe.internal.io.InputStreamByteReader;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFFontException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet;
import com.adobe.internal.pdftoolkit.core.fontset.PDFFontSetManager;
import com.adobe.internal.pdftoolkit.core.permissionprovider.PermissionProvider;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObjectContainer;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFOpenAction;
import com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFICCProfile;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectForm;
import com.adobe.internal.pdftoolkit.pdf.interactive.PDFViewerPreferences;
import com.adobe.internal.pdftoolkit.pdf.interactive.action.PDFAction;
import com.adobe.internal.pdftoolkit.pdf.interactive.action.PDFActionGoTo;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.PDFDestination;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.PDFDestinationExplicit;
import com.adobe.internal.pdftoolkit.pdf.interchange.prepress.PDFOutputIntent;
import com.adobe.internal.pdftoolkit.pdf.interchange.prepress.PDFOutputIntentsList;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageLayout;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageMode;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageTree;
import com.adobe.internal.pdftoolkit.services.rcg.RCGAlignment;
import com.adobe.internal.pdftoolkit.services.rcg.RCGOptions;
import com.adobe.internal.pdftoolkit.services.rcg.RCGOverflowMode;
import com.adobe.internal.pdftoolkit.services.rcg.RCGWrapOption;
import com.adobe.internal.pdftoolkit.services.rcg.RichTextContentGenerator;
import com.adobe.internal.pdftoolkit.services.security.SecurityLockPassword;
import com.adobe.internal.pdftoolkit.services.security.StandardEncryptionPermissions;
import com.adobe.internal.pdftoolkit.services.xfatext.FormatXFAResultInfo;
import com.adobe.internal.pdftoolkit.services.xfatext.RegionAndAnnotInfo;
import com.adobe.internal.pdftoolkit.services.xobjhandler.XObjectApplyOptions;
import com.adobe.internal.pdftoolkit.services.xobjhandler.XObjectContentType;
import com.adobe.internal.pdftoolkit.services.xobjhandler.XObjectUseOptions;
import com.adobe.native2pdf.xml.InitialView;
import com.adobe.native2pdf.xml.SecuritySettings;
import com.adobe.pdfg.common.Constants;
import com.adobe.pdfg.common.FileUtilities;
import com.adobe.pdfg.logging.PDFGLogger;
import com.adobe.pdfg.postprocess.PdfPostProcessorImpl;
import com.rsa.jsafe.provider.JsafeJCE;
import java.awt.color.ICC_Profile;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.Provider;
import java.security.Security;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public final class PdfPostProcessorUtilities {
private static PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(PdfPostProcessorUtilities.class);
private static final String richText0 = "<body style=\"font-size:24.0pt;font-family:Helvetica,sans-serif\"><p><span style=\"text-decoration:;font-family:Helvetica;color:#FF0000;font-weight:bold\">ADEP Document Services - PDF Generator Evaluation</span></p></body>";
private static final String defaultStyle = "font: Helvetica,sans-serif 9pt";
private static PDFFontSet defaultFontSet = null;
private static XObjectUseOptions useOptions = null;
private PdfPostProcessorUtilities() {
}
public static PDFPageMode getPdfPageMode(String pageMode) {
PDFPageMode pdfPageMode = PDFPageMode.PagesOnly;
if (Constants.IV_DOCUMENTSHOW_OPTIONS[1].equals(pageMode)) {
pdfPageMode = PDFPageMode.WithBookmarks;
} else if (Constants.IV_DOCUMENTSHOW_OPTIONS[2].equals(pageMode)) {
pdfPageMode = PDFPageMode.WithThumbs;
} else if (Constants.IV_DOCUMENTSHOW_OPTIONS[3].equals(pageMode)) {
pdfPageMode = PDFPageMode.WithAttachments;
} else if (Constants.IV_DOCUMENTSHOW_OPTIONS[4].equals(pageMode)) {
pdfPageMode = PDFPageMode.WithLayers;
}
return pdfPageMode;
}
public static PDFPageLayout getPdfPageLayout(String pageLayout) {
PDFPageLayout pdfPageLayout = null;
if (Constants.IV_PAGELAYOUT_OPTIONS[1].equals(pageLayout)) {
pdfPageLayout = PDFPageLayout.SinglePage;
} else if (Constants.IV_PAGELAYOUT_OPTIONS[2].equals(pageLayout)) {
pdfPageLayout = PDFPageLayout.Continuous;
} else if (Constants.IV_PAGELAYOUT_OPTIONS[3].equals(pageLayout)) {
pdfPageLayout = PDFPageLayout.TwoPageRight;
} else if (Constants.IV_PAGELAYOUT_OPTIONS[4].equals(pageLayout)) {
pdfPageLayout = PDFPageLayout.TwoColumnRight;
}
return pdfPageLayout;
}
public static PDFDestination getOpenDestination(InitialView.DocumentOptions.ZoomType zoom, PDFPage page) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
int UNSPECIFIED_POSITION = -32768;
PDFDestinationExplicit openDestination = null;
String zoomMode = zoom.getZoomMode();
if ("Fit Page".equals(zoomMode)) {
openDestination = PDFDestination.newDestFit((PDFPage)page);
} else if ("Fit Width".equals(zoomMode)) {
openDestination = PDFDestination.newDestFitH((PDFPage)page, (float)-32768.0f);
} else if ("Fit Visible".equals(zoomMode)) {
openDestination = PDFDestination.newDestFitBH((PDFPage)page, (float)-32768.0f);
} else if ("Default".equals(zoomMode)) {
openDestination = PDFDestination.newDestXYZ((PDFPage)page, (float)-32768.0f, (float)-32768.0f, (float)-32768.0f);
} else if ("Percentage".equals(zoomMode)) {
float percent = zoom.getPercent() / 100.0f;
openDestination = PDFDestination.newDestXYZ((PDFPage)page, (float)-32768.0f, (float)-32768.0f, (float)percent);
}
return openDestination;
}
public static boolean getDisplayDocTitle(InitialView.WindowOptions options) {
boolean result = false;
String stringTitleBar = options.getTitleDisplay();
if ("Document Title".equals(stringTitleBar)) {
result = true;
}
return result;
}
public static void loadProvider(Provider provider) {
String providerName = provider.getName();
if (Security.getProvider(providerName) == null) {
Security.addProvider(provider);
}
}
public static void setPdfChangePermissions(int acrobatVersion, StandardEncryptionPermissions permissions, String changePermissions) {
boolean allowAssemble = false;
boolean allowFill = false;
boolean allowAdd = false;
boolean allowModify = false;
if ("Ins-Del".equals(changePermissions)) {
allowAssemble = true;
} else if ("FillForm".equals(changePermissions)) {
if (acrobatVersion == 3) {
allowFill = true;
allowModify = true;
} else {
allowFill = true;
}
} else if ("Comment".equals(changePermissions)) {
allowFill = true;
allowAdd = true;
} else if ("Any".equals(changePermissions)) {
allowAssemble = true;
allowFill = true;
allowAdd = true;
allowModify = true;
}
permissions.allowAssemble(allowAssemble);
permissions.allowFill(allowFill);
permissions.allowAdd(allowAdd);
permissions.allowModify(allowModify);
}
public static void setPdfPrintPermissions(StandardEncryptionPermissions permissions, String printPermissions) {
permissions.allowPrintLow(false);
permissions.allowPrintHigh(false);
if ("Low".equals(printPermissions)) {
permissions.allowPrintLow(true);
} else if ("High".equals(printPermissions)) {
permissions.allowPrintLow(true);
permissions.allowPrintHigh(true);
}
}
public static void addPdfAEntryToDOM(Document document, Element parent) {
Element pdfaDescription = document.createElement("rdf:Description");
parent.appendChild(pdfaDescription);
Attr attributePdfa = document.createAttribute("xmlns:pdfaid");
attributePdfa.setValue("http://www.aiim.org/pdfa/ns/id/");
pdfaDescription.setAttributeNode(attributePdfa);
Element pdfaidPart = document.createElement("pdfaid:part");
pdfaidPart.appendChild(document.createTextNode("1"));
pdfaDescription.appendChild(pdfaidPart);
Element pdfaidConformance = document.createElement("pdfaid:conformance");
pdfaidConformance.appendChild(document.createTextNode("B"));
pdfaDescription.appendChild(pdfaidConformance);
}
public static InputStream serializeXmpMetadata(Document document) throws Exception {
try {
ByteArrayOutputStream xmpOutput = new ByteArrayOutputStream();
try {
Class serializerClass = Class.forName("com.sun.org.apache.xml.internal.serialize.XMLSerializer");
Object serializer = serializerClass.newInstance();
Method setOutputByteStreamMethod = serializerClass.getMethod("setOutputByteStream", OutputStream.class);
Method serializeMethod = serializerClass.getMethod("serialize", Document.class);
setOutputByteStreamMethod.invoke(serializer, xmpOutput);
serializeMethod.invoke(serializer, document);
}
catch (ClassNotFoundException e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
Class serializerFactoryClass = Class.forName("org.apache.xml.serialize.SerializerFactory");
Method getSerializerFactoryMethod = serializerFactoryClass.getMethod("getSerializerFactory", String.class);
Object serializerFactoryObj = getSerializerFactoryMethod.invoke(null, "xml");
Method makeSerializerMethod = serializerFactoryClass.getMethod("makeSerializer", OutputStream.class, Class.forName("org.apache.xml.serialize.OutputFormat"));
Object serializerObject = makeSerializerMethod.invoke(serializerFactoryObj, xmpOutput, null);
Method asDomSerializerMethod = serializerObject.getClass().getMethod("asDOMSerializer", new Class[0]);
Object domSerializerObject = asDomSerializerMethod.invoke(serializerObject, new Object[0]);
Method serializeMethod = domSerializerObject.getClass().getMethod("serialize", Document.class);
serializeMethod.invoke(domSerializerObject, document);
}
return new ByteArrayInputStream(xmpOutput.toByteArray());
}
catch (Exception e) {
pdfgLogger.debug(e.getMessage(), null, (Throwable)e);
throw e;
}
catch (NoClassDefFoundError ncdfe) {
pdfgLogger.debug(ncdfe.getMessage(), null, (Throwable)ncdfe);
throw new Exception(ncdfe);
}
catch (NoSuchMethodError nsme) {
pdfgLogger.debug(nsme.getMessage(), null, (Throwable)nsme);
throw new Exception(nsme);
}
}
public static boolean doesPdfAEntryExist(Element parent) {
boolean result = false;
Element descriptionElement = null;
NodeList children = parent.getChildNodes();
for (int count = 0; count < children.getLength(); ++count) {
Node child = children.item(count);
if (!"rdf:Description".equals(child.getNodeName()) || child.getNodeType() != 1) continue;
descriptionElement = (Element)child;
break;
}
if (descriptionElement != null && descriptionElement.hasAttribute("rdf:about") && "".equals(descriptionElement.getAttribute("rdf:about")) && "http://www.aiim.org/pdfa/ns/id/".equals(descriptionElement.getAttribute("xmlns:pdfaid"))) {
Node pdfaidPartElement = null;
children = descriptionElement.getChildNodes();
for (int count2 = 0; count2 < children.getLength(); ++count2) {
Node child = children.item(count2);
if (!"pdfaid:part".equals(child.getNodeName()) || child.getNodeType() != 1) continue;
pdfaidPartElement = (Element)child;
break;
}
if (pdfaidPartElement != null && "1".equals(pdfaidPartElement.getTextContent())) {
Node pdfaidConformanceElement = null;
children = descriptionElement.getChildNodes();
for (int count3 = 0; count3 < children.getLength(); ++count3) {
Node child = children.item(count3);
if (!"pdfaid:conformance".equals(child.getNodeName()) || child.getNodeType() != 1) continue;
pdfaidConformanceElement = (Element)child;
break;
}
if (pdfaidConformanceElement != null && "B".equals(pdfaidConformanceElement.getTextContent())) {
result = true;
}
}
}
return result;
}
public static void updateProducerEntryInDOM(Element parent, String pdfProducerString) throws Exception {
if (parent == null) {
return;
}
NodeList children = parent.getChildNodes();
Node rdfDescNode = null;
for (int i = 0; i < children.getLength(); ++i) {
rdfDescNode = children.item(i);
if (!"rdf:Description".equals(rdfDescNode.getNodeName()) || rdfDescNode.getNodeType() != 1) continue;
NodeList producerNodes = rdfDescNode.getChildNodes();
for (int j = 0; j < producerNodes.getLength(); ++j) {
Node producerNode = producerNodes.item(j);
if (!"pdf:Producer".equals(producerNode.getNodeName()) || producerNode.getNodeType() != 1) continue;
Node producerNodeText = null;
if (producerNode.hasChildNodes()) {
producerNodeText = producerNode.getFirstChild();
producerNodeText.setNodeValue(pdfProducerString);
} else {
Document doc = producerNode.getOwnerDocument();
if (doc != null) {
producerNodeText = doc.createTextNode(pdfProducerString);
producerNode.appendChild(producerNodeText);
}
}
return;
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Document parseXmpMetadata(InputStream in) throws Exception {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(in);
return document;
}
finally {
FileUtilities.closeStream((InputStream)in);
}
}
public static Element findPdfAEntryParent(Element root) throws Exception {
Element parent = null;
NodeList children = root.getChildNodes();
for (int count = 0; count < children.getLength(); ++count) {
Node child = children.item(count);
if (!"rdf:RDF".equals(child.getNodeName()) || child.getNodeType() != 1) continue;
parent = (Element)child;
break;
}
return parent;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void addPdfAOutputIntent(PDFDocument pdfDocument, int imageColorSpace) throws Exception {
InputStream in = null;
PDFOutputIntent outputIntent = null;
ICC_Profile iccProfile = null;
PDFICCProfile pdfIccProfile = null;
try {
switch (imageColorSpace) {
case 4: {
in = PdfPostProcessorUtilities.class.getResourceAsStream("USWebCoatedSWOP.icc");
iccProfile = ICC_Profile.getInstance(in);
pdfIccProfile = PDFICCProfile.newInstance((PDFDocument)pdfDocument, (ICC_Profile)iccProfile);
outputIntent = PDFOutputIntent.newInstance((PDFDocument)pdfDocument, (String)"GTS_PDFA1", (String)"CGATS TR 001");
outputIntent.setOutputCondition("U.S. Web Coated (SWOP) v2");
outputIntent.setInfo("U.S. Web Coated (SWOP) v2");
outputIntent.setDestOutputProfile(pdfIccProfile);
break;
}
case 2:
case 3: {
in = PdfPostProcessorUtilities.class.getResourceAsStream("sRGB.icm");
iccProfile = ICC_Profile.getInstance(in);
pdfIccProfile = PDFICCProfile.newInstance((PDFDocument)pdfDocument, (ICC_Profile)iccProfile);
outputIntent = PDFOutputIntent.newInstance((PDFDocument)pdfDocument, (String)"GTS_PDFA1", (String)"sRGB IEC61966-2.1");
outputIntent.setOutputCondition("sRGB IEC61966-2.1");
outputIntent.setInfo("sRGB IEC61966-2.1");
outputIntent.setDestOutputProfile(pdfIccProfile);
}
}
if (outputIntent != null) {
PDFOutputIntentsList outputIntentsList = PDFOutputIntentsList.newInstance((PDFDocument)pdfDocument);
outputIntentsList.add((PDFCosObjectContainer)outputIntent);
PDFCatalog catalog = pdfDocument.requireCatalog();
catalog.setOutputIntents(outputIntentsList);
}
}
finally {
if (in != null) {
FileUtilities.closeStream((InputStream)in);
}
}
}
public static void applyInitialView(InitialView initialView, PDFDocument pdfDocument) throws Exception {
if (initialView != null) {
PDFViewerPreferences viewerPreferences = pdfDocument.procureViewerPreferences();
PDFCatalog catalog = pdfDocument.requireCatalog();
PDFPageMode pageMode = null;
if (initialView.isSetDocumentOptions()) {
InitialView.DocumentOptions.ZoomType zoom;
PDFDestination openDestination;
InitialView.DocumentOptions configDocumentOptions = initialView.getDocumentOptions();
pageMode = PdfPostProcessorUtilities.getPdfPageMode(configDocumentOptions.getPageMode());
catalog.setPageMode(pageMode);
PDFPageLayout layout = null;
String pageLayout = configDocumentOptions.getPageLayout();
if (pageLayout != null && !"".equals(pageLayout) && !Constants.IV_PAGELAYOUT_OPTIONS[0].equals(pageLayout)) {
layout = PdfPostProcessorUtilities.getPdfPageLayout(pageLayout);
}
if (layout == null) {
catalog.removeValue(ASName.k_PageLayout);
} else {
catalog.setPageLayout(layout);
}
PDFPageTree pageTree = pdfDocument.requirePages();
int numPages = pageTree.getNumPages();
int pageNumber = configDocumentOptions.getPageNumber();
int gotoPage = numPages < pageNumber ? numPages - 1 : pageNumber - 1;
PDFPage pdfPage = pageTree.getPage(gotoPage);
if (pdfPage != null && (openDestination = PdfPostProcessorUtilities.getOpenDestination(zoom = configDocumentOptions.getZoomType(), pdfPage)) != null) {
PDFOpenAction openAction = PDFOpenAction.newInstance((PDFDocument)pdfDocument, (PDFAction)PDFActionGoTo.newInstance((PDFDocument)pdfDocument, (PDFDestination)openDestination));
catalog.setOpenAction(openAction);
}
}
if (initialView.isSetWindowOptions()) {
InitialView.WindowOptions configWindowOptions = initialView.getWindowOptions();
viewerPreferences.setFitWindow(configWindowOptions.isFitWindow());
viewerPreferences.setCenterWindow(configWindowOptions.isCenterWindow());
viewerPreferences.setDisplayDocTitle(PdfPostProcessorUtilities.getDisplayDocTitle(configWindowOptions));
if (configWindowOptions.isFullScreenMode()) {
catalog.setPageMode(PDFPageMode.FullScreen);
if (pageMode != null) {
viewerPreferences.setPageMode(pageMode);
}
}
}
if (initialView.isSetUiOptions()) {
InitialView.UiOptions uiConfigOptions = initialView.getUiOptions();
viewerPreferences.setHideMenubar(uiConfigOptions.isHideMenuBar());
viewerPreferences.setHideToolbar(uiConfigOptions.isHideToolBar());
viewerPreferences.setHideWindowUI(uiConfigOptions.isHideWindowUI());
}
pdfDocument.setViewerPreferences(viewerPreferences);
}
}
public static SecurityLockPassword getLockPassword(SecuritySettings.Settings encryptionSettings) throws Exception {
SecurityLockPassword lock = null;
if (encryptionSettings != null) {
boolean enableTextMetaData;
PermissionProvider provider;
PdfPostProcessorUtilities.loadProvider((Provider)new JsafeJCE());
String strUserPasswd = "";
if (encryptionSettings.isSetDocumentOpenPasswd()) {
strUserPasswd = encryptionSettings.getDocumentOpenPasswd();
}
String strOwnerPasswd = "";
if (encryptionSettings.isSetDocumentChangePasswd()) {
strOwnerPasswd = encryptionSettings.getDocumentChangePasswd();
}
byte[] userPasswd = null;
if (strUserPasswd != null && strUserPasswd.length() > 0) {
userPasswd = strUserPasswd.getBytes("UTF-8");
}
byte[] ownerPasswd = null;
if (strOwnerPasswd != null && strOwnerPasswd.length() > 0) {
ownerPasswd = strOwnerPasswd.getBytes("UTF-8");
}
StandardEncryptionPermissions encryptPerms = StandardEncryptionPermissions.newInstanceAll();
int acrobatVersion = encryptionSettings.getAcrobatVersion();
if (strOwnerPasswd.length() > 0) {
boolean bAllowCopying = encryptionSettings.isEnableCopying();
boolean bAllowTextAccess = encryptionSettings.isEnableTextAccess();
encryptPerms.allowCopy(bAllowCopying);
if (acrobatVersion == 3) {
encryptPerms.allowExtract(bAllowCopying);
} else {
encryptPerms.allowExtract(bAllowTextAccess);
}
PdfPostProcessorUtilities.setPdfChangePermissions(acrobatVersion, encryptPerms, encryptionSettings.getChangePermissions());
PdfPostProcessorUtilities.setPdfPrintPermissions(encryptPerms, encryptionSettings.getPrintPermissions());
}
PermissionProvider permissionProvider = provider = ownerPasswd == null ? null : encryptPerms.getPermissionProvider();
if (acrobatVersion >= 7) {
enableTextMetaData = !encryptionSettings.getEncrypt().equals("AllContent");
boolean encryptAttachmentsOnly = encryptionSettings.getEncrypt().equals("Attachments");
lock = encryptAttachmentsOnly ? (acrobatVersion < 9 ? SecurityLockPassword.newAES_128bit((byte[])userPasswd) : (acrobatVersion == 9 ? SecurityLockPassword.newAES_256bitR5((byte[])userPasswd) : SecurityLockPassword.newAES_256bitR6((byte[])userPasswd))) : (acrobatVersion < 9 ? SecurityLockPassword.newAES_128bit((byte[])ownerPasswd, (byte[])userPasswd, (PermissionProvider)provider, (boolean)(!enableTextMetaData)) : (acrobatVersion == 9 ? SecurityLockPassword.newAES_256bitR5((byte[])ownerPasswd, (byte[])userPasswd, (PermissionProvider)provider, (boolean)(!enableTextMetaData)) : SecurityLockPassword.newAES_256bitR6((byte[])ownerPasswd, (byte[])userPasswd, (PermissionProvider)provider, (boolean)(!enableTextMetaData))));
} else if (acrobatVersion == 6) {
enableTextMetaData = !encryptionSettings.getEncrypt().equals("AllContent");
lock = SecurityLockPassword.newRC4_128bit((byte[])ownerPasswd, (byte[])userPasswd, (PermissionProvider)provider, (boolean)(!enableTextMetaData));
} else if (acrobatVersion == 5) {
lock = SecurityLockPassword.newRC4_128bit((byte[])ownerPasswd, (byte[])userPasswd, (PermissionProvider)provider);
} else if (acrobatVersion >= 3) {
lock = SecurityLockPassword.newRC4_40bit((byte[])ownerPasswd, (byte[])userPasswd, (PermissionProvider)provider);
}
}
return lock;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static boolean isEncrypted(File inputPDF) throws Exception {
InputStreamByteReader byteReader = null;
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(inputPDF));
byteReader = new InputStreamByteReader((InputStream)in);
PDFDocument pdfDocument = PDFDocument.newInstance((ByteReader)byteReader, (PDFOpenOptions)PDFOpenOptions.newInstance());
boolean bl = pdfDocument.isEncrypted();
return bl;
}
finally {
try {
if (byteReader != null) {
byteReader.close();
}
}
catch (IOException e) {
pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
}
}
}
public static PDFDocument applyWaterMark(PDFDocument pdfDocument, PDFOpenOptions openOptions, ByteWriterFactory custByteWriterFact, PdfPostProcessorImpl pdfPostProcessorImpl) throws Exception {
return pdfDocument;
}
private static PDFFontSet getDefaultFontSet() throws PDFFontException {
PDFFontSet pdfFontSet = PDFFontSetManager.getPDFFontSetInstance();
String[] serif = new String[]{"Minion Pro", "Garamond"};
String[] sans = new String[]{"Helvetica", "Gill Sans Std"};
String[] cursive = new String[]{"Zapf-Chancery", "Gill Sans Std"};
String[] fantasy = new String[]{"Minion Pro"};
String[] monospace = new String[]{"CourierNew", "Courier"};
pdfFontSet.setGenericFontFamilyName(CSS20GenericFontFamily.SANS_SERIF, sans);
pdfFontSet.setGenericFontFamilyName(CSS20GenericFontFamily.SERIF, serif);
pdfFontSet.setGenericFontFamilyName(CSS20GenericFontFamily.MONOSPACE, monospace);
pdfFontSet.setGenericFontFamilyName(CSS20GenericFontFamily.FANTASY, fantasy);
pdfFontSet.setGenericFontFamilyName(CSS20GenericFontFamily.CURSIVE, cursive);
return pdfFontSet;
}
private static void initializeDefaults() {
useOptions = new XObjectUseOptions();
useOptions.setPrintOn(true);
useOptions.setContentType(XObjectContentType.Watermark);
useOptions.makeForeground();
}
private static class WatermarkCache {
WatermarkEntry dummy = new WatermarkEntry();
HashMap<WatermarkKey, ArrayList<WatermarkEntry>> cache = new HashMap();
private WatermarkCache() {
}
public WatermarkEntry find(double inWidth, double inHeight) {
return this.find(inWidth, inHeight, this.dummy);
}
public WatermarkEntry find(double inWidth, double inHeight, WatermarkEntry waterMrkEnt) {
WatermarkKey key = new WatermarkKey(inWidth, inHeight);
ArrayList entryList = this.cache.get(key);
boolean newKey = false;
if (entryList == null && waterMrkEnt != null) {
entryList = new ArrayList();
newKey = true;
} else if (entryList == null) {
return null;
}
Iterator<WatermarkEntry> itr = entryList.iterator();
while (itr.hasNext()) {
WatermarkEntry e = itr.next();
if (e.width != inWidth || e.height != inHeight) continue;
if (waterMrkEnt != this.dummy) {
itr.remove();
entryList.add(waterMrkEnt);
return waterMrkEnt;
}
return e;
}
if (newKey) {
this.cache.put(key, entryList);
}
if (waterMrkEnt != null && waterMrkEnt != this.dummy) {
entryList.add(waterMrkEnt);
return waterMrkEnt;
}
return null;
}
public void put(WatermarkEntry e) {
WatermarkEntry r = this.find(e.width, e.height, e);
}
}
private static class WatermarkEntry {
double width;
double height;
PDFDocument pdfDocument;
PDFXObjectForm form;
XObjectApplyOptions applyOptions;
WatermarkEntry(PDFDocument inPDFDocument, double inWidth, double inHeight) throws PDFException, AFMLException {
this.width = inWidth;
this.height = inHeight;
this.pdfDocument = inPDFDocument;
this.init();
}
public WatermarkEntry() {
}
public void init() throws PDFException, AFMLException {
RCGOptions rcgOptions = new RCGOptions(this.width < 0.0 ? 0.0 : this.width, this.height < 0.0 ? 0.0 : this.height);
rcgOptions.setDefaultStyle("font: Helvetica,sans-serif 9pt");
rcgOptions.setOverflow(RCGOverflowMode.Auto);
rcgOptions.setVerticalAlignment(RCGAlignment.Middle);
rcgOptions.setWrap(RCGWrapOption.Emergency);
ByteArrayInputStream richText0Stream = null;
try {
richText0Stream = new ByteArrayInputStream("<body style=\"font-size:24.0pt;font-family:Helvetica,sans-serif\"><p><span style=\"text-decoration:;font-family:Helvetica;color:#FF0000;font-weight:bold\">ADEP Document Services - PDF Generator Evaluation</span></p></body>".getBytes("UTF-8"));
}
catch (UnsupportedEncodingException uee) {
pdfgLogger.trace(uee.getMessage(), null, (Throwable)uee);
richText0Stream = new ByteArrayInputStream("<body style=\"font-size:24.0pt;font-family:Helvetica,sans-serif\"><p><span style=\"text-decoration:;font-family:Helvetica;color:#FF0000;font-weight:bold\">ADEP Document Services - PDF Generator Evaluation</span></p></body>".getBytes());
}
FormatXFAResultInfo info = RichTextContentGenerator.preformatRCGContent((PDFDocument)this.pdfDocument, (InputStream)richText0Stream, (PDFFontSet)defaultFontSet, (RCGOptions)rcgOptions, (XObjectUseOptions)useOptions);
RegionAndAnnotInfo regionInfo = (RegionAndAnnotInfo)info.getRegionSizes().get(0);
double watermarkWidth = regionInfo.getRight() - regionInfo.getLeft();
richText0Stream.reset();
this.applyOptions = new XObjectApplyOptions();
double radians = Math.atan(this.height / this.width);
boolean isNan = Double.isNaN(radians);
this.applyOptions.setRotation(isNan ? 90.0 : Math.toDegrees(radians));
this.applyOptions.setOpacity(0.3);
this.applyOptions.setPosition(0.0, 0.0);
String richText1 = "<body style=\"font-size:" + 20.0 * (this.width / watermarkWidth) + "pt;font-family:Helvetica,sans-serif\"><p><span style=\"text-decoration:;font-family:Helvetica;color:#FF0000;font-weight:bold\">ADEP Document Services - PDF Generator Evaluation</span></p></body>";
ByteArrayInputStream richText1Stream = new ByteArrayInputStream(richText1.getBytes());
richText0Stream.reset();
this.form = RichTextContentGenerator.generateXObject((PDFDocument)this.pdfDocument, (InputStream)richText1Stream, (PDFFontSet)defaultFontSet, (RCGOptions)rcgOptions, (XObjectUseOptions)useOptions);
}
}
private static class WatermarkKey {
long width;
long height;
WatermarkKey(double inWidth, double inHeight) {
this.width = (long)Math.floor(inWidth * 1024.0);
this.height = (long)Math.floor(inHeight * 1024.0);
}
public WatermarkKey() {
this.width = -1;
this.height = -1;
}
public boolean equals(Object obj) {
if (!(obj instanceof WatermarkKey)) {
return false;
}
WatermarkKey in = (WatermarkKey)obj;
return in.width == this.width && in.height == this.height;
}
public int hashCode() {
return (int)(this.width & 0xFFFFFFFFL ^ this.height * 256 & 0xFFFFFFFFL);
}
}
}