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

The following examples show how to use org.apache.pdfbox.util.Matrix#getTranslateInstance() . 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: LegacyPDFStreamEngine.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will initialize and process the contents of the stream.
 *
 * @param page the page to process
 * @throws java.io.IOException if there is an error accessing the stream.
 */
@Override
public void processPage(PDPage page) throws IOException
{
    this.pageRotation = page.getRotation();
    this.pageSize = page.getCropBox();

    if (pageSize.getLowerLeftX() == 0 && pageSize.getLowerLeftY() == 0)
    {
        translateMatrix = null;
    }
    else
    {
        // translation matrix for cropbox
        translateMatrix = Matrix.getTranslateInstance(-pageSize.getLowerLeftX(), -pageSize.getLowerLeftY());
    }
    super.processPage(page);
}
 
Example 2
Source File: PdfDenseMergeTool.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
void merge(PDDocument sourceDoc, PDPage page) throws IOException
{
    PDRectangle pageSizeToImport = page.getCropBox();
    BoundingBoxFinder boundingBoxFinder = new BoundingBoxFinder(page);
    boundingBoxFinder.processPage(page);
    Rectangle2D boundingBoxToImport = boundingBoxFinder.getBoundingBox();
    double heightToImport = boundingBoxToImport.getHeight();
    float maxHeight = pageSize.getHeight() - topMargin - bottomMargin;
    if (heightToImport > maxHeight)
    {
        throw new IllegalArgumentException(String.format("Page %s content too large; height: %s, limit: %s.", page, heightToImport, maxHeight));
    }

    if (gap + heightToImport > yPosition - (pageSize.getLowerLeftY() + bottomMargin))
    {
        newPage();
    }
    yPosition -= heightToImport + gap;

    LayerUtility layerUtility = new LayerUtility(document);
    PDFormXObject form = layerUtility.importPageAsForm(sourceDoc, page);

    currentContents.saveGraphicsState();
    Matrix matrix = Matrix.getTranslateInstance(0, (float)(yPosition - (boundingBoxToImport.getMinY() - pageSizeToImport.getLowerLeftY())));
    currentContents.transform(matrix);
    currentContents.drawForm(form);
    currentContents.restoreGraphicsState();
}
 
Example 3
Source File: PDFExtractor.java    From inception with Apache License 2.0 4 votes vote down vote up
public PDFExtractor(PDPage page, int pageIndex, Writer output) throws IOException
{
    super(page);
    this.pageIndex = pageIndex;
    this.output = output;
    this.writeGlyphCoords = true;

    String path = "org/apache/pdfbox/resources/glyphlist/additional.txt";
    InputStream input = GlyphList.class.getClassLoader().getResourceAsStream(path);
    this.glyphList = new GlyphList(GlyphList.getAdobeGlyphList(), input);

    this.pageRotation = page.getRotation();
    this.pageSize = page.getCropBox();
    if (this.pageSize.getLowerLeftX() == 0.0F && this.pageSize.getLowerLeftY() == 0.0F) {
        this.translateMatrix = null;
    }
    else {
        this.translateMatrix = Matrix.getTranslateInstance(-this.pageSize.getLowerLeftX(),
            -this.pageSize.getLowerLeftY());
    }

    // taken from DrawPrintTextLocations for setting flipAT, rotateAT and transAT
    PDRectangle cropBox = page.getCropBox();
    // flip y-axis
    flipAT = new AffineTransform();
    flipAT.translate(0, page.getBBox().getHeight());
    flipAT.scale(1, -1);

    // page may be rotated
    rotateAT = new AffineTransform();
    int rotation = page.getRotation();
    if (rotation != 0) {
        PDRectangle mediaBox = page.getMediaBox();
        switch (rotation) {
        case 90:
            rotateAT.translate(mediaBox.getHeight(), 0);
            break;
        case 270:
            rotateAT.translate(0, mediaBox.getWidth());
            break;
        case 180:
            rotateAT.translate(mediaBox.getWidth(), mediaBox.getHeight());
            break;
        default:
            break;
        }
        rotateAT.rotate(Math.toRadians(rotation));
    }
    // cropbox
    transAT = AffineTransform
        .getTranslateInstance(-cropBox.getLowerLeftX(), cropBox.getLowerLeftY());
}
 
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: PdfVeryDenseMergeTool.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
void merge(PDDocument sourceDoc, PDPage page) throws IOException
{
    PDRectangle pageSizeToImport = page.getCropBox();

    PageVerticalAnalyzer analyzer = new PageVerticalAnalyzer(page);
    analyzer.processPage(page);
    List<Float> verticalFlips = analyzer.getVerticalFlips();
    if (verticalFlips.size() < 2)
        return;

    LayerUtility layerUtility = new LayerUtility(document);
    PDFormXObject form = layerUtility.importPageAsForm(sourceDoc, page);

    int startFlip = verticalFlips.size() - 1;
    boolean first = true;
    while (startFlip > 0)
    {
        if (!first)
            newPage();

        float freeSpace = yPosition - pageSize.getLowerLeftY() - bottomMargin;
        int endFlip = startFlip + 1;
        while ((endFlip > 1) && (verticalFlips.get(startFlip) - verticalFlips.get(endFlip - 2) < freeSpace))
            endFlip -=2;
        if (endFlip < startFlip)
        {
            float height = verticalFlips.get(startFlip) - verticalFlips.get(endFlip);

            currentContents.saveGraphicsState();
            currentContents.addRect(0, yPosition - height, pageSizeToImport.getWidth(), height);
            currentContents.clip();
            Matrix matrix = Matrix.getTranslateInstance(0, (float)(yPosition - (verticalFlips.get(startFlip) - pageSizeToImport.getLowerLeftY())));
            currentContents.transform(matrix);
            currentContents.drawForm(form);
            currentContents.restoreGraphicsState();

            yPosition -= height + gap;
            startFlip = endFlip - 1;
        }
        else if (!first) 
            throw new IllegalArgumentException(String.format("Page %s content sections too large.", page));
        first = false;
    }
}