Search in sources :

Example 56 with PDRectangle

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

the class PDDocument method prepareNonVisibleSignature.

private void prepareNonVisibleSignature(PDSignatureField signatureField) {
    // "Signature fields that are not intended to be visible shall
    // have an annotation rectangle that has zero height and width."
    // Set rectangle for non-visual signature to rectangle array [ 0 0 0 0 ]
    signatureField.getWidgets().get(0).setRectangle(new PDRectangle());
    // The visual appearance must also exist for an invisible signature but may be empty.
    PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
    PDAppearanceStream appearanceStream = new PDAppearanceStream(this);
    appearanceStream.setBBox(new PDRectangle());
    appearanceDictionary.setNormalAppearance(appearanceStream);
    signatureField.getWidgets().get(0).setAppearance(appearanceDictionary);
}
Also used : PDAppearanceDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 57 with PDRectangle

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

the class PDDocument method assignSignatureRectangle.

private void assignSignatureRectangle(PDSignatureField signatureField, COSDictionary annotDict) {
    // Read and set the rectangle for visual signature
    COSArray rectArray = (COSArray) annotDict.getDictionaryObject(COSName.RECT);
    PDRectangle rect = new PDRectangle(rectArray);
    PDRectangle existingRectangle = signatureField.getWidgets().get(0).getRectangle();
    // in case of an existing field keep the original rect
    if (existingRectangle == null || existingRectangle.getCOSArray().size() != 4) {
        signatureField.getWidgets().get(0).setRectangle(rect);
    }
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 58 with PDRectangle

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

the class PDFormXObject method getBBox.

/**
 * An array of four numbers in the form coordinate system (see below),
 * giving the coordinates of the left, bottom, right, and top edges, respectively,
 * of the form XObject's bounding box.
 * These boundaries are used to clip the form XObject and to determine its size for caching.
 * @return The BBox of the form.
 */
@Override
public PDRectangle getBBox() {
    PDRectangle retval = null;
    COSArray array = (COSArray) getCOSObject().getDictionaryObject(COSName.BBOX);
    if (array != null) {
        retval = new PDRectangle(array);
    }
    return retval;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 59 with PDRectangle

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

the class PDAcroFormTest method testBadDA.

/**
 * PDFBOX-4235: a bad /DA string should not result in an NPE.
 *
 * @throws IOException
 */
@Test
public void testBadDA() throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    PDAcroForm acroForm = new PDAcroForm(document);
    doc.getDocumentCatalog().setAcroForm(acroForm);
    acroForm.setDefaultResources(new PDResources());
    PDTextField textBox = new PDTextField(acroForm);
    textBox.setPartialName("SampleField");
    // https://stackoverflow.com/questions/50609478/
    // "tf" is a typo, should have been "Tf" and this results that no font is chosen
    textBox.setDefaultAppearance("/Helv 0 tf 0 g");
    acroForm.getFields().add(textBox);
    PDAnnotationWidget widget = textBox.getWidgets().get(0);
    PDRectangle rect = new PDRectangle(50, 750, 200, 20);
    widget.setRectangle(rect);
    widget.setPage(page);
    page.getAnnotations().add(widget);
    try {
        textBox.setValue("huhu");
    } catch (IllegalArgumentException ex) {
        return;
    } finally {
        doc.close();
    }
    fail("IllegalArgumentException should have been thrown");
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) Test(org.junit.Test)

Example 60 with PDRectangle

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

the class LayerUtility method importPageAsForm.

/**
 * Imports a page from some PDF file as a Form XObject so it can be placed on another page
 * in the target document.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before invoking the Form XObject to
 * make sure that the graphics state is reset.
 *
 * @param sourceDoc the source PDF document that contains the page to be copied
 * @param page the page in the source PDF document to be copied
 * @return a Form XObject containing the original page's content
 * @throws IOException if an I/O error occurs
 */
public PDFormXObject importPageAsForm(PDDocument sourceDoc, PDPage page) throws IOException {
    importOcProperties(sourceDoc);
    PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE);
    PDFormXObject form = new PDFormXObject(newStream);
    // Copy resources
    PDResources pageRes = page.getResources();
    PDResources formRes = new PDResources();
    cloner.cloneMerge(pageRes, formRes);
    form.setResources(formRes);
    // Transfer some values from page to form
    transferDict(page.getCOSObject(), form.getCOSObject(), PAGE_TO_FORM_FILTER, true);
    Matrix matrix = form.getMatrix();
    AffineTransform at = matrix.createAffineTransform();
    PDRectangle mediaBox = page.getMediaBox();
    PDRectangle cropBox = page.getCropBox();
    PDRectangle viewBox = (cropBox != null ? cropBox : mediaBox);
    // Handle the /Rotation entry on the page dict
    int rotation = page.getRotation();
    // Transform to FOP's user space
    // at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight());
    at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(), mediaBox.getLowerLeftY() - viewBox.getLowerLeftY());
    switch(rotation) {
        case 90:
            at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
            at.translate(0, viewBox.getWidth());
            at.rotate(-Math.PI / 2.0);
            break;
        case 180:
            at.translate(viewBox.getWidth(), viewBox.getHeight());
            at.rotate(-Math.PI);
            break;
        case 270:
            at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
            at.translate(viewBox.getHeight(), 0);
            at.rotate(-Math.PI * 1.5);
            break;
        default:
    }
    // Compensate for Crop Boxes not starting at 0,0
    at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
    if (!at.isIdentity()) {
        form.setMatrix(at);
    }
    BoundingBox bbox = new BoundingBox();
    bbox.setLowerLeftX(viewBox.getLowerLeftX());
    bbox.setLowerLeftY(viewBox.getLowerLeftY());
    bbox.setUpperRightX(viewBox.getUpperRightX());
    bbox.setUpperRightY(viewBox.getUpperRightY());
    form.setBBox(new PDRectangle(bbox));
    return form;
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) BoundingBox(com.tom_roush.fontbox.util.BoundingBox) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream)

Aggregations

PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)82 IOException (java.io.IOException)19 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)13 PDAppearanceContentStream (com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream)12 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)11 Matrix (com.tom_roush.pdfbox.util.Matrix)11 COSArray (com.tom_roush.pdfbox.cos.COSArray)10 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)10 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)9 Path (android.graphics.Path)8 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)5 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)5 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 COSName (com.tom_roush.pdfbox.cos.COSName)4 PDExtendedGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState)4 PDAnnotationTextMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup)4 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)4