Example usage for org.apache.pdfbox.util Matrix getTranslateY

List of usage examples for org.apache.pdfbox.util Matrix getTranslateY

Introduction

In this page you can find the example usage for org.apache.pdfbox.util Matrix getTranslateY.

Prototype

public float getTranslateY() 

Source Link

Document

Returns the y-translation element of this matrix.

Usage

From source file:com.fngry.monk.biz.demo.pdf.pdfbox.PrintImageLocations.java

License:Apache License

/**
 * This is used to handle an operation./*from   w  w w  .j av  a 2s.  c  om*/
 *
 * @param operator The operation to perform.
 * @param operands The list of arguments.
 *
 * @throws IOException If there is an error processing the operation.
 */
@Override
protected void processOperator(Operator operator, List<COSBase> operands) throws IOException {
    String operation = operator.getName();
    if ("Do".equals(operation)) {
        COSName objectName = (COSName) operands.get(0);
        PDXObject xobject = getResources().getXObject(objectName);
        if (xobject instanceof PDImageXObject) {
            PDImageXObject image = (PDImageXObject) xobject;

            if ("png".equals(image.getSuffix())) {
                return;
            }

            int imageWidth = image.getWidth();
            int imageHeight = image.getHeight();

            //                System.out.println("Found image [" + objectName.getName() + "]");

            Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
            float imageXScale = ctmNew.getScalingFactorX();
            float imageYScale = ctmNew.getScalingFactorY();

            // position in user space units. 1 unit = 1/72 inch at 72 dpi
            //                System.out.println("position in PDF = " + ctmNew.getTranslateX() + ", " + ctmNew.getTranslateY() + " in user space units");
            //                // raw size in pixels
            //                System.out.println("raw image size  = " + imageWidth + ", " + imageHeight + " in pixels");
            //                // displayed size in user space units
            //                System.out.println("displayed size  = " + imageXScale + ", " + imageYScale + " in user space units");

            // displayed size in inches at 72 dpi rendering
            //                imageXScale /= 72;
            //                imageYScale /= 72;
            //                System.out.println("displayed size  = " + imageXScale + ", " + imageYScale + " in inches at 72 dpi rendering");
            //                // displayed size in millimeters at 72 dpi rendering
            //                imageXScale *= 25.4;
            //                imageYScale *= 25.4;
            //                System.out.println("displayed size  = " + imageXScale + ", " + imageYScale + " in millimeters at 72 dpi rendering");
            System.out.println();

            //                BufferedImage bufferImage = image.getImage();
            //                ByteArrayOutputStream os = new ByteArrayOutputStream();
            //                ImageIO.write(bufferImage, image.getSuffix(), os);
            //
            //                String fileName = this.pageName + "_" + objectName.getName() + "." + image.getSuffix();
            //                Path outputFile = new File("/Users/gaorongyu/Downloads/temp/"
            //                        + this.pageName + "_" + objectName.getName() + "." + image.getSuffix()).toPath();

            ImageInfo imageInfo = new ImageInfo((int) ctmNew.getTranslateX(), (int) ctmNew.getTranslateY(),
                    (int) imageXScale, (int) imageYScale);
            imageInfo.setImage(image);
            imageInfo.setObjectName(objectName);

            imageInfoList.add(imageInfo);
            //                java.nio.file.Files.copy(new ByteArrayInputStream(os.toByteArray()), outputFile);

        } else if (xobject instanceof PDFormXObject) {
            PDFormXObject form = (PDFormXObject) xobject;
            showForm(form);
        }
    } else {
        super.processOperator(operator, operands);
    }
}

From source file:com.repeatability.pdf.PDFTextStreamEngine.java

License:Apache License

/**
 * This method was originally written by Ben Litchfield for PDFStreamEngine.
 *///  ww  w  .jav a 2s  . c o m
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement)
        throws IOException {
    //
    // legacy calculations which were previously in PDFStreamEngine
    //

    PDGraphicsState state = getGraphicsState();
    Matrix ctm = state.getCurrentTransformationMatrix();
    float fontSize = state.getTextState().getFontSize();
    float horizontalScaling = state.getTextState().getHorizontalScaling() / 100f;
    Matrix textMatrix = getTextMatrix();

    BoundingBox bbox = font.getBoundingBox();
    if (bbox.getLowerLeftY() < Short.MIN_VALUE) {
        // PDFBOX-2158 and PDFBOX-3130
        // files by Salmat eSolutions / ClibPDF Library
        bbox.setLowerLeftY(-(bbox.getLowerLeftY() + 65536));
    }
    // 1/2 the bbox is used as the height todo: why?
    float glyphHeight = bbox.getHeight() / 2;

    // sometimes the bbox has very high values, but CapHeight is OK
    PDFontDescriptor fontDescriptor = font.getFontDescriptor();
    if (fontDescriptor != null) {
        float capHeight = fontDescriptor.getCapHeight();
        if (capHeight != 0 && capHeight < glyphHeight) {
            glyphHeight = capHeight;
        }
    }

    // transformPoint from glyph space -> text space
    float height;
    if (font instanceof PDType3Font) {
        height = font.getFontMatrix().transformPoint(0, glyphHeight).y;
    } else {
        height = glyphHeight / 1000;
    }

    float displacementX = displacement.getX();
    // the sorting algorithm is based on the width of the character. As the displacement
    // for vertical characters doesn't provide any suitable value for it, we have to 
    // calculate our own
    if (font.isVertical()) {
        displacementX = font.getWidth(code) / 1000;
        // there may be an additional scaling factor for true type fonts
        TrueTypeFont ttf = null;
        if (font instanceof PDTrueTypeFont) {
            ttf = ((PDTrueTypeFont) font).getTrueTypeFont();
        } else if (font instanceof PDType0Font) {
            PDCIDFont cidFont = ((PDType0Font) font).getDescendantFont();
            if (cidFont instanceof PDCIDFontType2) {
                ttf = ((PDCIDFontType2) cidFont).getTrueTypeFont();
            }
        }
        if (ttf != null && ttf.getUnitsPerEm() != 1000) {
            displacementX *= 1000f / ttf.getUnitsPerEm();
        }
    }
    // (modified) combined displacement, this is calculated *without* taking the character
    // spacing and word spacing into account, due to legacy code in TextStripper
    float tx = displacementX * fontSize * horizontalScaling;
    float ty = displacement.getY() * fontSize;

    // (modified) combined displacement matrix
    Matrix td = Matrix.getTranslateInstance(tx, ty);

    // (modified) text rendering matrix
    Matrix nextTextRenderingMatrix = td.multiply(textMatrix).multiply(ctm); // text space -> device space
    float nextX = nextTextRenderingMatrix.getTranslateX();
    float nextY = nextTextRenderingMatrix.getTranslateY();

    // (modified) width and height calculations
    float dxDisplay = nextX - textRenderingMatrix.getTranslateX();
    float dyDisplay = height * textRenderingMatrix.getScalingFactorY();

    //
    // start of the original method
    //

    // Note on variable names. There are three different units being used in this code.
    // Character sizes are given in glyph units, text locations are initially given in text
    // units, and we want to save the data in display units. The variable names should end with
    // Text or Disp to represent if the values are in text or disp units (no glyph units are
    // saved).

    float glyphSpaceToTextSpaceFactor = 1 / 1000f;
    if (font instanceof PDType3Font) {
        glyphSpaceToTextSpaceFactor = font.getFontMatrix().getScaleX();
    }

    float spaceWidthText = 0;
    try {
        // to avoid crash as described in PDFBOX-614, see what the space displacement should be
        spaceWidthText = font.getSpaceWidth() * glyphSpaceToTextSpaceFactor;
    } catch (Throwable exception) {
        LOG.warn(exception, exception);
    }

    if (spaceWidthText == 0) {
        spaceWidthText = font.getAverageFontWidth() * glyphSpaceToTextSpaceFactor;
        // the average space width appears to be higher than necessary so make it smaller
        spaceWidthText *= .80f;
    }
    if (spaceWidthText == 0) {
        spaceWidthText = 1.0f; // if could not find font, use a generic value
    }

    // the space width has to be transformed into display units
    float spaceWidthDisplay = spaceWidthText * textRenderingMatrix.getScalingFactorX();

    // use our additional glyph list for Unicode mapping
    unicode = font.toUnicode(code, glyphList);

    // when there is no Unicode mapping available, Acrobat simply coerces the character code
    // into Unicode, so we do the same. Subclasses of PDFStreamEngine don't necessarily want
    // this, which is why we leave it until this point in PDFTextStreamEngine.
    if (unicode == null) {
        if (font instanceof PDSimpleFont) {
            char c = (char) code;
            unicode = new String(new char[] { c });
        } else {
            // Acrobat doesn't seem to coerce composite font's character codes, instead it
            // skips them. See the "allah2.pdf" TestTextStripper file.
            return;
        }
    }

    // adjust for cropbox if needed
    Matrix translatedTextRenderingMatrix;
    if (translateMatrix == null) {
        translatedTextRenderingMatrix = textRenderingMatrix;
    } else {
        translatedTextRenderingMatrix = Matrix.concatenate(translateMatrix, textRenderingMatrix);
        nextX -= pageSize.getLowerLeftX();
        nextY -= pageSize.getLowerLeftY();
    }

    processTextPosition(new TextPosition(pageRotation, pageSize.getWidth(), pageSize.getHeight(),
            translatedTextRenderingMatrix, nextX, nextY, dyDisplay, dxDisplay, spaceWidthDisplay, unicode,
            new int[] { code }, font, fontSize, (int) (fontSize * textMatrix.getScalingFactorX())));
}

From source file:helper.pdfpreprocessing.pdf.TextHighlight.java

License:Apache License

/**
 * Computes a series of bounding boxes (PDRectangle) from a list of TextPositions. It will create a new bounding box
 * if the vertical tolerance is exceeded
 *
 * @param positions// w w  w  .  j  a v  a 2  s.c om
 * @throws IOException
 */
public List<PDRectangle> getTextBoundingBoxes(final List<TextPosition> positions) {
    final List<PDRectangle> boundingBoxes = new ArrayList<>();

    float lowerLeftX = -1, lowerLeftY = -1, upperRightX = -1, upperRightY = -1;
    boolean first = true;
    for (final TextPosition position : positions) {
        if (position == null) {
            continue;
        }
        final Matrix textPos = position.getTextMatrix();
        final float height = position.getHeight() * getHeightModifier();
        if (first) {
            lowerLeftX = textPos.getTranslateX();
            upperRightX = lowerLeftX + position.getWidth();

            lowerLeftY = textPos.getTranslateY();
            upperRightY = lowerLeftY + height;
            first = false;
            continue;
        }

        // we are still on the same line
        if (Math.abs(textPos.getTranslateY() - lowerLeftY) <= getVerticalTolerance()) {
            upperRightX = textPos.getTranslateX() + position.getWidth();
            upperRightY = textPos.getTranslateY() + height;
        } else {
            final PDRectangle boundingBox = boundingBox(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
            boundingBoxes.add(boundingBox);

            // new line
            lowerLeftX = textPos.getTranslateX();
            upperRightX = lowerLeftX + position.getWidth();

            lowerLeftY = textPos.getTranslateY();
            upperRightY = lowerLeftY + height;
        }
    }
    if (!(lowerLeftX == -1 && lowerLeftY == -1 && upperRightX == -1 && upperRightY == -1)) {
        final PDRectangle boundingBox = boundingBox(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
        boundingBoxes.add(boundingBox);
    }
    return boundingBoxes;
}