Search in sources :

Example 11 with PDPage

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();
}
Also used : PDAppearanceCharacteristicsDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) ArrayList(java.util.ArrayList) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 12 with PDPage

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);
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDAnnotationTextMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDAnnotationLink(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) PDAnnotationSquareCircle(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle) Before(org.junit.Before)

Example 13 with PDPage

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();
    }
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) COSName(com.tom_roush.pdfbox.cos.COSName) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) HashSet(java.util.HashSet)

Example 14 with PDPage

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();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) StandardProtectionPolicy(com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) AccessPermission(com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) File(java.io.File) Test(org.junit.Test)

Example 15 with PDPage

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...
}
Also used : PDNumberTreeNode(com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode) COSObjectable(com.tom_roush.pdfbox.pdmodel.common.COSObjectable) PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)

Aggregations

PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)55 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)24 File (java.io.File)23 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)13 Test (org.junit.Test)13 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)11 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)10 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 IOException (java.io.IOException)7 Bitmap (android.graphics.Bitmap)6 COSArray (com.tom_roush.pdfbox.cos.COSArray)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)5 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)5 COSBase (com.tom_roush.pdfbox.cos.COSBase)4 PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)4 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)4 Paint (android.graphics.Paint)3 COSName (com.tom_roush.pdfbox.cos.COSName)3