use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDFRenderer method renderImage.
/**
* Returns the given page as an RGB or ARGB image at the given scale.
* @param pageIndex the zero-based index of the page to be converted
* @param scale the scaling factor, where 1 = 72 DPI
* @param imageType the type of image to return
* @param destination controlling visibility of optional content groups
* @return the rendered page image
* @throws IOException if the PDF cannot be read
*/
public Bitmap renderImage(int pageIndex, float scale, ImageType imageType, RenderDestination destination) throws IOException {
PDPage page = document.getPage(pageIndex);
PDRectangle cropbBox = page.getCropBox();
float widthPt = cropbBox.getWidth();
float heightPt = cropbBox.getHeight();
// PDFBOX-4306 avoid single blank pixel line on the right or on the bottom
int widthPx = (int) Math.max(Math.floor(widthPt * scale), 1);
int heightPx = (int) Math.max(Math.floor(heightPt * scale), 1);
// PDFBOX-4518 the maximum size (w*h) of a buffered image is limited to Integer.MAX_VALUE
if ((long) widthPx * (long) heightPx > Integer.MAX_VALUE) {
throw new IOException(//
"Maximum size of image exceeded (w * h * scale) = " + widthPt + " * " + heightPt + " * " + scale + " > " + Integer.MAX_VALUE);
}
int rotationAngle = page.getRotation();
Bitmap.Config bimType = imageType.toBitmapConfig();
if (imageType != ImageType.ARGB && hasBlendMode(page)) {
// PDFBOX-4095: if the PDF has blending on the top level, draw on transparent background
// Inpired from PDF.js: if a PDF page uses any blend modes other than Normal,
// PDF.js renders everything on a fully transparent RGBA canvas.
// Finally when the page has been rendered, PDF.js draws the RGBA canvas on a white canvas.
bimType = Bitmap.Config.ARGB_8888;
}
// swap width and height
Bitmap image;
if (rotationAngle == 90 || rotationAngle == 270) {
image = Bitmap.createBitmap(heightPx, widthPx, bimType);
} else {
image = Bitmap.createBitmap(widthPx, heightPx, bimType);
}
pageImage = image;
// use a transparent background if the image type supports alpha
Paint paint = new Paint();
Canvas canvas = new Canvas(image);
if (imageType == ImageType.ARGB) {
paint.setColor(Color.TRANSPARENT);
} else {
paint.setColor(Color.WHITE);
}
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, image.getWidth(), image.getHeight(), paint);
paint.reset();
transform(canvas, page, scale, scale);
// the end-user may provide a custom PageDrawer
PageDrawerParameters parameters = new PageDrawerParameters(this, page, subsamplingAllowed, destination);
PageDrawer drawer = createPageDrawer(parameters);
drawer.drawPage(paint, canvas, page.getCropBox());
if (image.getConfig() != imageType.toBitmapConfig()) {
// PDFBOX-4095: draw temporary transparent image on white background
Bitmap newImage = Bitmap.createBitmap(image.getWidth(), image.getHeight(), imageType.toBitmapConfig());
Canvas dstCanvas = new Canvas(newImage);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
dstCanvas.drawRect(0, 0, image.getWidth(), image.getHeight(), paint);
dstCanvas.drawBitmap(image, 0.0f, 0.0f, paint);
image = newImage;
}
return image;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDVisibleSignDesigner method calculatePageSize.
/**
* Each page of document can be different sizes. This method calculates the page size based on
* the page media box.
*
* @param document
* @param page The 1-based page number for which the page size should be calculated.
* @throws IllegalArgumentException if the page argument is lower than 0.
*/
private void calculatePageSize(PDDocument document, int page) {
if (page < 1) {
throw new IllegalArgumentException("First page of pdf is 1, not " + page);
}
PDPage firstPage = document.getPage(page - 1);
PDRectangle mediaBox = firstPage.getMediaBox();
pageHeight(mediaBox.getHeight());
pageWidth = mediaBox.getWidth();
imageSizeInPercents = 100;
rotation = firstPage.getRotation() % 360;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDPage method getMediaBox.
/**
* A rectangle, expressed in default user space units, defining the boundaries of the physical medium on which the
* page is intended to be displayed or printed.
*
* @return the media box.
*/
public PDRectangle getMediaBox() {
if (mediaBox == null) {
COSBase base = PDPageTree.getInheritableAttribute(page, COSName.MEDIA_BOX);
if (base instanceof COSArray) {
mediaBox = new PDRectangle((COSArray) base);
}
}
if (mediaBox == null) {
Log.d("PdfBox-Android", "Can't find MediaBox, will use U.S. Letter");
mediaBox = PDRectangle.LETTER;
}
return mediaBox;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDPage method clipToMediaBox.
/**
* Clips the given box to the bounds of the media box.
*/
private PDRectangle clipToMediaBox(PDRectangle box) {
PDRectangle mediaBox = getMediaBox();
PDRectangle result = new PDRectangle();
result.setLowerLeftX(Math.max(mediaBox.getLowerLeftX(), box.getLowerLeftX()));
result.setLowerLeftY(Math.max(mediaBox.getLowerLeftY(), box.getLowerLeftY()));
result.setUpperRightX(Math.min(mediaBox.getUpperRightX(), box.getUpperRightX()));
result.setUpperRightY(Math.min(mediaBox.getUpperRightY(), box.getUpperRightY()));
return result;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle 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;
}
Aggregations