Search in sources :

Example 11 with PDRectangle

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

the class PDAbstractAppearanceHandler method setTransformationMatrix.

private void setTransformationMatrix(PDAppearanceStream appearanceStream) {
    PDRectangle bbox = getRectangle();
    appearanceStream.setBBox(bbox);
    AffineTransform transform = AffineTransform.getTranslateInstance(-bbox.getLowerLeftX(), -bbox.getLowerLeftY());
    appearanceStream.setMatrix(transform);
}
Also used : AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 12 with PDRectangle

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

the class PDAbstractAppearanceHandler method handleBorderBox.

PDRectangle handleBorderBox(PDAnnotationSquareCircle annotation, float lineWidth) {
    // There are two options. The handling is not part of the PDF specification but
    // implementation specific to Adobe Reader
    // - if /RD is set the border box is the /Rect entry inset by the respective
    // border difference.
    // - if /RD is not set the border box is defined by the /Rect entry. The /RD entry will
    // be set to be the line width and the /Rect is enlarged by the /RD amount
    PDRectangle borderBox;
    float[] rectDifferences = annotation.getRectDifferences();
    if (rectDifferences.length == 0) {
        borderBox = getPaddedRectangle(getRectangle(), lineWidth / 2);
        // the differences rectangle
        annotation.setRectDifferences(lineWidth / 2);
        annotation.setRectangle(addRectDifferences(getRectangle(), annotation.getRectDifferences()));
        // when the normal appearance stream was generated BBox and Matrix have been set to the
        // values of the original /Rect. As the /Rect was changed that needs to be adjusted too.
        annotation.getNormalAppearanceStream().setBBox(getRectangle());
        AffineTransform transform = AffineTransform.getTranslateInstance(-getRectangle().getLowerLeftX(), -getRectangle().getLowerLeftY());
        annotation.getNormalAppearanceStream().setMatrix(transform);
    } else {
        borderBox = applyRectDifferences(getRectangle(), rectDifferences);
        borderBox = getPaddedRectangle(borderBox, lineWidth / 2);
    }
    return borderBox;
}
Also used : AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 13 with PDRectangle

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

the class PDPolygonAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    float lineWidth = getLineWidth();
    PDRectangle rect = annotation.getRectangle();
    // Adjust rectangle even if not empty
    // CTAN-example-Annotations.pdf p2
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    float[][] pathArray = getPathArray(annotation);
    if (pathArray == null) {
        return;
    }
    for (int i = 0; i < pathArray.length; ++i) {
        for (int j = 0; j < pathArray[i].length / 2; ++j) {
            float x = pathArray[i][j * 2];
            float y = pathArray[i][j * 2 + 1];
            minX = Math.min(minX, x);
            minY = Math.min(minY, y);
            maxX = Math.max(maxX, x);
            maxY = Math.max(maxY, y);
        }
    }
    rect.setLowerLeftX(Math.min(minX - lineWidth, rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - lineWidth, rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + lineWidth, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + lineWidth, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    PDAppearanceContentStream contentStream = null;
    try {
        contentStream = getNormalAppearanceAsContentStream();
        boolean hasStroke = contentStream.setStrokingColorOnDemand(getColor());
        boolean hasBackground = contentStream.setNonStrokingColorOnDemand(annotation.getInteriorColor());
        setOpacity(contentStream, annotation.getConstantOpacity());
        contentStream.setBorderLine(lineWidth, annotation.getBorderStyle(), annotation.getBorder());
        PDBorderEffectDictionary borderEffect = annotation.getBorderEffect();
        if (borderEffect != null && borderEffect.getStyle().equals(PDBorderEffectDictionary.STYLE_CLOUDY)) {
            CloudyBorder cloudyBorder = new CloudyBorder(contentStream, borderEffect.getIntensity(), lineWidth, getRectangle());
            cloudyBorder.createCloudyPolygon(pathArray);
            annotation.setRectangle(cloudyBorder.getRectangle());
            PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
            appearanceStream.setBBox(cloudyBorder.getBBox());
            appearanceStream.setMatrix(cloudyBorder.getMatrix());
        } else {
            for (int i = 0; i < pathArray.length; i++) {
                float[] pointsArray = pathArray[i];
                // first array shall be of size 2 and specify the moveto operator
                if (i == 0 && pointsArray.length == 2) {
                    contentStream.moveTo(pointsArray[0], pointsArray[1]);
                } else {
                    // entries of length 2 shall be treated as lineto operator
                    if (pointsArray.length == 2) {
                        contentStream.lineTo(pointsArray[0], pointsArray[1]);
                    } else if (pointsArray.length == 6) {
                        contentStream.curveTo(pointsArray[0], pointsArray[1], pointsArray[2], pointsArray[3], pointsArray[4], pointsArray[5]);
                    }
                }
            }
            contentStream.closePath();
        }
        contentStream.drawShape(lineWidth, hasStroke, hasBackground);
    } catch (IOException e) {
        Log.e("PdfBox-Android", e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(contentStream);
    }
}
Also used : PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDBorderEffectDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderEffectDictionary) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException)

Example 14 with PDRectangle

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

the class PDAcroFormTest method createAcroFormWithMissingResourceInformation.

private byte[] createAcroFormWithMissingResourceInformation() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDAcroForm newAcroForm = new PDAcroForm(document);
    document.getDocumentCatalog().setAcroForm(newAcroForm);
    PDTextField textBox = new PDTextField(newAcroForm);
    textBox.setPartialName("SampleField");
    newAcroForm.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);
    // acroForm.setNeedAppearances(true);
    // acroForm.getField("SampleField").getCOSObject().setString(COSName.V, "content");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // this is a working PDF
    document.save(baos);
    document.close();
    return baos.toByteArray();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 15 with PDRectangle

use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle 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)

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