use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestCheckBox method testCheckboxPDModel.
/**
* This will test the checkbox PDModel.
*
* @throws IOException If there is an error creating the field.
*/
public void testCheckboxPDModel() throws IOException {
PDDocument doc = null;
try {
doc = new PDDocument();
PDAcroForm form = new PDAcroForm(doc);
PDCheckBox checkBox = new PDCheckBox(form);
// test that there are no nulls returned for an empty field
// only specific methods are tested here
assertNotNull(checkBox.getExportValues());
assertNotNull(checkBox.getValue());
// Test setting/getting option values - the dictionaries Opt entry
List<String> options = new ArrayList<String>();
options.add("Value01");
options.add("Value02");
checkBox.setExportValues(options);
COSArray optItem = (COSArray) checkBox.getCOSObject().getItem(COSName.OPT);
// assert that the values have been correctly set
assertNotNull(checkBox.getCOSObject().getItem(COSName.OPT));
assertEquals(optItem.size(), 2);
assertEquals(options.get(0), optItem.getString(0));
// assert that the values can be retrieved correctly
List<String> retrievedOptions = checkBox.getExportValues();
assertEquals(retrievedOptions.size(), 2);
assertEquals(retrievedOptions, options);
// assert that the Opt entry is removed
checkBox.setExportValues(null);
assertNull(checkBox.getCOSObject().getItem(COSName.OPT));
// if there is no Opt entry an empty List shall be returned
assertTrue(checkBox.getExportValues().isEmpty());
} finally {
if (doc != null) {
doc.close();
}
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestCheckBox method testCheckBoxNoAppearance.
/**
* PDFBOX-4366: Create and test a checkbox with no /AP. The created file works with Adobe Reader!
*
* @throws IOException
*/
public void testCheckBoxNoAppearance() throws IOException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(doc);
// need this or it won't appear on Adobe Reader
acroForm.setNeedAppearances(true);
doc.getDocumentCatalog().setAcroForm(acroForm);
List<PDField> fields = new ArrayList<PDField>();
PDCheckBox checkBox = new PDCheckBox(acroForm);
checkBox.setPartialName("checkbox");
PDAnnotationWidget widget = checkBox.getWidgets().get(0);
widget.setRectangle(new PDRectangle(50, 600, 100, 100));
PDBorderStyleDictionary bs = new PDBorderStyleDictionary();
bs.setStyle(PDBorderStyleDictionary.STYLE_SOLID);
bs.setWidth(1);
COSDictionary acd = new COSDictionary();
PDAppearanceCharacteristicsDictionary ac = new PDAppearanceCharacteristicsDictionary(acd);
ac.setBackground(new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE));
ac.setBorderColour(new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE));
// 4 is checkmark, 8 is cross
ac.setNormalCaption("4");
widget.setAppearanceCharacteristics(ac);
widget.setBorderStyle(bs);
checkBox.setValue("Off");
fields.add(checkBox);
page.getAnnotations().add(widget);
acroForm.setFields(fields);
assertEquals("Off", checkBox.getValue());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestFields method testWidgetMissingRect.
/**
* This will test the handling of a widget with a missing (required) /Rect entry.
*
* @throws IOException If there is an error loading the form or the field.
*/
public void testWidgetMissingRect() throws IOException {
PDDocument doc = null;
try {
doc = PDDocument.load(new File(PATH_OF_PDF));
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
PDTextField textField = (PDTextField) form.getField("TextField-DefaultValue");
PDAnnotationWidget widget = textField.getWidgets().get(0);
// initially there is an Appearance Entry in the form
assertNotNull(widget.getCOSObject().getDictionaryObject(COSName.AP));
widget.getCOSObject().removeItem(COSName.RECT);
textField.setValue("field value");
// There shall be no appearance entry if there is no /Rect to
// behave as Adobe Acrobat does
assertNull(widget.getCOSObject().getDictionaryObject(COSName.AP));
} finally {
if (doc != null) {
doc.close();
}
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestFields method testAcroFormsBasicFields.
/**
* This will test some form fields functionality based with
* a sample form.
*
* @throws IOException If there is an error creating the field.
*/
public void testAcroFormsBasicFields() throws IOException {
PDDocument doc = null;
try {
doc = PDDocument.load(new File(PATH_OF_PDF));
// get and assert that there is a form
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
assertNotNull(form);
// assert that there is no value, set the field value and
// ensure it has been set
PDTextField textField = (PDTextField) form.getField("TextField");
assertNull(textField.getCOSObject().getItem(COSName.V));
textField.setValue("field value");
assertNotNull(textField.getCOSObject().getItem(COSName.V));
assertEquals(textField.getValue(), "field value");
// assert when setting to null the key has also been removed
assertNotNull(textField.getCOSObject().getItem(COSName.V));
textField.setValue(null);
assertNull(textField.getCOSObject().getItem(COSName.V));
// get the TextField with a DV entry
textField = (PDTextField) form.getField("TextField-DefaultValue");
assertNotNull(textField);
assertEquals(textField.getDefaultValue(), "DefaultValue");
assertEquals(textField.getDefaultValue(), ((COSString) textField.getCOSObject().getDictionaryObject(COSName.DV)).getString());
assertEquals(textField.getDefaultAppearance(), "/Helv 12 Tf 0 g");
// get a rich text field with a DV entry
textField = (PDTextField) form.getField("RichTextField-DefaultValue");
assertNotNull(textField);
assertEquals(textField.getDefaultValue(), "DefaultValue");
assertEquals(textField.getDefaultValue(), ((COSString) textField.getCOSObject().getDictionaryObject(COSName.DV)).getString());
assertEquals(textField.getValue(), "DefaultValue");
assertEquals(textField.getDefaultAppearance(), "/Helv 12 Tf 0 g");
assertEquals(textField.getDefaultStyleString(), "font: Helvetica,sans-serif 12.0pt; text-align:left; color:#000000 ");
// do not test for the full content as this is a rather long xml string
assertEquals(textField.getRichTextValue().length(), 338);
// get a rich text field with a text stream for the value
textField = (PDTextField) form.getField("LongRichTextField");
assertNotNull(textField);
assertEquals(textField.getCOSObject().getDictionaryObject(COSName.V).getClass().getName(), "com.tom_roush.pdfbox.cos.COSStream");
assertEquals(textField.getValue().length(), 145396);
} finally {
if (doc != null) {
doc.close();
}
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestFields method testFlags.
/**
* This will test setting field flags on the PDField.
*
* @throws IOException If there is an error creating the field.
*/
public void testFlags() throws IOException {
PDDocument doc = null;
try {
doc = new PDDocument();
PDAcroForm form = new PDAcroForm(doc);
PDTextField textBox = new PDTextField(form);
// assert that default is false.
assertFalse(textBox.isComb());
// try setting and clearing a single field
textBox.setComb(true);
assertTrue(textBox.isComb());
textBox.setComb(false);
assertFalse(textBox.isComb());
// try setting and clearing multiple fields
textBox.setComb(true);
textBox.setDoNotScroll(true);
assertTrue(textBox.isComb());
assertTrue(textBox.doNotScroll());
textBox.setComb(false);
textBox.setDoNotScroll(false);
assertFalse(textBox.isComb());
assertFalse(textBox.doNotScroll());
// assert that setting a field to false multiple times works
textBox.setComb(false);
assertFalse(textBox.isComb());
textBox.setComb(false);
assertFalse(textBox.isComb());
// assert that setting a field to true multiple times works
textBox.setComb(true);
assertTrue(textBox.isComb());
textBox.setComb(true);
assertTrue(textBox.isComb());
} finally {
if (doc != null) {
doc.close();
}
}
}
Aggregations