Example usage for org.apache.pdfbox.pdmodel.interactive.annotation PDAppearanceStream PDAppearanceStream

List of usage examples for org.apache.pdfbox.pdmodel.interactive.annotation PDAppearanceStream PDAppearanceStream

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.annotation PDAppearanceStream PDAppearanceStream.

Prototype

public PDAppearanceStream(PDDocument document) 

Source Link

Document

Creates a Form Image XObject for writing, in the given document.

Usage

From source file:airviewer.AnnotationGenerator.java

/**
 *
 * @param a//from www .j  a va2 s  .  c  o m
 * @param d
 * @param p
 * @param shouldResize
 * @return
 */
public static PDAppearanceStream generateSquareAppearance(PDAnnotation a, PDDocument d, PDPage p,
        boolean shouldResize) {
    assert null != a;
    assert null != d;
    assert null != p;

    PDAppearanceStream annotationAppearanceStream = null;

    try {
        if (shouldResize) {
            resizeAnnotationToContent(a);
        }

        final String contents = a.getContents();
        final boolean hasContents = null != contents && 0 < contents.length();
        float borderWidth = 0;
        if (a instanceof PDAnnotationMarkup) {
            final PDBorderStyleDictionary borderStyle = ((PDAnnotationMarkup) a).getBorderStyle();
            if (null != a.getColor() && null != borderStyle) {
                borderWidth = Math.abs(borderStyle.getWidth());
            }
        }
        final float fontSize = FONT_SIZE_PDF_POINTS;
        final float textHeight = fontSize;
        final float margin = MARGIN_SIZE_PDF_POINTS;

        PDRectangle position = a.getRectangle();
        final float lowerLeftX = position.getLowerLeftX();
        final float lowerLeftY = position.getLowerLeftY();
        float width = position.getWidth();
        float height = position.getHeight();

        annotationAppearanceStream = new PDAppearanceStream(d);

        annotationAppearanceStream.setBBox(position);
        annotationAppearanceStream.setMatrix(new AffineTransform());
        annotationAppearanceStream.setResources(p.getResources());

        try (PDPageContentStream appearanceContent = new PDPageContentStream(d, annotationAppearanceStream)) {
            appearanceContent.transform(new Matrix()); // Identity transform

            // Rect is inset by half border width to prevent border leaking
            // outside bounding box
            final float insetLowerLeftX = lowerLeftX + borderWidth * 0.5f;
            final float insetLowerLeftY = lowerLeftY + borderWidth * 0.5f;
            final float insetWidth = width - borderWidth;
            final float insetheight = height - borderWidth;
            appearanceContent.addRect(insetLowerLeftX, insetLowerLeftY, insetWidth, insetheight);
            appearanceContent.setLineWidth(borderWidth);
            appearanceContent.setNonStrokingColor(GRAY);

            if (null != a.getColor() && 0 < borderWidth) {
                appearanceContent.setStrokingColor(a.getColor());
                appearanceContent.fillAndStroke();
            } else {
                appearanceContent.fill();

            }

            if (hasContents) {
                appearanceContent.moveTo(0, 0);
                appearanceContent.beginText();

                // Center vertically, left justified inside border with margin
                appearanceContent.newLineAtOffset(lowerLeftX + borderWidth + margin,
                        lowerLeftY + (height + LINE_SPACE_SIZE_PDF_POINTS) * 0.5f - textHeight * 0.5f);
                appearanceContent.setFont(FONT, fontSize);
                if (null != a.getColor()) {
                    appearanceContent.setNonStrokingColor(a.getColor()); // Sets color of text
                } else {
                    appearanceContent.setNonStrokingColor(BLACK); // Sets color of text

                }
                appearanceContent.showText(a.getContents());
                appearanceContent.endText();
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(AnnotationGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }

    return annotationAppearanceStream;
}

From source file:airviewer.AnnotationGenerator.java

/**
 *
 * @param a/*from www  .  java 2  s  .co  m*/
 * @param d
 * @param p
 * @param shouldResize
 * @return
 */
public static PDAppearanceStream generateCircleAppearance(PDAnnotation a, PDDocument d, PDPage p,
        boolean shouldResize) {
    assert null != a;
    assert null != d;
    assert null != p;

    PDAppearanceStream annotationAppearanceStream = null;

    try {
        if (shouldResize) {
            resizeAnnotationToContent(a);
        }

        final String contents = a.getContents();
        final boolean hasContents = null != contents && 0 < contents.length();
        float borderWidth = 0;
        if (a instanceof PDAnnotationMarkup) {
            final PDBorderStyleDictionary borderStyle = ((PDAnnotationMarkup) a).getBorderStyle();
            if (null != a.getColor() && null != borderStyle) {
                borderWidth = Math.abs(borderStyle.getWidth());
            }
        }
        final float fontSize = FONT_SIZE_PDF_POINTS;
        final float textHeight = fontSize;
        final float margin = MARGIN_SIZE_PDF_POINTS;

        PDRectangle position = a.getRectangle();
        final float lowerLeftX = position.getLowerLeftX();
        final float lowerLeftY = position.getLowerLeftY();
        float width = position.getWidth();
        float height = position.getHeight();

        annotationAppearanceStream = new PDAppearanceStream(d);

        annotationAppearanceStream.setBBox(position);
        annotationAppearanceStream.setMatrix(new AffineTransform());
        annotationAppearanceStream.setResources(p.getResources());

        try (PDPageContentStream appearanceContent = new PDPageContentStream(d, annotationAppearanceStream)) {
            appearanceContent.transform(new Matrix()); // Identity transform

            // Rect is inset by half border width to prevent border leaking
            // outside bounding box
            final float insetLowerLeftX = lowerLeftX + borderWidth * 0.5f;
            final float insetLowerLeftY = lowerLeftY + borderWidth * 0.5f;
            final float insetWidth = width - borderWidth;
            final float insetheight = height - borderWidth;

            if (null != a.getColor()) {
                appearanceContent.setLineWidth(borderWidth);
                appearanceContent.moveTo(insetLowerLeftX, insetLowerLeftY + insetheight * 0.5f);
                appearanceContent.curveTo(insetLowerLeftX, insetLowerLeftY + insetheight * 0.75f,
                        insetLowerLeftX + insetWidth * 0.25f, insetLowerLeftY + insetheight,
                        insetLowerLeftX + insetWidth * 0.5f, insetLowerLeftY + insetheight);
                appearanceContent.curveTo(insetLowerLeftX + insetWidth * 0.75f, insetLowerLeftY + insetheight,
                        insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.75f,
                        insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.5f);
                appearanceContent.curveTo(insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.25f,
                        insetLowerLeftX + insetWidth * 0.75f, insetLowerLeftY,
                        insetLowerLeftX + insetWidth * 0.5f, insetLowerLeftY);
                appearanceContent.curveTo(insetLowerLeftX + insetWidth * 0.25f, insetLowerLeftY,
                        insetLowerLeftX, insetLowerLeftY + insetheight * 0.25f, insetLowerLeftX,
                        insetLowerLeftY + insetheight * 0.5f);
            }
            appearanceContent.setNonStrokingColor(GRAY);

            if (null != a.getColor() && 0 < borderWidth) {
                appearanceContent.setStrokingColor(a.getColor());
                appearanceContent.fillAndStroke();
            } else {
                appearanceContent.fill();

            }

            if (hasContents) {
                appearanceContent.moveTo(0, 0);
                appearanceContent.beginText();

                // Center text vertically, left justified inside border with margin
                appearanceContent.newLineAtOffset(lowerLeftX + borderWidth + margin,
                        lowerLeftY + (height + LINE_SPACE_SIZE_PDF_POINTS) * 0.5f - textHeight * 0.5f);
                appearanceContent.setFont(FONT, fontSize);
                appearanceContent.setNonStrokingColor(BLACK); // Sets color of text
                appearanceContent.showText(a.getContents());
                appearanceContent.endText();
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(AnnotationGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }

    return annotationAppearanceStream;
}

From source file:airviewer.BoxAnnotationMaker.java

License:Apache License

/**
 *
 * @param document/*from w  w  w . j  a v  a 2 s.c om*/
 * @param arguments(lowerLeftX, lowerLeftY, width, height)
 * @return
 */
public static List<PDAnnotation> make(PDDocument document, ArrayList<String> arguments) {
    assert null != arguments && arguments.size() == 5;
    assert null != document;

    List<PDAnnotation> result;

    try {
        int pageNumber = parseInt(arguments.get(0));
        float lowerLeftX = parseFloat(arguments.get(1));
        float lowerLeftY = parseFloat(arguments.get(2));
        float width = parseFloat(arguments.get(3));
        float height = parseFloat(arguments.get(4));
        String contents = "";
        PDFont font = PDType1Font.HELVETICA_OBLIQUE;
        float fontSize = 16; // Or whatever font size you want.
        //float textWidth = font.getStringWidth(contents) * fontSize / 1000.0f;
        //float textHeight = 32;

        try {
            PDPage page = document.getPage(pageNumber);
            PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE);
            PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
            borderThick.setWidth(72 / 12); // 12th inch
            PDRectangle position = new PDRectangle();
            position.setLowerLeftX(lowerLeftX);
            position.setLowerLeftY(lowerLeftY);
            position.setUpperRightX(lowerLeftX + width);
            position.setUpperRightY(lowerLeftY + height);

            PDAnnotationSquareCircle aSquare = new PDAnnotationSquareCircle(
                    PDAnnotationSquareCircle.SUB_TYPE_SQUARE);
            aSquare.setAnnotationName(new UID().toString());
            aSquare.setContents(contents);
            aSquare.setColor(red); // Outline in red, not setting a fill
            PDColor fillColor = new PDColor(new float[] { .8f, .8f, .8f }, PDDeviceRGB.INSTANCE);
            aSquare.setInteriorColor(fillColor);
            aSquare.setBorderStyle(borderThick);
            aSquare.setRectangle(position);
            result = new ArrayList<>(page.getAnnotations()); // copy
            page.getAnnotations().add(aSquare);

            // The following lines are needed for PDFRenderer to render 
            // annotations. Preview and Acrobat don't seem to need these.
            if (null == aSquare.getAppearance()) {
                aSquare.setAppearance(new PDAppearanceDictionary());
                PDAppearanceStream annotationAppearanceStream = new PDAppearanceStream(document);
                position.setLowerLeftX(lowerLeftX - borderThick.getWidth() * 0.5f);
                position.setLowerLeftY(lowerLeftY - borderThick.getWidth() * 0.5f);
                position.setUpperRightX(lowerLeftX + width + borderThick.getWidth() * 0.5f);
                position.setUpperRightY(lowerLeftY + height + borderThick.getWidth() * 0.5f);
                annotationAppearanceStream.setBBox(position);
                annotationAppearanceStream.setMatrix(new AffineTransform());
                annotationAppearanceStream.setResources(page.getResources());

                try (PDPageContentStream appearanceContent = new PDPageContentStream(document,
                        annotationAppearanceStream)) {
                    Matrix transform = new Matrix();
                    appearanceContent.transform(transform);
                    appearanceContent.addRect(lowerLeftX, lowerLeftY, width, height);
                    appearanceContent.setLineWidth(borderThick.getWidth());
                    appearanceContent.setNonStrokingColor(fillColor);
                    appearanceContent.setStrokingColor(red);
                    appearanceContent.fillAndStroke();
                    appearanceContent.beginText();

                    // Center text vertically, left justified
                    appearanceContent.newLineAtOffset(lowerLeftX, lowerLeftY + height * 0.5f - fontSize * 0.5f);
                    appearanceContent.setFont(font, fontSize);
                    appearanceContent.setNonStrokingColor(red);
                    appearanceContent.showText(contents);
                    appearanceContent.endText();
                }
                aSquare.getAppearance().setNormalAppearance(annotationAppearanceStream);
            }
            //System.out.println(page.getAnnotations().toString());

        } catch (IOException ex) {
            Logger.getLogger(DocumentCommandWrapper.class.getName()).log(Level.SEVERE, null, ex);
            result = null;
        }
    } catch (NumberFormatException | NullPointerException ex) {
        System.err.println("\tNon number encountered where floating point number expected.");
        result = null;
    }

    return result;
}

From source file:airviewer.EllipseAnnotationMaker.java

License:Apache License

/**
 * // w  w w.j av  a2  s .c o  m
 * @param document
 * @param arguments(pageNumber, lowerLeftX, lowerLeftY, width, height, contents)
 * @return 
 */
public static List<PDAnnotation> make(PDDocument document, ArrayList<String> arguments) {
    assert null != arguments && arguments.size() == 6;
    assert null != document;

    List<PDAnnotation> result;

    try {
        int pageNumber = parseInt(arguments.get(0));
        float lowerLeftX = parseFloat(arguments.get(1));
        float lowerLeftY = parseFloat(arguments.get(2));
        float width = parseFloat(arguments.get(3));
        float height = parseFloat(arguments.get(4));
        String contents = arguments.get(5);

        PDFont font = PDType1Font.HELVETICA_OBLIQUE;
        final float fontSize = 16.0f; // Or whatever font size you want.
        //final float lineSpacing = 4.0f;
        width = max(width, font.getStringWidth(contents) * fontSize / 1000.0f);
        //final float textHeight = fontSize + lineSpacing;

        try {
            PDPage page = document.getPage(pageNumber);
            PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE);
            PDColor black = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE);
            PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
            borderThick.setWidth(72 / 12); // 12th inch
            PDRectangle position = new PDRectangle();
            position.setLowerLeftX(lowerLeftX);
            position.setLowerLeftY(lowerLeftY);
            position.setUpperRightX(lowerLeftX + width);
            position.setUpperRightY(lowerLeftY + height);

            PDAnnotationSquareCircle aCircle = new PDAnnotationSquareCircle(
                    PDAnnotationSquareCircle.SUB_TYPE_CIRCLE);
            aCircle.setAnnotationName(new UID().toString());
            aCircle.setContents(contents);
            PDColor fillColor = new PDColor(new float[] { .8f, .8f, .8f }, PDDeviceRGB.INSTANCE);
            aCircle.setInteriorColor(fillColor);
            aCircle.setColor(red);
            aCircle.setBorderStyle(borderThick);
            aCircle.setRectangle(position);

            result = new ArrayList<>(page.getAnnotations()); // Copy
            page.getAnnotations().add(aCircle);

            // The following lines are needed for PDFRenderer to render 
            // annotations. Preview and Acrobat don't seem to need these.
            if (null == aCircle.getAppearance()) {
                aCircle.setAppearance(new PDAppearanceDictionary());
                PDAppearanceStream annotationAppearanceStream = new PDAppearanceStream(document);
                position.setLowerLeftX(lowerLeftX - borderThick.getWidth() * 0.5f);
                position.setLowerLeftY(lowerLeftY - borderThick.getWidth() * 0.5f);
                position.setUpperRightX(lowerLeftX + width + borderThick.getWidth() * 0.5f);
                position.setUpperRightY(lowerLeftY + height + borderThick.getWidth() * 0.5f);
                annotationAppearanceStream.setBBox(position);
                annotationAppearanceStream.setMatrix(new AffineTransform());
                annotationAppearanceStream.setResources(page.getResources());

                try (PDPageContentStream appearanceContent = new PDPageContentStream(document,
                        annotationAppearanceStream)) {
                    Matrix transform = new Matrix();
                    appearanceContent.transform(transform);
                    appearanceContent.moveTo(lowerLeftX, lowerLeftY + height * 0.5f);
                    appearanceContent.curveTo(lowerLeftX, lowerLeftY + height * 0.75f,
                            lowerLeftX + width * 0.25f, lowerLeftY + height, lowerLeftX + width * 0.5f,
                            lowerLeftY + height);
                    appearanceContent.curveTo(lowerLeftX + width * 0.75f, lowerLeftY + height,
                            lowerLeftX + width, lowerLeftY + height * 0.75f, lowerLeftX + width,
                            lowerLeftY + height * 0.5f);
                    appearanceContent.curveTo(lowerLeftX + width, lowerLeftY + height * 0.25f,
                            lowerLeftX + width * 0.75f, lowerLeftY, lowerLeftX + width * 0.5f, lowerLeftY);
                    appearanceContent.curveTo(lowerLeftX + width * 0.25f, lowerLeftY, lowerLeftX,
                            lowerLeftY + height * 0.25f, lowerLeftX, lowerLeftY + height * 0.5f);
                    appearanceContent.setLineWidth(borderThick.getWidth());
                    appearanceContent.setNonStrokingColor(fillColor);
                    appearanceContent.setStrokingColor(red);
                    appearanceContent.fillAndStroke();
                    appearanceContent.moveTo(0, 0);

                    appearanceContent.beginText();
                    appearanceContent.setNonStrokingColor(black);
                    // Center text vertically, left justified
                    appearanceContent.newLineAtOffset(lowerLeftX + borderThick.getWidth(),
                            lowerLeftY + height * 0.5f - fontSize * 0.5f);
                    appearanceContent.setFont(font, fontSize);
                    appearanceContent.showText(contents);
                    appearanceContent.endText();
                }
                aCircle.getAppearance().setNormalAppearance(annotationAppearanceStream);
            }

        } catch (IOException ex) {
            Logger.getLogger(DocumentCommandWrapper.class.getName()).log(Level.SEVERE, null, ex);
            result = null;
        }
    } catch (NumberFormatException | NullPointerException ex) {
        System.err.println("Non number encountered where floating point number expected.");
        result = null;
    } catch (IOException ex) {
        Logger.getLogger(EllipseAnnotationMaker.class.getName()).log(Level.SEVERE, null, ex);
        result = null;
    }

    return result;
}

From source file:airviewer.TextAnnotationMaker.java

License:Apache License

/**
 * //w w  w . j  av  a2s . c  o  m
 * @param document
 * @param arguments(pageNumber, lowerLeftX, lowerLeftY);
        
    String contents 
 * @return 
 */
public static List<PDAnnotation> make(PDDocument document, ArrayList<String> arguments) {
    assert null != arguments && arguments.size() == 4;
    assert null != document;

    List<PDAnnotation> result;

    try {
        int pageNumber = parseInt(arguments.get(0));
        float lowerLeftX = parseFloat(arguments.get(1));
        float lowerLeftY = parseFloat(arguments.get(2));

        String contents = arguments.get(3);
        PDFont font = PDType1Font.HELVETICA_OBLIQUE;
        final float fontSize = 16.0f; // Or whatever font size you want.
        final float lineSpacing = 4.0f;
        float width = font.getStringWidth(contents) * fontSize / 1000.0f; // font.getStringWidth(contents) returns thousanths of PS point
        final float textHeight = fontSize + lineSpacing;

        try {
            PDPage page = document.getPage(pageNumber);
            PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE);
            PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
            borderThick.setWidth(72 / 12); // 12th inch
            PDRectangle position = new PDRectangle();
            position.setLowerLeftX(lowerLeftX);
            position.setLowerLeftY(lowerLeftY);
            position.setUpperRightX(lowerLeftX + width);
            position.setUpperRightY(lowerLeftY + textHeight);

            PDAnnotationSquareCircle aSquare = new PDAnnotationSquareCircle(
                    PDAnnotationSquareCircle.SUB_TYPE_SQUARE);
            aSquare.setAnnotationName(new UID().toString());
            aSquare.setContents(contents);
            PDColor fillColor = new PDColor(new float[] { .8f, .8f, .8f }, PDDeviceRGB.INSTANCE);
            aSquare.setInteriorColor(fillColor);
            aSquare.setRectangle(position);
            result = new ArrayList<>(page.getAnnotations()); // copy
            page.getAnnotations().add(aSquare);

            // The following lines are needed for PDFRenderer to render 
            // annotations. Preview and Acrobat don't seem to need these.
            if (null == aSquare.getAppearance()) {
                aSquare.setAppearance(new PDAppearanceDictionary());
                PDAppearanceStream annotationAppearanceStream = new PDAppearanceStream(document);
                position.setLowerLeftX(lowerLeftX - borderThick.getWidth() * 0.5f);
                position.setLowerLeftY(lowerLeftY - borderThick.getWidth() * 0.5f);
                position.setUpperRightX(lowerLeftX + width + borderThick.getWidth() * 0.5f);
                position.setUpperRightY(lowerLeftY + textHeight + borderThick.getWidth() * 0.5f);
                annotationAppearanceStream.setBBox(position);
                annotationAppearanceStream.setMatrix(new AffineTransform());
                annotationAppearanceStream.setResources(page.getResources());

                try (PDPageContentStream appearanceContent = new PDPageContentStream(document,
                        annotationAppearanceStream)) {
                    Matrix transform = new Matrix();
                    appearanceContent.transform(transform);
                    appearanceContent.addRect(lowerLeftX, lowerLeftY, width, textHeight);
                    appearanceContent.setNonStrokingColor(fillColor);
                    appearanceContent.fill();
                    appearanceContent.beginText();

                    // Center text vertically, left justified
                    appearanceContent.newLineAtOffset(lowerLeftX,
                            lowerLeftY + textHeight * 0.5f - fontSize * 0.5f);
                    appearanceContent.setFont(font, fontSize);
                    appearanceContent.setNonStrokingColor(red);
                    appearanceContent.showText(contents);
                    appearanceContent.endText();
                }
                aSquare.getAppearance().setNormalAppearance(annotationAppearanceStream);
            }
            //System.out.println(page.getAnnotations().toString());

        } catch (IOException ex) {
            Logger.getLogger(DocumentCommandWrapper.class.getName()).log(Level.SEVERE, null, ex);
            result = null;
        }
    } catch (NumberFormatException | NullPointerException ex) {
        System.err.println("Non number encountered where floating point number expected.");
        result = null;
    } catch (IOException ex) {
        Logger.getLogger(TextAnnotationMaker.class.getName()).log(Level.SEVERE, null, ex);
        result = null;
    }

    return result;
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createAppearanceDictionary(PDXObjectForm holderForml, PDSignatureField signatureField,
        float degrees) throws IOException {

    PDAppearanceDictionary appearance = new PDAppearanceDictionary();
    appearance.getCOSObject().setDirect(true);

    PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForml.getCOSStream());
    AffineTransform transform = new AffineTransform();
    transform.setToIdentity();//from w  ww.  j  a  v  a2  s. c  o m
    transform.rotate(Math.toRadians(degrees));
    appearanceStream.setMatrix(transform);
    appearance.setNormalAppearance(appearanceStream);
    signatureField.getWidget().setAppearance(appearance);

    getStructure().setAppearanceDictionary(appearance);
    logger.debug("PDF appearance Dictionary has been created");

}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createAppearanceDictionary(PDFormXObject holderForml, PDSignatureField signatureField,
        float degrees) throws IOException {

    PDAppearanceDictionary appearance = new PDAppearanceDictionary();
    appearance.getCOSObject().setDirect(true);

    PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForml.getCOSStream());
    AffineTransform transform = new AffineTransform();
    transform.setToIdentity();/*from  ww w  .java2 s  .  c om*/
    transform.rotate(Math.toRadians(degrees));
    appearanceStream.setMatrix(transform);
    appearance.setNormalAppearance(appearanceStream);
    signatureField.getWidget().setAppearance(appearance);

    getStructure().setAppearanceDictionary(appearance);
    logger.debug("PDF appereance Dictionary has been created");

}

From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.java

License:Apache License

PdfBoxGraphics2D(PDDocument document, PDRectangle bbox, PdfBoxGraphics2D parentGfx) throws IOException {
    this.document = document;
    this.bbox = bbox;

    PDAppearanceStream appearance = new PDAppearanceStream(document);
    xFormObject = appearance;// ww w  .j a va  2 s .co  m
    xFormObject.setResources(new PDResources());
    xFormObject.setBBox(bbox);
    contentStream = new PDPageContentStream(document, appearance,
            xFormObject.getStream().createOutputStream(COSName.FLATE_DECODE));
    contentStreamSaveState();

    if (parentGfx != null) {
        this.colorMapper = parentGfx.colorMapper;
        this.fontTextDrawer = parentGfx.fontTextDrawer;
        this.imageEncoder = parentGfx.imageEncoder;
        this.paintApplier = parentGfx.paintApplier;
    }

    baseTransform = new AffineTransform();
    baseTransform.translate(0, bbox.getHeight());
    baseTransform.scale(1, -1);

    calcImage = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
    calcGfx = calcImage.createGraphics();
    font = calcGfx.getFont();
    copyInfo = null;

}

From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.java

License:Apache License

private void applyShadingAsColor(PDShading shading) throws IOException {
    /*/*from   ww w. j av a 2  s  .c  o  m*/
     * If the paint has a shading we must create a tiling pattern and set that as
     * stroke color...
     */
    PDTilingPattern pattern = new PDTilingPattern();
    pattern.setPaintType(PDTilingPattern.PAINT_COLORED);
    pattern.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING_FASTER_TILING);
    PDRectangle anchorRect = bbox;
    pattern.setBBox(anchorRect);
    pattern.setXStep(anchorRect.getWidth());
    pattern.setYStep(anchorRect.getHeight());

    PDAppearanceStream appearance = new PDAppearanceStream(this.document);
    appearance.setResources(pattern.getResources());
    appearance.setBBox(pattern.getBBox());

    PDPageContentStream imageContentStream = new PDPageContentStream(document, appearance,
            ((COSStream) pattern.getCOSObject()).createOutputStream());
    imageContentStream.addRect(0, 0, anchorRect.getWidth(), anchorRect.getHeight());
    imageContentStream.clip();
    imageContentStream.shadingFill(shading);
    imageContentStream.close();

    PDColorSpace patternCS1 = new PDPattern(null);
    COSName tilingPatternName = xFormObject.getResources().add(pattern);
    PDColor patternColor = new PDColor(tilingPatternName, patternCS1);

    contentStream.setNonStrokingColor(patternColor);
    contentStream.setStrokingColor(patternColor);
}