Search in sources :

Example 51 with PDRectangle

use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.

the class TestLayerUtility method createOverlay1.

private File createOverlay1() throws IOException {
    File targetFile = new File(testResultsDir, "overlay1.pdf");
    PDDocument doc = new PDDocument();
    try {
        // Create new page
        PDPage page = new PDPage();
        doc.addPage(page);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
            page.setResources(resources);
        }
        // Setup page content stream and paint background/title
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.OVERWRITE, false);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        contentStream.setNonStrokingColor(AWTColor.LIGHT_GRAY);
        contentStream.beginText();
        float fontSize = 96;
        contentStream.setFont(font, fontSize);
        String text = "OVERLAY";
        // float sw = font.getStringWidth(text);
        // Too bad, base 14 fonts don't return character metrics.
        PDRectangle crop = page.getCropBox();
        float cx = crop.getWidth() / 2f;
        float cy = crop.getHeight() / 2f;
        Matrix transform = new Matrix();
        transform.translate(cx, cy);
        transform.rotate(Math.toRadians(45));
        transform.translate(-190, /* sw/2 */
        0);
        contentStream.setTextMatrix(transform);
        contentStream.showText(text);
        contentStream.endText();
        contentStream.close();
        doc.save(targetFile.getAbsolutePath());
    } finally {
        doc.close();
    }
    return targetFile;
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) Matrix(com.tom_roush.pdfbox.util.Matrix) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) File(java.io.File)

Example 52 with PDRectangle

use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.

the class PDFTextStripper method fillBeadRectangles.

private void fillBeadRectangles(PDPage page) {
    beadRectangles = new ArrayList<PDRectangle>();
    for (PDThreadBead bead : page.getThreadBeads()) {
        if (bead == null) {
            // can't skip, because of null entry handling in processTextPosition()
            beadRectangles.add(null);
            continue;
        }
        PDRectangle rect = bead.getRectangle();
        // bead rectangle is in PDF coordinates (y=0 is bottom),
        // glyphs are in image coordinates (y=0 is top),
        // so we must flip
        PDRectangle mediaBox = page.getMediaBox();
        float upperRightY = mediaBox.getUpperRightY() - rect.getLowerLeftY();
        float lowerLeftY = mediaBox.getUpperRightY() - rect.getUpperRightY();
        rect.setLowerLeftY(lowerLeftY);
        rect.setUpperRightY(upperRightY);
        // adjust for cropbox
        PDRectangle cropBox = page.getCropBox();
        if (cropBox.getLowerLeftX() != 0 || cropBox.getLowerLeftY() != 0) {
            rect.setLowerLeftX(rect.getLowerLeftX() - cropBox.getLowerLeftX());
            rect.setLowerLeftY(rect.getLowerLeftY() - cropBox.getLowerLeftY());
            rect.setUpperRightX(rect.getUpperRightX() - cropBox.getLowerLeftX());
            rect.setUpperRightY(rect.getUpperRightY() - cropBox.getLowerLeftY());
        }
        beadRectangles.add(rect);
    }
}
Also used : PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDThreadBead(com.tom_roush.pdfbox.pdmodel.interactive.pagenavigation.PDThreadBead)

Example 53 with PDRectangle

use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.

the class PageDrawer method shadingFill.

@Override
public void shadingFill(COSName shadingName) throws IOException {
    PDShading shading = getResources().getShading(shadingName);
    if (shading == null) {
        Log.e("PdfBox-Android", "shading " + shadingName + " does not exist in resources dictionary");
        return;
    }
    Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
    // Paint paint = shading.toPaint(ctm);
    // paint = applySoftMaskToPaint(paint, getGraphicsState().getSoftMask());
    // graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
    // graphics.setPaint(paint);
    // graphics.setClip(null);
    // lastClip = null;
    // get the transformed BBox and intersect with current clipping path
    // need to do it here and not in shading getRaster() because it may have been rotated
    PDRectangle bbox = shading.getBBox();
    if (bbox != null) {
    // Area bboxArea = new Area(bbox.transform(ctm));
    // bboxArea.intersect(getGraphicsState().getCurrentClippingPath());
    // graphics.fill(bboxArea);
    } else {
    // graphics.fill(getGraphicsState().getCurrentClippingPath());
    }
}
Also used : PDShading(com.tom_roush.pdfbox.pdmodel.graphics.shading.PDShading) Matrix(com.tom_roush.pdfbox.util.Matrix) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 54 with PDRectangle

use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.

the class Overlay method createOverlayStream.

private COSStream createOverlayStream(PDPage page, LayoutPage layoutPage, COSName xObjectId) throws IOException {
    // create a new content stream that executes the XObject content
    StringBuilder overlayStream = new StringBuilder();
    overlayStream.append("q\nq\n");
    PDRectangle overlayMediaBox = new PDRectangle(layoutPage.overlayMediaBox.getCOSArray());
    if (layoutPage.overlayRotation == 90 || layoutPage.overlayRotation == 270) {
        overlayMediaBox.setLowerLeftX(layoutPage.overlayMediaBox.getLowerLeftY());
        overlayMediaBox.setLowerLeftY(layoutPage.overlayMediaBox.getLowerLeftX());
        overlayMediaBox.setUpperRightX(layoutPage.overlayMediaBox.getUpperRightY());
        overlayMediaBox.setUpperRightY(layoutPage.overlayMediaBox.getUpperRightX());
    }
    AffineTransform at = calculateAffineTransform(page, overlayMediaBox);
    double[] flatmatrix = new double[6];
    at.getMatrix(flatmatrix);
    for (double v : flatmatrix) {
        overlayStream.append(float2String((float) v));
        overlayStream.append(" ");
    }
    overlayStream.append(" cm\n");
    // if debugging, insert
    // 0 0 overlayMediaBox.getHeight() overlayMediaBox.getWidth() re\ns\n
    // into the content stream
    overlayStream.append(" /");
    overlayStream.append(xObjectId.getName());
    overlayStream.append(" Do Q\nQ\n");
    return createStream(overlayStream.toString());
}
Also used : AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 55 with PDRectangle

use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.

the class Overlay method calculateAffineTransform.

/**
 * Calculate the transform to be used when positioning the overlay. The default implementation
 * centers on the destination. Override this method to do your own, e.g. move to a corner, or
 * rotate.
 *
 * @param page The page that will get the overlay.
 * @param overlayMediaBox The overlay media box.
 * @return The affine transform to be used.
 */
protected AffineTransform calculateAffineTransform(PDPage page, PDRectangle overlayMediaBox) {
    AffineTransform at = new AffineTransform();
    PDRectangle pageMediaBox = page.getMediaBox();
    float hShift = (pageMediaBox.getWidth() - overlayMediaBox.getWidth()) / 2.0f;
    float vShift = (pageMediaBox.getHeight() - overlayMediaBox.getHeight()) / 2.0f;
    at.translate(hShift, vShift);
    return at;
}
Also used : AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) 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