@Test public void copyObject2() throws IOException { PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject2_1.pdf")); PdfPage page1 = pdfDoc1.addNewPage(); page1.flush(); PdfDictionary catalog1 = pdfDoc1.getCatalog().getPdfObject(); PdfName aDirectName = new PdfName("aDirect"); PdfArray aDirect = (PdfArray) new PdfArray().makeIndirect(pdfDoc1); ArrayList<PdfObject> tmpArray = new ArrayList<PdfObject>(2); tmpArray.add(new PdfNumber(1)); tmpArray.add(new PdfNumber(2).makeIndirect(pdfDoc1)); aDirect.add(new PdfArray(tmpArray)); aDirect.add(new PdfBoolean(true)); TreeMap<PdfName, PdfObject> tmpMap = new TreeMap<PdfName, PdfObject>(); tmpMap.put(new PdfName("one"), new PdfNumber(1)); tmpMap.put(new PdfName("two"), new PdfNumber(2).makeIndirect(pdfDoc1)); aDirect.add(new PdfDictionary(tmpMap)); aDirect.add(new PdfName("name")); aDirect.add(new PdfNull().makeIndirect(pdfDoc1)); aDirect.add(new PdfNumber(100)); aDirect.add(new PdfString("string")); catalog1.put(aDirectName, aDirect); pdfDoc1.close(); PdfDocument pdfDoc1R = new PdfDocument(new PdfReader(destinationFolder + "copyObject2_1.pdf")); aDirect = (PdfArray) pdfDoc1R.getCatalog().getPdfObject().get(aDirectName); PdfDocument pdfDoc2 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject2_2.pdf")); PdfPage page2 = pdfDoc2.addNewPage(); page2.flush(); PdfDictionary catalog2 = pdfDoc2.getCatalog().getPdfObject(); catalog2.put(aDirectName, aDirect.copyTo(pdfDoc2)); pdfDoc1R.close(); pdfDoc2.close(); PdfReader reader = new PdfReader(destinationFolder + "copyObject2_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); Assert.assertEquals("Rebuilt", false, reader.hasRebuiltXref()); PdfDictionary catalog = pdfDocument.getCatalog().getPdfObject(); PdfArray a = catalog.getAsArray(new PdfName("aDirect")); Assert.assertNotNull(a); Assert.assertEquals(1, ((PdfNumber) ((PdfArray) a.get(0)).get(0)).intValue()); Assert.assertEquals(2, ((PdfArray) a.get(0)).getAsNumber(1).intValue()); Assert.assertEquals(true, ((PdfBoolean) a.get(1)).getValue()); Assert.assertEquals(1, ((PdfNumber) ((PdfDictionary) a.get(2)).get(new PdfName("one"))).intValue()); Assert.assertEquals(2, ((PdfDictionary) a.get(2)).getAsNumber(new PdfName("two")).intValue()); Assert.assertEquals(new PdfName("name"), a.get(3)); Assert.assertTrue(a.get(4).isNull()); Assert.assertEquals(100, ((PdfNumber) a.get(5)).intValue()); Assert.assertEquals("string", ((PdfString) a.get(6)).toUnicodeString()); pdfDocument.close(); }
@Override public void close() throws IOException { try { super.close(); } finally { try { if (duplicateStream != null) { duplicateStream.close(); } } catch (Exception ex) { Logger logger = LoggerFactory.getLogger(PdfWriter.class); logger.error("Closing of the duplicatedStream failed.", ex); } } }
PdfWriter extends PdfOutputStream implements Serializable { @Override public void close() throws IOException { try { super.close(); } finally { try { if (duplicateStream != null) { duplicateStream.close(); } } catch (Exception ex) { Logger logger = LoggerFactory.getLogger(PdfWriter.class); logger.error("Closing of the duplicatedStream failed.", ex); } } } }
PdfWriter extends PdfOutputStream implements Serializable { @Override public void close() throws IOException { try { super.close(); } finally { try { if (duplicateStream != null) { duplicateStream.close(); } } catch (Exception ex) { Logger logger = LoggerFactory.getLogger(PdfWriter.class); logger.error("Closing of the duplicatedStream failed.", ex); } } } PdfWriter(java.io.File file);  PdfWriter(java.io.OutputStream os);  PdfWriter(java.io.OutputStream os, WriterProperties properties);  PdfWriter(String filename);  PdfWriter(String filename, WriterProperties properties); }
PdfWriter extends PdfOutputStream implements Serializable { @Override public void close() throws IOException { try { super.close(); } finally { try { if (duplicateStream != null) { duplicateStream.close(); } } catch (Exception ex) { Logger logger = LoggerFactory.getLogger(PdfWriter.class); logger.error("Closing of the duplicatedStream failed.", ex); } } } PdfWriter(java.io.File file);  PdfWriter(java.io.OutputStream os);  PdfWriter(java.io.OutputStream os, WriterProperties properties);  PdfWriter(String filename);  PdfWriter(String filename, WriterProperties properties); boolean isFullCompression(); int getCompressionLevel(); PdfWriter setCompressionLevel(int compressionLevel); PdfWriter setSmartMode(boolean smartMode); @Override void write(int b); @Override void write(byte[] b); @Override void write(byte[] b, int off, int len); @Override void close(); }
PdfWriter extends PdfOutputStream implements Serializable { @Override public void close() throws IOException { try { super.close(); } finally { try { if (duplicateStream != null) { duplicateStream.close(); } } catch (Exception ex) { Logger logger = LoggerFactory.getLogger(PdfWriter.class); logger.error("Closing of the duplicatedStream failed.", ex); } } } PdfWriter(java.io.File file);  PdfWriter(java.io.OutputStream os);  PdfWriter(java.io.OutputStream os, WriterProperties properties);  PdfWriter(String filename);  PdfWriter(String filename, WriterProperties properties); boolean isFullCompression(); int getCompressionLevel(); PdfWriter setCompressionLevel(int compressionLevel); PdfWriter setSmartMode(boolean smartMode); @Override void write(int b); @Override void write(byte[] b); @Override void write(byte[] b, int off, int len); @Override void close();  }