Example usage for org.apache.pdfbox.pdmodel.graphics.pattern PDTilingPattern getResources

List of usage examples for org.apache.pdfbox.pdmodel.graphics.pattern PDTilingPattern getResources

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.graphics.pattern PDTilingPattern getResources.

Prototype

@Override
public PDResources getResources() 

Source Link

Document

This will get the resources for this pattern.

Usage

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

License:Apache License

private void applyShadingAsColor(PDShading shading) throws IOException {
    /*//  www  . j a v  a2  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);
}