use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class LayerUtility method appendFormAsLayer.
/**
* Places the given form over the existing content of the indicated page (like an overlay).
* The form is enveloped in a marked content section to indicate that it's part of an
* optional content group (OCG), here used as a layer. This optional group is returned and
* can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
* <p>
* You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make
* sure that the graphics state is reset.
*
* @param targetPage the target page
* @param form the form to place
* @param transform the transformation matrix that controls the placement of your form. You'll
* need this if your page has a crop box different than the media box, or if these have negative
* coordinates, or if you want to scale or adjust your form.
* @param layerName the name for the layer/OCG to produce
* @return the optional content group that was generated for the form usage
* @throws IOException if an I/O error occurs
*/
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException {
PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
PDOptionalContentProperties ocprops = catalog.getOCProperties();
if (ocprops == null) {
ocprops = new PDOptionalContentProperties();
catalog.setOCProperties(ocprops);
}
if (ocprops.hasGroup(layerName)) {
throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
}
PDRectangle cropBox = targetPage.getCropBox();
if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity()) {
// PDFBOX-4044
Log.w("PdfBox-Android", "Negative cropBox " + cropBox + " and identity transform may make your form invisible");
}
PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
ocprops.addGroup(layer);
PDPageContentStream contentStream = new PDPageContentStream(targetDoc, targetPage, AppendMode.APPEND, !DEBUG);
contentStream.beginMarkedContent(COSName.OC, layer);
contentStream.saveGraphicsState();
contentStream.transform(new Matrix(transform));
contentStream.drawForm(form);
contentStream.restoreGraphicsState();
contentStream.endMarkedContent();
contentStream.close();
return layer;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDSquigglyAppearanceHandler method generateNormalAppearance.
@Override
public void generateNormalAppearance() {
PDAnnotationTextMarkup annotation = (PDAnnotationTextMarkup) getAnnotation();
PDRectangle rect = annotation.getRectangle();
float[] pathsArray = annotation.getQuadPoints();
if (pathsArray == null) {
return;
}
AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
PDColor color = annotation.getColor();
if (color == null || color.getComponents().length == 0) {
return;
}
if (Float.compare(ab.width, 0) == 0) {
// value found in adobe reader
ab.width = 1.5f;
}
// Adjust rectangle even if not empty, see PLPDF.com-MarkupAnnotations.pdf
// TODO in a class structure this should be overridable
// this is similar to polyline but different data type
// all coordinates (unlike painting) are used because I'm lazy
float minX = Float.MAX_VALUE;
float minY = Float.MAX_VALUE;
float maxX = Float.MIN_VALUE;
float maxY = Float.MIN_VALUE;
for (int i = 0; i < pathsArray.length / 2; ++i) {
float x = pathsArray[i * 2];
float y = pathsArray[i * 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 - ab.width / 2, rect.getLowerLeftX()));
rect.setLowerLeftY(Math.min(minY - ab.width / 2, rect.getLowerLeftY()));
rect.setUpperRightX(Math.max(maxX + ab.width / 2, rect.getUpperRightX()));
rect.setUpperRightY(Math.max(maxY + ab.width / 2, rect.getUpperRightY()));
annotation.setRectangle(rect);
PDAppearanceContentStream cs = null;
try {
cs = getNormalAppearanceAsContentStream();
setOpacity(cs, annotation.getConstantOpacity());
cs.setStrokingColor(color);
// https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
for (int i = 0; i < pathsArray.length / 8; ++i) {
// Adobe uses a fixed pattern that assumes a height of 40, and it transforms to that height
// horizontally and the same / 1.8 vertically.
// translation apparently based on bottom left, but slightly different in Adobe
// TODO what if the annotation is not horizontal?
float height = pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5];
cs.transform(new Matrix(height / 40f, 0, 0, height / 40f / 1.8f, pathsArray[i * 8 + 4], pathsArray[i * 8 + 5]));
// Create form, BBox is mostly fixed, except for the horizontal size which is
// horizontal size divided by the horizontal transform factor from above
// (almost)
PDFormXObject form = new PDFormXObject(createCOSStream());
form.setBBox(new PDRectangle(-0.5f, -0.5f, (pathsArray[i * 8 + 2] - pathsArray[i * 8]) / height * 40f + 0.5f, 13));
form.setResources(new PDResources());
form.setMatrix(AffineTransform.getTranslateInstance(0.5f, 0.5f));
cs.drawForm(form);
PDFormContentStream formCS = null;
try {
formCS = new PDFormContentStream(form);
PDTilingPattern pattern = new PDTilingPattern();
pattern.setBBox(new PDRectangle(0, 0, 10, 12));
pattern.setXStep(10);
pattern.setYStep(13);
pattern.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING_FASTER_TILING);
pattern.setPaintType(PDTilingPattern.PAINT_UNCOLORED);
PDPatternContentStream patternCS = null;
try {
patternCS = new PDPatternContentStream(pattern);
// from Adobe
patternCS.setLineCapStyle(1);
patternCS.setLineJoinStyle(1);
patternCS.setLineWidth(1);
patternCS.setMiterLimit(10);
patternCS.moveTo(0, 1);
patternCS.lineTo(5, 11);
patternCS.lineTo(10, 1);
patternCS.stroke();
} finally {
IOUtils.closeQuietly(patternCS);
}
COSName patternName = form.getResources().add(pattern);
// PDColorSpace patternColorSpace = new PDPattern(null, PDDeviceRGB.INSTANCE);
// PDColor patternColor = new PDColor(color.getComponents(), patternName, patternColorSpace);
// formCS.setNonStrokingColor(patternColor); TODO: PdfBox-Android
// With Adobe, the horizontal size is slightly different, don't know why
formCS.addRect(0, 0, (pathsArray[i * 8 + 2] - pathsArray[i * 8]) / height * 40f, 12);
formCS.fill();
} finally {
IOUtils.closeQuietly(formCS);
}
}
} catch (IOException ex) {
Log.e("PdfBox-Android", ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(cs);
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDStrikeoutAppearanceHandler method generateNormalAppearance.
@Override
public void generateNormalAppearance() {
PDAnnotationTextMarkup annotation = (PDAnnotationTextMarkup) getAnnotation();
PDRectangle rect = annotation.getRectangle();
float[] pathsArray = annotation.getQuadPoints();
if (pathsArray == null) {
return;
}
AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
PDColor color = annotation.getColor();
if (color == null || color.getComponents().length == 0) {
return;
}
if (Float.compare(ab.width, 0) == 0) {
// value found in adobe reader
ab.width = 1.5f;
}
// Adjust rectangle even if not empty, see PLPDF.com-MarkupAnnotations.pdf
// TODO in a class structure this should be overridable
// this is similar to polyline but different data type
float minX = Float.MAX_VALUE;
float minY = Float.MAX_VALUE;
float maxX = Float.MIN_VALUE;
float maxY = Float.MIN_VALUE;
for (int i = 0; i < pathsArray.length / 2; ++i) {
float x = pathsArray[i * 2];
float y = pathsArray[i * 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 - ab.width / 2, rect.getLowerLeftX()));
rect.setLowerLeftY(Math.min(minY - ab.width / 2, rect.getLowerLeftY()));
rect.setUpperRightX(Math.max(maxX + ab.width / 2, rect.getUpperRightX()));
rect.setUpperRightY(Math.max(maxY + ab.width / 2, rect.getUpperRightY()));
annotation.setRectangle(rect);
PDAppearanceContentStream cs = null;
try {
cs = getNormalAppearanceAsContentStream();
setOpacity(cs, annotation.getConstantOpacity());
cs.setStrokingColor(color);
if (ab.dashArray != null) {
cs.setLineDashPattern(ab.dashArray, 0);
}
cs.setLineWidth(ab.width);
// https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
for (int i = 0; i < pathsArray.length / 8; ++i) {
// get mid point between bounds, subtract the line width to approximate what Adobe is doing
// See e.g. CTAN-example-Annotations.pdf and PLPDF.com-MarkupAnnotations.pdf
// and https://bugs.ghostscript.com/show_bug.cgi?id=693664
// do the math for diagonal annotations with this weird old trick:
// https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
float len0 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8] - pathsArray[i * 8 + 4], 2) + Math.pow(pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5], 2)));
float x0 = pathsArray[i * 8 + 4];
float y0 = pathsArray[i * 8 + 5];
if (Float.compare(len0, 0) != 0) {
// only if both coordinates are not identical to avoid divide by zero
x0 += (pathsArray[i * 8] - pathsArray[i * 8 + 4]) / len0 * (len0 / 2 - ab.width);
y0 += (pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5]) / len0 * (len0 / 2 - ab.width);
}
float len1 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6], 2) + Math.pow(pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7], 2)));
float x1 = pathsArray[i * 8 + 6];
float y1 = pathsArray[i * 8 + 7];
if (Float.compare(len1, 0) != 0) {
// only if both coordinates are not identical to avoid divide by zero
x1 += (pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6]) / len1 * (len1 / 2 - ab.width);
y1 += (pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7]) / len1 * (len1 / 2 - ab.width);
}
cs.moveTo(x0, y0);
cs.lineTo(x1, y1);
}
cs.stroke();
} catch (IOException ex) {
Log.e("PdfBox-Android", ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(cs);
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDTextAppearanceHandler method drawNote.
private void drawNote(PDAnnotationText annotation, final PDAppearanceContentStream contentStream) throws IOException {
PDRectangle bbox = adjustRectAndBBox(annotation, 18, 20);
contentStream.setMiterLimit(4);
// get round edge the easy way. Adobe uses 4 lines with 4 arcs of radius 0.785 which is bigger.
contentStream.setLineJoinStyle(1);
contentStream.setLineCapStyle(0);
// value from Adobe
contentStream.setLineWidth(0.61f);
contentStream.addRect(1, 1, bbox.getWidth() - 2, bbox.getHeight() - 2);
contentStream.moveTo(bbox.getWidth() / 4, bbox.getHeight() / 7 * 2);
contentStream.lineTo(bbox.getWidth() * 3 / 4 - 1, bbox.getHeight() / 7 * 2);
contentStream.moveTo(bbox.getWidth() / 4, bbox.getHeight() / 7 * 3);
contentStream.lineTo(bbox.getWidth() * 3 / 4 - 1, bbox.getHeight() / 7 * 3);
contentStream.moveTo(bbox.getWidth() / 4, bbox.getHeight() / 7 * 4);
contentStream.lineTo(bbox.getWidth() * 3 / 4 - 1, bbox.getHeight() / 7 * 4);
contentStream.moveTo(bbox.getWidth() / 4, bbox.getHeight() / 7 * 5);
contentStream.lineTo(bbox.getWidth() * 3 / 4 - 1, bbox.getHeight() / 7 * 5);
contentStream.fillAndStroke();
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDTextAppearanceHandler method drawStar.
private void drawStar(PDAnnotationText annotation, final PDAppearanceContentStream contentStream) throws IOException {
PDRectangle bbox = adjustRectAndBBox(annotation, 20, 19);
float min = Math.min(bbox.getWidth(), bbox.getHeight());
contentStream.setMiterLimit(4);
contentStream.setLineJoinStyle(1);
contentStream.setLineCapStyle(0);
// value from Adobe
contentStream.setLineWidth(0.59f);
contentStream.transform(Matrix.getScaleInstance(0.001f * min / 0.8f, 0.001f * min / 0.8f));
// we get the shape of a Zapf Dingbats star (0x2605) and use that one.
// Adobe uses a different font (which one?), or created the shape from scratch.
Path path = PDType1Font.ZAPF_DINGBATS.getPath("a35");
addPath(contentStream, path);
contentStream.fillAndStroke();
}
Aggregations