@Test public void copyObject6() throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_1.pdf")); PdfDictionary helloWorld = (PdfDictionary) new PdfDictionary().makeIndirect(pdfDoc); helloWorld.put(new PdfName("Hello"), new PdfString("World")); PdfPage page = pdfDoc.addNewPage(); page.getPdfObject().put(new PdfName("HelloWorld"), helloWorld); pdfDoc.close(); pdfDoc = new PdfDocument(new PdfReader(destinationFolder + "copyObject6_1.pdf")); helloWorld = (PdfDictionary) pdfDoc.getPage(1).getPdfObject().get(new PdfName("HelloWorld")); PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_2.pdf")); PdfPage page1 = pdfDoc1.addNewPage(); page1.getPdfObject().put(new PdfName("HelloWorldCopy1"), helloWorld.copyTo(pdfDoc1)); page1.getPdfObject().put(new PdfName("HelloWorldCopy2"), helloWorld.copyTo(pdfDoc1, true)); page1.getPdfObject().put(new PdfName("HelloWorldCopy3"), helloWorld.copyTo(pdfDoc1, false)); page1.flush(); pdfDoc.close(); pdfDoc1.close(); PdfReader reader = new PdfReader(destinationFolder + "copyObject6_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); Assert.assertEquals("Rebuilt", false, reader.hasRebuiltXref()); PdfObject obj1 = pdfDocument.getPage(1).getPdfObject().get(new PdfName("HelloWorldCopy1")); PdfIndirectReference ref1 = obj1.getIndirectReference(); Assert.assertEquals(6, ref1.objNr); Assert.assertEquals(0, ref1.genNr); PdfObject obj2 = pdfDocument.getPage(1).getPdfObject().get(new PdfName("HelloWorldCopy2")); PdfIndirectReference ref2 = obj2.getIndirectReference(); Assert.assertEquals(7, ref2.getObjNumber()); Assert.assertEquals(0, ref2.getGenNumber()); PdfObject obj3 = pdfDocument.getPage(1).getPdfObject().get(new PdfName("HelloWorldCopy3")); PdfIndirectReference ref3 = obj3.getIndirectReference(); Assert.assertEquals(7, ref3.getObjNumber()); Assert.assertEquals(0, ref3.getGenNumber()); 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();  }