Java Code Examples for org.apache.pdfbox.util.Matrix#concatenate()

The following examples show how to use org.apache.pdfbox.util.Matrix#concatenate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void intersectShadingBBox(PDColor color, Area area) throws IOException
{
    if (color.getColorSpace() instanceof PDPattern)
    {
        PDColorSpace colorSpace = color.getColorSpace();
        PDAbstractPattern pat = ((PDPattern) colorSpace).getPattern(color);
        if (pat instanceof PDShadingPattern)
        {
            PDShading shading = ((PDShadingPattern) pat).getShading();
            PDRectangle bbox = shading.getBBox();
            if (bbox != null)
            {
                Matrix m = Matrix.concatenate(getInitialMatrix(), pat.getMatrix());
                Area bboxArea = new Area(bbox.transform(m));
                area.intersect(bboxArea);
            }
        }
    }
}
 
Example 2
Source File: MoveText.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws MissingOperandException
{
    if (arguments.size() < 2)
    {
        throw new MissingOperandException(operator, arguments);
    }
    Matrix textLineMatrix = context.getTextLineMatrix();
    if (textLineMatrix == null)
    {
        LOG.warn("TextLineMatrix is null, " + getName() + " operator will be ignored");
        return;
    }        
    
    COSBase base0 = arguments.get(0);
    COSBase base1 = arguments.get(1);
    if (!(base0 instanceof COSNumber))
    {
        return;
    }
    if (!(base1 instanceof COSNumber))
    {
        return;
    }
    COSNumber x = (COSNumber) base0;
    COSNumber y = (COSNumber) base1;

    Matrix matrix = new Matrix(1, 0, 0, 1, x.floatValue(), y.floatValue());
    textLineMatrix.concatenate(matrix);
    context.setTextMatrix(textLineMatrix.clone());
}
 
Example 3
Source File: TilingPaint.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Returns the pattern image in parent stream coordinates.
 */
private BufferedImage getImage(PageDrawer drawer, PDTilingPattern pattern, PDColorSpace colorSpace, 
        PDColor color, AffineTransform xform, Rectangle2D anchorRect) throws IOException
{
    float width = (float) Math.abs(anchorRect.getWidth());
    float height = (float) Math.abs(anchorRect.getHeight());

    // device scale transform (i.e. DPI) (see PDFBOX-1466.pdf)
    Matrix xformMatrix = new Matrix(xform);
    float xScale = Math.abs(xformMatrix.getScalingFactorX());
    float yScale = Math.abs(xformMatrix.getScalingFactorY());
    width *= xScale;
    height *= yScale;

    int rasterWidth = Math.max(1, ceiling(width));
    int rasterHeight = Math.max(1, ceiling(height));

    BufferedImage image = new BufferedImage(rasterWidth, rasterHeight, BufferedImage.TYPE_INT_ARGB);

    Graphics2D graphics = image.createGraphics();

    // flip a -ve YStep around its own axis (see gs-bugzilla694385.pdf)
    if (pattern.getYStep() < 0)
    {
        graphics.translate(0, rasterHeight);
        graphics.scale(1, -1);
    }

    // flip a -ve XStep around its own axis
    if (pattern.getXStep() < 0)
    {
        graphics.translate(rasterWidth, 0);
        graphics.scale(-1, 1);
    }

    // device scale transform (i.e. DPI)
    graphics.scale(xScale, yScale);

    // apply only the scaling from the pattern transform, doing scaling here improves the
    // image quality and prevents large scale-down factors from creating huge tiling cells.
    Matrix newPatternMatrix;
    newPatternMatrix = Matrix.getScaleInstance(
            Math.abs(patternMatrix.getScalingFactorX()),
            Math.abs(patternMatrix.getScalingFactorY()));

    // move origin to (0,0)
    newPatternMatrix.concatenate(
            Matrix.getTranslateInstance(-pattern.getBBox().getLowerLeftX(),
                    -pattern.getBBox().getLowerLeftY()));

    // render using PageDrawer
    drawer.drawTilingPattern(graphics, pattern, colorSpace, color, newPatternMatrix);
    graphics.dispose();

    return image;
}
 
Example 4
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Process the given annotation with the specified appearance stream.
 *
 * @param annotation The annotation containing the appearance stream to process.
 * @param appearance The appearance stream to process.
 * @throws IOException If there is an error reading or parsing the appearance content stream.
 */
protected void processAnnotation(PDAnnotation annotation, PDAppearanceStream appearance)
        throws IOException
{
    PDResources parent = pushResources(appearance);
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();

    PDRectangle bbox = appearance.getBBox();
    PDRectangle rect = annotation.getRectangle();
    Matrix matrix = appearance.getMatrix();

    // zero-sized rectangles are not valid
    if (rect != null && rect.getWidth() > 0 && rect.getHeight() > 0 && bbox != null)
    {
        // transformed appearance box  fixme: may be an arbitrary shape
        Rectangle2D transformedBox = bbox.transform(matrix).getBounds2D();

        // compute a matrix which scales and translates the transformed appearance box to align
        // with the edges of the annotation's rectangle
        Matrix a = Matrix.getTranslateInstance(rect.getLowerLeftX(), rect.getLowerLeftY());
        a.concatenate(Matrix.getScaleInstance((float)(rect.getWidth() / transformedBox.getWidth()),
                (float)(rect.getHeight() / transformedBox.getHeight())));
        a.concatenate(Matrix.getTranslateInstance((float) -transformedBox.getX(),
                (float) -transformedBox.getY()));

        // Matrix shall be concatenated with A to form a matrix AA that maps from the appearance's
        // coordinate system to the annotation's rectangle in default user space
        //
        // HOWEVER only the opposite order works for rotated pages with 
        // filled fields / annotations that have a matrix in the appearance stream, see PDFBOX-3083
        Matrix aa = Matrix.concatenate(a, matrix);

        // make matrix AA the CTM
        getGraphicsState().setCurrentTransformationMatrix(aa);

        // clip to bounding box
        clipToRect(bbox);

        // needed for patterns in appearance streams, e.g. PDFBOX-2182
        initialMatrix = aa.clone();

        processStreamOperators(appearance);
    }
    
    restoreGraphicsStack(savedStack);
    popResources(parent);
}
 
Example 5
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Process the given annotation with the specified appearance stream.
 *
 * @param annotation The annotation containing the appearance stream to process.
 * @param appearance The appearance stream to process.
 * @throws IOException If there is an error reading or parsing the appearance content stream.
 */
protected void processAnnotation(PDAnnotation annotation, PDAppearanceStream appearance)
        throws IOException
{
    PDResources parent = pushResources(appearance);
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();

    PDRectangle bbox = appearance.getBBox();
    PDRectangle rect = annotation.getRectangle();
    Matrix matrix = appearance.getMatrix();

    // zero-sized rectangles are not valid
    if (rect != null && rect.getWidth() > 0 && rect.getHeight() > 0 && bbox != null)
    {
        // transformed appearance box  fixme: may be an arbitrary shape
        Rectangle2D transformedBox = bbox.transform(matrix).getBounds2D();

        // compute a matrix which scales and translates the transformed appearance box to align
        // with the edges of the annotation's rectangle
        Matrix a = Matrix.getTranslateInstance(rect.getLowerLeftX(), rect.getLowerLeftY());
        a.concatenate(Matrix.getScaleInstance((float)(rect.getWidth() / transformedBox.getWidth()),
                (float)(rect.getHeight() / transformedBox.getHeight())));
        a.concatenate(Matrix.getTranslateInstance((float) -transformedBox.getX(),
                (float) -transformedBox.getY()));

        // Matrix shall be concatenated with A to form a matrix AA that maps from the appearance's
        // coordinate system to the annotation's rectangle in default user space
        //
        // HOWEVER only the opposite order works for rotated pages with 
        // filled fields / annotations that have a matrix in the appearance stream, see PDFBOX-3083
        Matrix aa = Matrix.concatenate(a, matrix);

        // make matrix AA the CTM
        getGraphicsState().setCurrentTransformationMatrix(aa);

        // clip to bounding box
        clipToRect(bbox);

        // needed for patterns in appearance streams, e.g. PDFBOX-2182
        initialMatrix = aa.clone();

        processStreamOperators(appearance);
    }
    
    restoreGraphicsStack(savedStack);
    popResources(parent);
}
 
Example 6
Source File: TilingPaint.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Creates a new tiling Paint. The parameters color and colorSpace must be null for a colored
 * tiling Paint (because it has its own colors), and non null for an uncolored tiling Paint.
 *
 * @param drawer renderer to render the page
 * @param pattern tiling pattern dictionary
 * @param colorSpace color space for this tiling
 * @param color color for this tiling
 * @param xform device scale transform
 *
 * @throws java.io.IOException if something goes wrong while drawing the pattern
 */
TilingPaint(PageDrawer drawer, PDTilingPattern pattern, PDColorSpace colorSpace,
                   PDColor color, AffineTransform xform) throws IOException
{
    // pattern space -> user space
    patternMatrix = Matrix.concatenate(drawer.getInitialMatrix(), pattern.getMatrix());
    Rectangle2D anchorRect = getAnchorRect(pattern);
    paint = new TexturePaint(getImage(drawer, pattern, colorSpace, color, xform, anchorRect), anchorRect);
}