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();  }
@Test public void createEmptyDocument() throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "emptyDocument.pdf")); pdfDoc.getDocumentInfo().setAuthor("Alexander Chingarev"). setCreator("iText 6"). setTitle("Empty iText 6 Document"); PdfPage page = pdfDoc.addNewPage(); page.flush(); pdfDoc.close(); PdfReader reader = new PdfReader(destinationFolder + "emptyDocument.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); Assert.assertEquals("Rebuilt", false, reader.hasRebuiltXref()); Assert.assertNotNull(pdfDocument.getPage(1)); String date = pdfDocument.getDocumentInfo().getPdfObject().getAsString(PdfName.CreationDate).getValue(); Calendar cl = PdfDate.decode(date); double diff = DateTimeUtil.getUtcMillisFromEpoch(null) - DateTimeUtil.getUtcMillisFromEpoch(cl); String message = "Unexpected creation date. Different from now is " + (float) diff / 1000 + "s"; Assert.assertTrue(message, diff < 5000); pdfDocument.close(); }
@Test public void useObjectForMultipleTimes1() throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "useObjectForMultipleTimes1.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); page.flush(); pdfDoc.getCatalog().getPdfObject().put(new PdfName("HelloWorld"), helloWorld); pdfDoc.close(); validateUseObjectForMultipleTimesTest(destinationFolder + "useObjectForMultipleTimes1.pdf"); }
@Test public void useObjectForMultipleTimes2() throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "useObjectForMultipleTimes2.pdf")); PdfDictionary helloWorld = (PdfDictionary) new PdfDictionary().makeIndirect(pdfDoc); helloWorld.put(new PdfName("Hello"), new PdfString("World")); helloWorld.flush(); PdfPage page = pdfDoc.addNewPage(); page.getPdfObject().put(new PdfName("HelloWorld"), helloWorld); page.flush(); pdfDoc.getCatalog().getPdfObject().put(new PdfName("HelloWorld"), helloWorld); pdfDoc.close(); validateUseObjectForMultipleTimesTest(destinationFolder + "useObjectForMultipleTimes2.pdf"); }
@Test public void useObjectForMultipleTimes3() throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "useObjectForMultipleTimes3.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); page.flush(); helloWorld.flush(); pdfDoc.getCatalog().getPdfObject().put(new PdfName("HelloWorld"), helloWorld); pdfDoc.close(); validateUseObjectForMultipleTimesTest(destinationFolder + "useObjectForMultipleTimes3.pdf"); }
@Test public void useObjectForMultipleTimes4() throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "useObjectForMultipleTimes4.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); page.flush(); pdfDoc.getCatalog().getPdfObject().put(new PdfName("HelloWorld"), helloWorld); helloWorld.flush(); pdfDoc.close(); validateUseObjectForMultipleTimesTest(destinationFolder + "useObjectForMultipleTimes4.pdf"); }
@Test public void copyObject1() throws IOException { PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject1_1.pdf")); PdfPage page1 = pdfDoc1.addNewPage(); page1.flush(); PdfDictionary catalog1 = pdfDoc1.getCatalog().getPdfObject(); PdfArray aDirect = new PdfArray(); ArrayList<PdfObject> tmpArray = new ArrayList<PdfObject>(2); tmpArray.add(new PdfNumber(1)); tmpArray.add(new PdfNumber(2)); 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)); aDirect.add(new PdfDictionary(tmpMap)); aDirect.add(new PdfName("name")); aDirect.add(new PdfNull()); aDirect.add(new PdfNumber(100)); aDirect.add(new PdfString("string")); catalog1.put(new PdfName("aDirect"), aDirect); PdfDocument pdfDoc2 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject1_2.pdf")); PdfPage page2 = pdfDoc2.addNewPage(); page2.flush(); PdfDictionary catalog2 = pdfDoc2.getCatalog().getPdfObject(); catalog2.put(new PdfName("aDirect"), aDirect.copyTo(pdfDoc2)); pdfDoc1.close(); pdfDoc2.close(); PdfReader reader = new PdfReader(destinationFolder + "copyObject1_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); Assert.assertEquals("Rebuilt", false, reader.hasRebuiltXref()); PdfDictionary catalog = pdfDocument.getCatalog().getPdfObject(); PdfArray a = (PdfArray) catalog.get(new PdfName("aDirect")); Assert.assertNotNull(a); Assert.assertEquals(1, ((PdfNumber) ((PdfArray) a.get(0)).get(0)).intValue()); Assert.assertEquals(2, ((PdfNumber) ((PdfArray) a.get(0)).get(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, ((PdfNumber) ((PdfDictionary) a.get(2)).get(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(); }
@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(); }
@Test public void copyObject3() throws IOException { { PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject3_1.pdf")); PdfPage page1 = pdfDoc1.addNewPage(); page1.flush(); PdfDictionary catalog1 = pdfDoc1.getCatalog().getPdfObject(); PdfArray arr1 = (PdfArray) new PdfArray().makeIndirect(pdfDoc1); PdfArray arr2 = (PdfArray) new PdfArray().makeIndirect(pdfDoc1); arr1.add(arr2); PdfDictionary dic1 = (PdfDictionary) new PdfDictionary().makeIndirect(pdfDoc1); arr2.add(dic1); PdfDictionary dic2 = (PdfDictionary) new PdfDictionary().makeIndirect(pdfDoc1); dic1.put(new PdfName("dic2"), dic2); PdfName arr1Name = new PdfName("arr1"); dic2.put(arr1Name, arr1); catalog1.put(arr1Name, arr1); pdfDoc1.close(); PdfDocument pdfDoc1R = new PdfDocument(new PdfReader(destinationFolder + "copyObject3_1.pdf")); arr1 = (PdfArray) pdfDoc1R.getCatalog().getPdfObject().get(arr1Name); PdfDocument pdfDoc2 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject3_2.pdf")); PdfPage page2 = pdfDoc2.addNewPage(); page2.flush(); PdfDictionary catalog2 = pdfDoc2.getCatalog().getPdfObject(); catalog2.put(arr1Name, arr1.copyTo(pdfDoc2)); pdfDoc1R.close(); pdfDoc2.close(); } { PdfReader reader = new PdfReader(destinationFolder + "copyObject3_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); Assert.assertEquals("Rebuilt", false, reader.hasRebuiltXref()); PdfDictionary catalog = pdfDocument.getCatalog().getPdfObject(); PdfArray arr1 = catalog.getAsArray(new PdfName("arr1")); PdfArray arr2 = arr1.getAsArray(0); PdfDictionary dic1 = arr2.getAsDictionary(0); PdfDictionary dic2 = dic1.getAsDictionary(new PdfName("dic2")); Assert.assertEquals(arr1, dic2.getAsArray(new PdfName("arr1"))); pdfDocument.close(); } }
@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(); }
@Test() public void copyObject7() throws IOException { String exceptionMessage = null; PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_1.pdf")); PdfDocument pdfDoc2 = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_2.pdf")); try { PdfPage page1 = pdfDoc1.addNewPage(); PdfDictionary directDict = new PdfDictionary(); PdfObject indirectDict = new PdfDictionary().makeIndirect(pdfDoc1); page1.getPdfObject().put(new PdfName("HelloWorldDirect"), directDict); page1.getPdfObject().put(new PdfName("HelloWorldIndirect"), indirectDict); PdfPage page2 = pdfDoc2.addNewPage(); page2.getPdfObject().put(new PdfName("HelloWorldDirect"), directDict.copyTo(pdfDoc2)); page2.getPdfObject().put(new PdfName("HelloWorldIndirect"), indirectDict.copyTo(pdfDoc2)); } catch (PdfException ex) { exceptionMessage = ex.getMessage(); } finally { pdfDoc1.close(); pdfDoc2.close(); } Assert.assertEquals(exceptionMessage, PdfException.CannotCopyIndirectObjectFromTheDocumentThatIsBeingWritten); }
@Test() public void copyObject8() throws IOException { String exceptionMessage = null; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_1.pdf")); try { PdfPage page1 = pdfDoc.addNewPage(); PdfDictionary directDict = new PdfDictionary(); PdfObject indirectDict = new PdfDictionary().makeIndirect(pdfDoc); page1.getPdfObject().put(new PdfName("HelloWorldDirect"), directDict); page1.getPdfObject().put(new PdfName("HelloWorldIndirect"), indirectDict); indirectDict.copyTo(null); } catch (PdfException ex) { exceptionMessage = ex.getMessage(); } finally { pdfDoc.close(); } Assert.assertEquals(exceptionMessage, PdfException.DocumentForCopyToCannotBeNull); }
@Test public void directInIndirectChain() throws IOException { String filename = destinationFolder + "directInIndirectChain.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename)); PdfArray level1 = new PdfArray(); level1.add(new PdfNumber(1).makeIndirect(pdfDoc)); PdfDictionary level2 = new PdfDictionary(); level1.add(level2); PdfArray level3 = new PdfArray(); level2.put(new PdfName("level3"), level3); level2.put(new PdfName("num"), new PdfNumber(2).makeIndirect(pdfDoc)); level3.add(new PdfNumber(3).makeIndirect(pdfDoc)); level3.add(new PdfNumber(3).makeIndirect(pdfDoc)); PdfDictionary level4 = new PdfDictionary(); level4.put(new PdfName("num"), new PdfNumber(4).makeIndirect(pdfDoc)); level3.add(level4); PdfPage page1 = pdfDoc.addNewPage(); page1.getPdfObject().put(new PdfName("test"), level1); pdfDoc.close(); PdfReader reader = new PdfReader(filename); PdfDocument pdfDocument = new PdfDocument(reader); Assert.assertEquals("Rebuilt", false, reader.hasRebuiltXref()); Assert.assertEquals("Page count", 1, pdfDocument.getNumberOfPages()); PdfDictionary page = pdfDocument.getPage(1).getPdfObject(); Assert.assertEquals(PdfName.Page, page.get(PdfName.Type)); pdfDocument.close(); }
@Test public void createPdfStreamByInputStream() throws IOException { String filename = destinationFolder + "createPdfStreamByInputStream.pdf"; PdfDocument document = new PdfDocument(new PdfWriter(filename)); document.getDocumentInfo().setAuthor("Alexander Chingarev"). setCreator("iText 6"). setTitle("Empty iText 6 Document"); PdfPage page = document.addNewPage(); page.flush(); String streamContent = "Some text content with strange symbols ∞²"; PdfStream stream = new PdfStream(document, new ByteArrayInputStream(streamContent.getBytes())); stream.flush(); int streamIndirectNumber = stream.getIndirectReference().getObjNumber(); document.close(); PdfReader reader6 = new PdfReader(filename); document = new PdfDocument(reader6); Assert.assertEquals("Rebuilt", false, reader6.hasRebuiltXref()); Assert.assertEquals("Fixed", false, reader6.hasFixedXref()); PdfStream pdfStream = (PdfStream) document.getXref().get(streamIndirectNumber).getRefersTo(); Assert.assertArrayEquals("Stream by InputStream", streamContent.getBytes(), pdfStream.getBytes()); document.close(); }