PdfCollectionItem extends PdfObjectWrapper<PdfDictionary> { public PdfCollectionItem addItem(String key, String value) { PdfCollectionField field = schema.getField(key); getPdfObject().put(new PdfName(key), field.getValue(value)); return this; } PdfCollectionItem(PdfCollectionSchema schema); PdfCollectionItem addItem(String key, String value); void addItem(String key, PdfDate date); void addItem(String key, PdfNumber number); PdfCollectionItem setPrefix(String key, String prefix);  }
@Test public void addItemTest() { final String fieldName = "fieldName"; final String fieldValue = "fieldValue"; PdfDictionary pdfObject = new PdfDictionary(); pdfObject.put(PdfName.Subtype, PdfName.S); PdfCollectionField field = new PdfCollectionField(pdfObject); PdfCollectionSchema schema = new PdfCollectionSchema(); schema.addField(fieldName, field); PdfCollectionItem item = new PdfCollectionItem(schema); item.addItem(fieldName, fieldValue); Assert.assertEquals(fieldValue, item.getPdfObject().getAsString(new PdfName(fieldName)).getValue()); }
@Test public void addDateItemTest() { final String fieldName = "fieldName"; final String timeValueAsString = "D:19860426012347+04'00'"; PdfDictionary pdfObject = new PdfDictionary(); pdfObject.put(PdfName.Subtype, PdfName.D); PdfCollectionField field = new PdfCollectionField(pdfObject); PdfCollectionSchema schema = new PdfCollectionSchema(); schema.addField(fieldName, field); PdfCollectionItem item = new PdfCollectionItem(schema); item.addItem(fieldName, new PdfDate(PdfDate.decode(timeValueAsString))); Assert.assertTrue(((PdfString)field.getValue(timeValueAsString)).getValue().startsWith("D:1986")); }
@Test public void dontAddDateItemToAnotherSubTypeFieldTest() { final String fieldName = "fieldName"; final String timeValueAsString = "D:19860426012347+04'00'"; PdfDictionary pdfObject = new PdfDictionary(); pdfObject.put(PdfName.Subtype, PdfName.F); PdfCollectionField field = new PdfCollectionField(pdfObject); PdfCollectionSchema schema = new PdfCollectionSchema(); schema.addField(fieldName, field); PdfCollectionItem item = new PdfCollectionItem(schema); item.addItem(fieldName, new PdfDate(PdfDate.decode(timeValueAsString))); Assert.assertNull(item.getPdfObject().getAsString(new PdfName(fieldName))); }
@Test public void addNumberItemTest() { final String fieldName = "fieldName"; final double numberValue = 0.1234; PdfDictionary pdfObject = new PdfDictionary(); pdfObject.put(PdfName.Subtype, PdfName.N); PdfCollectionField field = new PdfCollectionField(pdfObject); PdfCollectionSchema schema = new PdfCollectionSchema(); schema.addField(fieldName, field); PdfCollectionItem item = new PdfCollectionItem(schema); item.addItem(fieldName, new PdfNumber(numberValue)); Assert.assertEquals(numberValue, item.getPdfObject().getAsNumber(new PdfName(fieldName)).getValue(), 0.0001); }
@Test public void dontAddNumberItemToAnotherSubTypeFieldTest() { final String fieldName = "fieldName"; final double numberValue = 0.1234; PdfDictionary pdfObject = new PdfDictionary(); pdfObject.put(PdfName.Subtype, PdfName.F); PdfCollectionField field = new PdfCollectionField(pdfObject); PdfCollectionSchema schema = new PdfCollectionSchema(); schema.addField(fieldName, field); PdfCollectionItem item = new PdfCollectionItem(schema); item.addItem(fieldName, new PdfNumber(numberValue)); Assert.assertNull(item.getPdfObject().getAsString(new PdfName(fieldName))); }