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);
}
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;
}
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);
}
}
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();
}
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();
}
Aggregations