use of com.tom_roush.pdfbox.pdmodel.PDPage 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.PDPage in project PdfBox-Android by TomRoush.
the class COSArrayListTest method setUp.
/*
* Create thre new different annotations an add them to the Java List/Array as
* well as PDFBox List/Array implementations.
*/
@Before
public void setUp() throws Exception {
annotationsList = new ArrayList<PDAnnotation>();
PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
PDAnnotationLink txtLink = new PDAnnotationLink();
PDAnnotationSquareCircle aCircle = new PDAnnotationSquareCircle(PDAnnotationSquareCircle.SUB_TYPE_CIRCLE);
annotationsList.add(txtMark);
annotationsList.add(txtLink);
annotationsList.add(aCircle);
assertTrue(annotationsList.size() == 3);
tbcAnnotationsList = new ArrayList<PDAnnotation>();
tbcAnnotationsList.add(txtMark);
tbcAnnotationsList.add(txtLink);
tbcAnnotationsList.add(aCircle);
assertTrue(tbcAnnotationsList.size() == 3);
annotationsArray = new COSArray();
annotationsArray.add(txtMark);
annotationsArray.add(txtLink);
annotationsArray.add(aCircle);
assertTrue(annotationsArray.size() == 3);
tbcAnnotationsArray = new COSBase[3];
tbcAnnotationsArray[0] = txtMark.getCOSObject();
tbcAnnotationsArray[1] = txtLink.getCOSObject();
tbcAnnotationsArray[2] = aCircle.getCOSObject();
assertTrue(tbcAnnotationsArray.length == 3);
// add the annotations to the page
pdPage = new PDPage();
pdPage.setAnnotations(annotationsList);
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class TestOptionalContentGroups method testOCGConsumption.
/**
* Tests OCG functions on a loaded PDF.
* @throws Exception if an error occurs
*/
public void testOCGConsumption() throws Exception {
File pdfFile = new File(testResultsDir, "ocg-generation.pdf");
if (!pdfFile.exists()) {
testOCGGeneration();
}
PDDocument doc = PDDocument.load(pdfFile);
try {
assertEquals(1.5f, doc.getVersion());
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDPage page = doc.getPage(0);
PDResources resources = page.getResources();
COSName mc0 = COSName.getPDFName("oc1");
PDOptionalContentGroup ocg = (PDOptionalContentGroup) resources.getProperties(mc0);
assertNotNull(ocg);
assertEquals("background", ocg.getName());
assertNull(resources.getProperties(COSName.getPDFName("inexistent")));
PDOptionalContentProperties ocgs = catalog.getOCProperties();
assertEquals(BaseState.ON, ocgs.getBaseState());
Set<String> names = new java.util.HashSet<String>(Arrays.asList(ocgs.getGroupNames()));
assertEquals(3, names.size());
assertTrue(names.contains("background"));
assertTrue(ocgs.isGroupEnabled("background"));
assertTrue(ocgs.isGroupEnabled("enabled"));
assertFalse(ocgs.isGroupEnabled("disabled"));
ocgs.setGroupEnabled("background", false);
assertFalse(ocgs.isGroupEnabled("background"));
PDOptionalContentGroup background = ocgs.getGroup("background");
assertEquals(ocg.getName(), background.getName());
assertNull(ocgs.getGroup("inexistent"));
Collection<PDOptionalContentGroup> coll = ocgs.getOptionalContentGroups();
assertEquals(3, coll.size());
Set<String> nameSet = new HashSet<String>();
for (PDOptionalContentGroup ocg2 : coll) {
nameSet.add(ocg2.getName());
}
assertTrue(nameSet.contains("background"));
assertTrue(nameSet.contains("enabled"));
assertTrue(nameSet.contains("disabled"));
} finally {
doc.close();
}
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class TestSymmetricKeyEncryption method testPDFBox4453.
/**
* PDFBOX-4453: verify that identical encrypted strings are really decrypted each.
*
* @throws IOException
*/
@Test
public void testPDFBox4453() throws IOException {
final int TESTCOUNT = 1000;
File file = new File(testResultsDir, "PDFBOX-4453.pdf");
PDDocument doc = new PDDocument();
doc.addPage(new PDPage());
for (int i = 0; i < TESTCOUNT; ++i) {
// strings must be in different dictionaries so that the actual
// encryption key changes
COSDictionary dict = new COSDictionary();
doc.getPage(0).getCOSObject().setItem(COSName.getPDFName("_Test-" + i), dict);
// need two different keys so that there are both encrypted and decrypted COSStrings
// with value "0"
dict.setString("key1", "3");
dict.setString("key2", "0");
}
// RC4-40
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "", new AccessPermission());
spp.setEncryptionKeyLength(40);
spp.setPreferAES(false);
doc.protect(spp);
doc.save(file);
doc.close();
doc = PDDocument.load(file);
Assert.assertTrue(doc.isEncrypted());
for (int i = 0; i < TESTCOUNT; ++i) {
COSDictionary dict = doc.getPage(0).getCOSObject().getCOSDictionary(COSName.getPDFName("_Test-" + i));
assertEquals("3", dict.getString("key1"));
assertEquals("0", dict.getString("key2"));
}
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method checkWithNumberTree.
/**
* PDFBOX-4408: Check that /StructParents values from pages and /StructParent values from
* annotations are found in the /ParentTree.
*
* @param document
*/
void checkWithNumberTree(PDDocument document) throws IOException {
PDDocumentCatalog documentCatalog = document.getDocumentCatalog();
PDNumberTreeNode parentTree = documentCatalog.getStructureTreeRoot().getParentTree();
Map<Integer, COSObjectable> numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
Set<Integer> keySet = numberTreeAsMap.keySet();
PDAcroForm acroForm = documentCatalog.getAcroForm();
if (acroForm != null) {
for (PDField field : acroForm.getFieldTree()) {
for (PDAnnotationWidget widget : field.getWidgets()) {
if (widget.getStructParent() >= 0) {
assertTrue("field '" + field.getFullyQualifiedName() + "' /StructParent " + widget.getStructParent() + " missing in /ParentTree", keySet.contains(widget.getStructParent()));
}
}
}
}
for (PDPage page : document.getPages()) {
if (page.getStructParents() >= 0) {
assertTrue(keySet.contains(page.getStructParents()));
}
for (PDAnnotation ann : page.getAnnotations()) {
if (ann.getStructParent() >= 0) {
assertTrue("/StructParent " + ann.getStructParent() + " missing in /ParentTree", keySet.contains(ann.getStructParent()));
}
}
}
// might also test image and form dictionaries...
}
Aggregations