Package org.apache.pdfbox.pdmodel.common

Examples of org.apache.pdfbox.pdmodel.common.PDRectangle


    public PDRectangle getCropBox()
    {
        COSArray array = (COSArray) PDPageTree.getInheritableAttribute(page, COSName.CROP_BOX);
        if (array != null)
        {
            return clipToMediaBox(new PDRectangle(array));
        }
        else
        {
            return getMediaBox();
        }
View Full Code Here


     *
     * @return The BleedBox attribute.
     */
    public PDRectangle getBleedBox()
    {
        PDRectangle retval;
        COSArray array = (COSArray) page.getDictionaryObject(COSName.BLEED_BOX);
        if (array != null)
        {
            retval = clipToMediaBox(new PDRectangle(array));
        }
        else
        {
            retval = getCropBox();
        }
View Full Code Here

     *
     * @return The TrimBox attribute.
     */
    public PDRectangle getTrimBox()
    {
        PDRectangle retval;
        COSArray array = (COSArray) page.getDictionaryObject(COSName.TRIM_BOX);
        if (array != null)
        {
            retval = clipToMediaBox(new PDRectangle(array));
        }
        else
        {
            retval = getCropBox();
        }
View Full Code Here

     *
     * @return The ArtBox attribute.
     */
    public PDRectangle getArtBox()
    {
        PDRectangle retval;
        COSArray array = (COSArray) page.getDictionaryObject(COSName.ART_BOX);
        if (array != null)
        {
            retval = clipToMediaBox(new PDRectangle(array));
        }
        else
        {
            retval = getCropBox();
        }
View Full Code Here

    /**
     * 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;
    }
View Full Code Here

            PDFont pdFont, List<Object> tokens, PDAppearanceStream appearanceStream)
            throws IOException
    {
        PrintWriter printWriter = new PrintWriter(output, true);
        float fontSize = 0.0f;
        PDRectangle boundingBox = appearanceStream.getBBox();
        if (boundingBox == null)
        {
            boundingBox = fieldWidget.getRectangle().createRetranslatedRectangle();
        }
        printWriter.println("BT");
        if (defaultAppearance != null)
        {
            String daString = defaultAppearance.getString();
            PDFStreamParser daParser = new PDFStreamParser(new ByteArrayInputStream(
                    daString.getBytes("ISO-8859-1")));
            daParser.parse();
            List<Object> daTokens = daParser.getTokens();
            fontSize = calculateFontSize(pdFont, boundingBox, tokens, daTokens);
            int fontIndex = daTokens.indexOf(Operator.getOperator("Tf"));
            if (fontIndex != -1)
            {
                daTokens.set(fontIndex - 1, new COSFloat(fontSize));
            }
            ContentStreamWriter daWriter = new ContentStreamWriter(output);
            daWriter.writeTokens(daTokens);
        }

        PDRectangle borderEdge = getSmallestDrawnRectangle(boundingBox, tokens);

        // Acrobat calculates the left and right padding dependent on the offset of the border edge
        // This calculation works for forms having been generated by Acrobat.
        // Need to revisit this for forms being generated with other software.
        float paddingLeft = Math.max(2, Math.round(4 * borderEdge.getLowerLeftX()));
        float paddingRight = Math.max(2,
                Math.round(4 * (boundingBox.getUpperRightX() - borderEdge.getUpperRightX())));
        float verticalOffset = getVerticalOffset(boundingBox, pdFont, fontSize, tokens);

        // Acrobat shifts the value so it aligns to the bottom if
        // the font's caps are larger than the height of the borderEdge
        //
        // This is based on a small sample of test files and might not be generally the case.
        // The fontHeight calculation has been taken from getVerticalOffset().
        // We potentially need to revisit that calculation
        float fontHeight = boundingBox.getHeight() - verticalOffset * 2;

        if (fontHeight + 2 * borderEdge.getLowerLeftX() > borderEdge.getHeight())
        {
            verticalOffset = pdFont.getBoundingBox().getHeight() / 1000 * fontSize
                    - borderEdge.getHeight();
        }

        float leftOffset = 0f;

        // Acrobat aligns left regardless of the quadding if the text is wider than the remaining width
        float stringWidth = (pdFont.getStringWidth(value) / 1000) * fontSize;
        int q = getQ();
        if (q == PDTextField.QUADDING_LEFT
                || stringWidth > borderEdge.getWidth() - paddingLeft - paddingRight)
        {
            leftOffset = paddingLeft;
        }
        else if (q == PDTextField.QUADDING_CENTERED)
        {
            leftOffset = (boundingBox.getWidth() - stringWidth) / 2;
        }
        else if (q == PDTextField.QUADDING_RIGHT)
        {
            leftOffset = boundingBox.getWidth() - stringWidth - paddingRight;
        }
        else
        {
            // Unknown quadding value - default to left
            printWriter.println(paddingLeft + " " + verticalOffset + " Td");
            LOG.debug("Unknown justification value, defaulting to left: " + q);
        }

        printWriter.println(leftOffset + " " + verticalOffset + " Td");

        // add the value as hex string to deal with non ISO-8859-1 data values
        if (!isMultiLineValue(value) || stringWidth > borderEdge.getWidth() - paddingLeft -
                paddingRight)
        {
            printWriter.println("<" + new COSString(value).getHexString() + "> Tj");
        }
        else
View Full Code Here

        return retval;
    }

    private PDRectangle getSmallestDrawnRectangle(PDRectangle boundingBox, List<Object> tokens)
    {
        PDRectangle smallest = boundingBox;
        for (int i = 0; i < tokens.size(); i++)
        {
            Object next = tokens.get(i);
            if (next == Operator.getOperator("re"))
            {
                COSNumber x = (COSNumber) tokens.get(i - 4);
                COSNumber y = (COSNumber) tokens.get(i - 3);
                COSNumber width = (COSNumber) tokens.get(i - 2);
                COSNumber height = (COSNumber) tokens.get(i - 1);
                PDRectangle potentialSmallest = new PDRectangle();
                potentialSmallest.setLowerLeftX(x.floatValue());
                potentialSmallest.setLowerLeftY(y.floatValue());
                potentialSmallest.setUpperRightX(x.floatValue() + width.floatValue());
                potentialSmallest.setUpperRightY(y.floatValue() + height.floatValue());
                if (smallest == null
                        || smallest.getLowerLeftX() < potentialSmallest.getLowerLeftX()
                        || smallest.getUpperRightY() > potentialSmallest.getUpperRightY())
                {
                    smallest = potentialSmallest;
                }
            }
        }
View Full Code Here

    {
        this.renderer = renderer;
        this.page = page;
        this.pageNum = pageNum;

        PDRectangle cropBox = page.getCropBox();
        drawDimension = new Dimension((int)cropBox.getWidth(), (int)cropBox.getHeight());
        int rotation = page.getRotation();
        if (rotation == 90 || rotation == 270)
        {
            pageDimension = new Dimension(drawDimension.height, drawDimension.width);
        }
View Full Code Here

    private static void changeCropBox(PDDocument document, float a, float b, float c, float d)
    {
        for (PDPage page : document.getPages())
        {
            System.out.println("resizing page");
            PDRectangle rectangle = new PDRectangle();
            rectangle.setLowerLeftX(a);
            rectangle.setLowerLeftY(b);
            rectangle.setUpperRightX(c);
            rectangle.setUpperRightY(d);
            page.setCropBox(rectangle);

        }
    }
View Full Code Here

        //Transfer some values from page to form
        transferDict(page.getCOSObject(), form.getCOSStream(), PAGE_TO_FORM_FILTER, true);

        Matrix matrix = form.getMatrix();
        AffineTransform at = matrix != null ? matrix.createAffineTransform() : new AffineTransform();
        PDRectangle mediaBox = page.getMediaBox();
        PDRectangle cropBox = page.getCropBox();
        PDRectangle viewBox = (cropBox != null ? cropBox : mediaBox);

        //Handle the /Rotation entry on the page dict
        int rotation = getNormalizedRotation(page);

        //Transform to FOP's user space
        //at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight());
        at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(),
                mediaBox.getLowerLeftY() - viewBox.getLowerLeftY());
        switch (rotation)
        {
        case 90:
            at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
            at.translate(0, viewBox.getWidth());
            at.rotate(-Math.PI / 2.0);
            break;
        case 180:
            at.translate(viewBox.getWidth(), viewBox.getHeight());
            at.rotate(-Math.PI);
            break;
        case 270:
            at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
            at.translate(viewBox.getHeight(), 0);
            at.rotate(-Math.PI * 1.5);
        default:
            //no additional transformations necessary
        }
        //Compensate for Crop Boxes not starting at 0,0
        at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
        if (!at.isIdentity())
        {
            form.setMatrix(at);
        }

        BoundingBox bbox = new BoundingBox();
        bbox.setLowerLeftX(viewBox.getLowerLeftX());
        bbox.setLowerLeftY(viewBox.getLowerLeftY());
        bbox.setUpperRightX(viewBox.getUpperRightX());
        bbox.setUpperRightY(viewBox.getUpperRightY());
        form.setBBox(new PDRectangle(bbox));

        return form;
    }
View Full Code Here

TOP

Related Classes of org.apache.pdfbox.pdmodel.common.PDRectangle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.