PdfCollectionItem extends PdfObjectWrapper<PdfDictionary> { public PdfCollectionItem setPrefix(String key, String prefix) { PdfName fieldName = new PdfName(key); PdfObject obj = getPdfObject().get(fieldName); if (obj == null) { throw new PdfException(PdfException.YouMustSetAValueBeforeAddingAPrefix); } PdfDictionary subItem = new PdfDictionary(); subItem.put(PdfName.D, obj); subItem.put(PdfName.P, new PdfString(prefix)); getPdfObject().put(fieldName, subItem); 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 addPrefixToEmptyFieldTest() { junitExpectedException.expect(PdfException.class); junitExpectedException.expectMessage(PdfException.YouMustSetAValueBeforeAddingAPrefix); final String fieldName = "fieldName"; final String fieldPrefix = "fieldPrefix"; 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.setPrefix(fieldName, fieldPrefix); }