public class PDFObjectStreamTestCase {
@Test
public void testObjectStreamsEnabled() throws IOException {
- TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
PDFDocument doc = new PDFDocument("");
+ String out = buildObjectStreamsPDF(doc);
+ Assert.assertTrue(out.contains("<<\n /Type /ObjStm\n /N 3\n /First 15\n /Length 260\n>>\n"
+ + "stream\n8 0\n9 52\n4 121\n<<\n/Producer"));
+ }
+
+ @Test
+ public void testObjectStreamsWithEncryption() throws IOException {
+ PDFDocument doc = new PDFDocument("");
+ doc.setEncryption(new PDFEncryptionParams());
+ String out = buildObjectStreamsPDF(doc);
+ Assert.assertTrue(out.contains("<<\n /Type /ObjStm\n /N 3\n /First 16\n /Length 282\n>>\nstream"));
+ }
+
+ private String buildObjectStreamsPDF(PDFDocument doc) throws IOException {
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Map<String, List<String>> filterMap = new HashMap<>();
List<String> filterList = new ArrayList<>();
filterList.add("null");
gen.flushPDFDoc();
doc.outputTrailer(out);
Assert.assertTrue(out.toString().contains("/Subtype /Image"));
- Assert.assertTrue(out.toString().contains("<<\n /Type /ObjStm\n /N 3\n /First 15\n /Length 260\n>>\n"
- + "stream\n8 0\n9 52\n4 121\n<<\n/Producer"));
+ return out.toString();
}
}
public class PDFSigningTestCase {
@Test
- public void textFO() throws Exception {
+ public void testFO() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
- foToOutput(out, MimeConstants.MIME_PDF);
+ foToOutput(out, false);
+ String endStr = checkOutput(out);
+ Assert.assertTrue(endStr.contains("/FT /Sig\n"
+ + " /Type /Annot\n"
+ + " /Subtype /Widget\n"
+ + " /F 132\n"
+ + " /T (Signature1)\n"
+ + " /TU (Signature1)\n"
+ + " /Rect [0 0 0 0]"));
+ }
+
+ @Test
+ public void testWithObjectStreams() throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ foToOutput(out, true);
+ String endStr = checkOutput(out);
+ Assert.assertFalse(endStr.contains("/Subtype /Widget"));
+ }
+
+ private String checkOutput(ByteArrayOutputStream out) throws Exception {
StringTokenizer byteRange = new StringTokenizer(out.toString().split("/ByteRange ")[1]);
byteRange.nextToken();
int startOfContents = Integer.parseInt(byteRange.nextToken());
String endStr = new String(end);
Assert.assertTrue(endStr.contains(
"/ByteRange [0 " + startOfContents + " " + endOfContents + " " + sizeOfEnd + "]"));
- Assert.assertTrue(endStr.contains("/FT /Sig\n"
- + " /Type /Annot\n"
- + " /Subtype /Widget\n"
- + " /F 132\n"
- + " /T (Signature1)\n"
- + " /TU (Signature1)\n"
- + " /Rect [0 0 0 0]"));
+ return endStr;
}
- private void foToOutput(ByteArrayOutputStream out, String mimeFopIf) throws Exception {
- FopFactory fopFactory = getFopFactory();
+ private void foToOutput(ByteArrayOutputStream out, boolean objectStreams) throws Exception {
+ FopFactory fopFactory = getFopFactory(objectStreams);
FOUserAgent userAgent = fopFactory.newFOUserAgent();
- Fop fop = fopFactory.newFop(mimeFopIf, userAgent, out);
+ Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Source src = new StreamSource(LayoutMasterSetTestCase.class.getResourceAsStream("side-regions.fo"));
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
}
- private FopFactory getFopFactory() throws Exception {
+ private FopFactory getFopFactory(boolean objectStreams) throws Exception {
String pkcs = PDFSigningTestCase.class.getResource("keystore.pkcs12").toString();
String fopxconf = "<fop version=\"1.0\">\n"
+ " <renderers>\n"
- + " <renderer mime=\"application/pdf\">\n"
- + " <sign-params>\n"
+ + " <renderer mime=\"application/pdf\">\n";
+ if (objectStreams) {
+ fopxconf += "<use-object-streams>true</use-object-streams>";
+ }
+ fopxconf += " <sign-params>\n"
+ " <keystore>" + pkcs + "</keystore>\n"
+ " </sign-params>\n"
+ " </renderer>\n"