Search in sources :

Example 11 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class PDButtonTest method setUp.

@Before
public void setUp() throws IOException {
    document = new PDDocument();
    acroForm = new PDAcroForm(document);
    acrobatDocument = PDDocument.load(new File(IN_DIR, NAME_OF_PDF));
    acrobatAcroForm = acrobatDocument.getDocumentCatalog().getAcroForm();
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) Before(org.junit.Before)

Example 12 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class PDButtonTest method testOptionsAndNamesNotNumbers.

@Test
public /**
 * PDFBOX-3682
 *
 * Test a radio button with options.
 * Special handling for a radio button with /Opt and the On state not being named
 * after the index.
 *
 * @throws IOException
 */
void testOptionsAndNamesNotNumbers() {
    File file;
    PDDocument pdfDocument = null;
    try {
        file = new File(TARGET_PDF_DIR, "PDFBOX-3682.pdf");
        assumeTrue(file.exists());
        pdfDocument = PDDocument.load(file);
        pdfDocument.getDocumentCatalog().getAcroForm().getField("RadioButton").setValue("c");
        PDRadioButton radioButton = (PDRadioButton) pdfDocument.getDocumentCatalog().getAcroForm().getField("RadioButton");
        radioButton.setValue("c");
        // test that the old behavior is now invalid
        assertFalse("This shall no longer be 2", "2".equals(radioButton.getValueAsString()));
        assertFalse("This shall no longer be 2", "2".equals(radioButton.getWidgets().get(2).getCOSObject().getNameAsString(COSName.AS)));
        // test for the correct behavior
        assertTrue("This shall be c", "c".equals(radioButton.getValueAsString()));
        assertTrue("This shall be c", "c".equals(radioButton.getWidgets().get(2).getCOSObject().getNameAsString(COSName.AS)));
    } catch (IOException e) {
        fail("Unexpected IOException " + e.getMessage());
    } finally {
        if (pdfDocument != null) {
            try {
                pdfDocument.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 13 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class MainActivity method fillForm.

/**
 * Fills in a PDF form and saves the result
 */
public void fillForm(View v) {
    try {
        // Load the document and get the AcroForm
        PDDocument document = PDDocument.load(assetManager.open("FormTest.pdf"));
        PDDocumentCatalog docCatalog = document.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        // Fill the text field
        PDTextField field = (PDTextField) acroForm.getField("TextField");
        field.setValue("Filled Text Field");
        // Optional: don't allow this field to be edited
        field.setReadOnly(true);
        PDField checkbox = acroForm.getField("Checkbox");
        ((PDCheckBox) checkbox).check();
        PDField radio = acroForm.getField("Radio");
        ((PDRadioButton) radio).setValue("Second");
        PDField listbox = acroForm.getField("ListBox");
        List<Integer> listValues = new ArrayList<>();
        listValues.add(1);
        listValues.add(2);
        ((PDListBox) listbox).setSelectedOptionsIndex(listValues);
        PDField dropdown = acroForm.getField("Dropdown");
        ((PDComboBox) dropdown).setValue("Hello");
        String path = root.getAbsolutePath() + "/FilledForm.pdf";
        tv.setText("Saved filled form to " + path);
        document.save(path);
        document.close();
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while filling form fields", e);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) PDCheckBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDCheckBox) ArrayList(java.util.ArrayList) PDComboBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDComboBox) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) IOException(java.io.IOException) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) PDRadioButton(com.tom_roush.pdfbox.pdmodel.interactive.form.PDRadioButton) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDTextField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDTextField) PDListBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDListBox)

Example 14 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class MainActivity method stripText.

/**
 * Strips the text from a PDF and displays the text on screen
 */
public void stripText(View v) {
    String parsedText = null;
    PDDocument document = null;
    try {
        document = PDDocument.load(assetManager.open("Hello.pdf"));
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while loading document to strip", e);
    }
    try {
        PDFTextStripper pdfStripper = new PDFTextStripper();
        pdfStripper.setStartPage(0);
        pdfStripper.setEndPage(1);
        parsedText = "Parsed text: " + pdfStripper.getText(document);
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while stripping text", e);
    } finally {
        try {
            if (document != null)
                document.close();
        } catch (IOException e) {
            Log.e("PdfBox-Android-Sample", "Exception thrown while closing document", e);
        }
    }
    tv.setText(parsedText);
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) IOException(java.io.IOException) PDFTextStripper(com.tom_roush.pdfbox.text.PDFTextStripper)

Example 15 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class PDStructureElementTest method testPDFBox4197.

/**
 * PDFBOX-4197: test that object references in array attributes of a PDStructureElement are caught.
 *
 * @throws IOException
 */
@Test
public // TODO: PdfBox-Android - provide test file
void testPDFBox4197() throws IOException {
    File testFile = new File(TARGETPDFDIR, "PDFBOX-4197.pdf");
    assumeTrue(testFile.exists());
    PDDocument doc = PDDocument.load(testFile);
    PDStructureTreeRoot structureTreeRoot = doc.getDocumentCatalog().getStructureTreeRoot();
    Set<Revisions<PDAttributeObject>> attributeSet = new HashSet<Revisions<PDAttributeObject>>();
    checkElement(structureTreeRoot.getK(), attributeSet);
    doc.close();
    // collect attributes and check their count.
    Assert.assertEquals(117, attributeSet.size());
    int cnt = 0;
    for (Revisions<PDAttributeObject> attributes : attributeSet) {
        cnt += attributes.size();
    }
    // this one was 105 before PDFBOX-4197 was fixed
    Assert.assertEquals(111, cnt);
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)137 File (java.io.File)80 Test (org.junit.Test)69 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)22 InputStream (java.io.InputStream)21 Bitmap (android.graphics.Bitmap)18 IOException (java.io.IOException)14 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)11 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)11 ArrayList (java.util.ArrayList)11 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)9 FileOutputStream (java.io.FileOutputStream)9 COSArray (com.tom_roush.pdfbox.cos.COSArray)8 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)7 Paint (android.graphics.Paint)6