use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.
the class PDPageContentStream method drawImage.
/**
* Draw an image at the origin with the given transformation matrix.
*
* @param image The image to draw.
* @param matrix The transformation matrix to apply to the image.
*
* @throws IOException If there is an error writing to the stream.
* @throws IllegalStateException If the method was called within a text block.
*/
public void drawImage(PDImageXObject image, Matrix matrix) throws IOException {
if (inTextMode) {
throw new IllegalStateException("Error: drawImage is not allowed within a text block.");
}
saveGraphicsState();
AffineTransform transform = matrix.createAffineTransform();
transform(new Matrix(transform));
writeOperand(resources.add(image));
writeOperator(OperatorName.DRAW_OBJECT);
restoreGraphicsState();
}
use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.
the class PDPageContentStream method drawXObject.
/**
* Draw an xobject(form or image) using the given {@link AffineTransform} to position
* the xobject.
*
* @param xobject The xobject to draw.
* @param transform the transformation matrix
* @throws IOException If there is an error writing to the stream.
* @throws IllegalStateException If the method was called within a text block.
* @deprecated Use {@link #drawImage(PDImageXObject, Matrix) drawImage(PDImageXObject, Matrix)}
* or {@link #drawForm(PDFormXObject) drawForm(PDFormXObject)} with
* {@link #transform(Matrix) transform(Matrix)} instead.
*/
@Deprecated
public void drawXObject(PDXObject xobject, AffineTransform transform) throws IOException {
if (inTextMode) {
throw new IllegalStateException("Error: drawXObject is not allowed within a text block.");
}
String xObjectPrefix;
if (xobject instanceof PDImageXObject) {
xObjectPrefix = "Im";
} else {
xObjectPrefix = "Form";
}
COSName objMapping = resources.add(xobject, xObjectPrefix);
saveGraphicsState();
transform(new Matrix(transform));
writeOperand(objMapping);
writeOperator(OperatorName.DRAW_OBJECT);
restoreGraphicsState();
}
use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.
the class PDPageContentStream method drawImage.
/**
* Draw an inline image at the x,y coordinates and a certain width and height.
*
* @param inlineImage The inline image to draw.
* @param x The x-coordinate to draw the inline image.
* @param y The y-coordinate to draw the inline image.
* @param width The width of the inline image to draw.
* @param height The height of the inline image to draw.
*
* @throws IOException If there is an error writing to the stream.
* @throws IllegalStateException If the method was called within a text block.
*/
public void drawImage(PDInlineImage inlineImage, float x, float y, float width, float height) throws IOException {
if (inTextMode) {
throw new IllegalStateException("Error: drawImage is not allowed within a text block.");
}
saveGraphicsState();
transform(new Matrix(width, 0, 0, height, x, y));
// create the image dictionary
StringBuilder sb = new StringBuilder();
sb.append(OperatorName.BEGIN_INLINE_IMAGE);
sb.append("\n /W ");
sb.append(inlineImage.getWidth());
sb.append("\n /H ");
sb.append(inlineImage.getHeight());
sb.append("\n /CS ");
sb.append("/");
sb.append(inlineImage.getColorSpace().getName());
if (inlineImage.getDecode() != null && inlineImage.getDecode().size() > 0) {
sb.append("\n /D ");
sb.append("[");
for (COSBase base : inlineImage.getDecode()) {
sb.append(((COSNumber) base).intValue());
sb.append(" ");
}
sb.append("]");
}
if (inlineImage.isStencil()) {
sb.append("\n /IM true");
}
sb.append("\n /BPC ");
sb.append(inlineImage.getBitsPerComponent());
// image dictionary
write(sb.toString());
writeLine();
// binary data
writeOperator(OperatorName.BEGIN_INLINE_IMAGE_DATA);
writeBytes(inlineImage.getData());
writeLine();
writeOperator(OperatorName.END_INLINE_IMAGE);
restoreGraphicsState();
}
use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.
the class AppearanceGeneratorHelper method prepareNormalAppearanceStream.
private PDAppearanceStream prepareNormalAppearanceStream(PDAnnotationWidget widget) {
PDAppearanceStream appearanceStream = new PDAppearanceStream(field.getAcroForm().getDocument());
// Calculate the entries for the bounding box and the transformation matrix
// settings for the appearance stream
int rotation = resolveRotation(widget);
PDRectangle rect = widget.getRectangle();
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0);
PointF point2D = matrix.transformPoint(rect.getWidth(), rect.getHeight());
PDRectangle bbox = new PDRectangle(Math.abs((float) point2D.x), Math.abs((float) point2D.y));
appearanceStream.setBBox(bbox);
AffineTransform at = calculateMatrix(bbox, rotation);
if (!at.isIdentity()) {
appearanceStream.setMatrix(at);
}
appearanceStream.setFormType(1);
appearanceStream.setResources(new PDResources());
return appearanceStream;
}
use of com.tom_roush.pdfbox.util.Matrix in project PdfBox-Android by TomRoush.
the class TestLayerUtility method createOverlay1.
private File createOverlay1() throws IOException {
File targetFile = new File(testResultsDir, "overlay1.pdf");
PDDocument doc = new PDDocument();
try {
// Create new page
PDPage page = new PDPage();
doc.addPage(page);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
page.setResources(resources);
}
// Setup page content stream and paint background/title
PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.OVERWRITE, false);
PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.setNonStrokingColor(AWTColor.LIGHT_GRAY);
contentStream.beginText();
float fontSize = 96;
contentStream.setFont(font, fontSize);
String text = "OVERLAY";
// float sw = font.getStringWidth(text);
// Too bad, base 14 fonts don't return character metrics.
PDRectangle crop = page.getCropBox();
float cx = crop.getWidth() / 2f;
float cy = crop.getHeight() / 2f;
Matrix transform = new Matrix();
transform.translate(cx, cy);
transform.rotate(Math.toRadians(45));
transform.translate(-190, /* sw/2 */
0);
contentStream.setTextMatrix(transform);
contentStream.showText(text);
contentStream.endText();
contentStream.close();
doc.save(targetFile.getAbsolutePath());
} finally {
doc.close();
}
return targetFile;
}
Aggregations