Search in sources :

Example 31 with PDRectangle

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;
}
Also used : Bitmap(android.graphics.Bitmap) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) Canvas(android.graphics.Canvas) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 32 with PDRectangle

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;
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 33 with PDRectangle

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;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 34 with PDRectangle

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;
}
Also used : PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 35 with PDRectangle

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;
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PointF(android.graphics.PointF) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) 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