Example usage for com.lowagie.text.pdf PdfContentByte clip

List of usage examples for com.lowagie.text.pdf PdfContentByte clip

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfContentByte clip.

Prototype


public void clip() 

Source Link

Document

Modify the current clipping path by intersecting it with the current path, using the nonzero winding number rule to determine which regions lie inside the clipping path.

Usage

From source file:org.mapfish.print.Transformer.java

License:Open Source License

public void setClipping(PdfContentByte dc) {
    dc.rectangle(paperPosX, paperPosY, paperWidth, paperHeight);
    dc.clip();
    dc.newPath();
}

From source file:org.xhtmlrenderer.pdf.ITextOutputDevice.java

License:Open Source License

private void followPath(Shape s, int drawType) {
    PdfContentByte cb = _currentPage;
    if (s == null)
        return;/*  w  w w. ja va  2 s  .co m*/

    if (drawType == STROKE) {
        if (!(_stroke instanceof BasicStroke)) {
            s = _stroke.createStrokedShape(s);
            followPath(s, FILL);
            return;
        }
    }
    if (drawType == STROKE) {
        setStrokeDiff(_stroke, _oldStroke);
        _oldStroke = _stroke;
        ensureStrokeColor();
    } else if (drawType == FILL) {
        ensureFillColor();
    }

    PathIterator points;
    if (drawType == CLIP) {
        points = s.getPathIterator(IDENTITY);
    } else {
        points = s.getPathIterator(_transform);
    }
    float[] coords = new float[6];
    int traces = 0;
    while (!points.isDone()) {
        ++traces;
        int segtype = points.currentSegment(coords);
        normalizeY(coords);
        switch (segtype) {
        case PathIterator.SEG_CLOSE:
            cb.closePath();
            break;

        case PathIterator.SEG_CUBICTO:
            cb.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;

        case PathIterator.SEG_LINETO:
            cb.lineTo(coords[0], coords[1]);
            break;

        case PathIterator.SEG_MOVETO:
            cb.moveTo(coords[0], coords[1]);
            break;

        case PathIterator.SEG_QUADTO:
            System.out.println("Quad to " + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3]);
            cb.curveTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        }
        points.next();
    }

    switch (drawType) {
    case FILL:
        if (traces > 0) {
            if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD)
                cb.eoFill();
            else
                cb.fill();
        }
        break;
    case STROKE:
        if (traces > 0)
            cb.stroke();
        break;
    default: // drawType==CLIP
        if (traces == 0)
            cb.rectangle(0, 0, 0, 0);
        if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD)
            cb.eoClip();
        else
            cb.clip();
        cb.newPath();
    }
}

From source file:questions.images.TransparentEllipse2.java

public static void main(String[] args) {

    Document document = new Document(PageSize.POSTCARD);
    try {//from w  w  w  .j a  va2  s  .  c o m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        // clipped image
        cb.ellipse(1, 1, PageSize.POSTCARD.getWidth() - 2, PageSize.POSTCARD.getHeight() - 2);
        cb.clip();
        cb.newPath();
        Image img = Image.getInstance(RESOURCE);
        img.scaleToFit(PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
        cb.addImage(img, PageSize.POSTCARD.getWidth(), 0, 0, PageSize.POSTCARD.getHeight(), 0, 0);

        //Prepare gradation list
        int gradationStep = 40;
        float[] gradationRatioList = new float[gradationStep];
        for (int i = 0; i < gradationStep; i++) {
            gradationRatioList[i] = 1 - (float) Math.sin(Math.toRadians(90.0f / gradationStep * (i + 1)));
        }

        //Create template
        PdfTemplate template = cb.createTemplate(PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());

        //Prepare transparent group
        PdfTransparencyGroup transGroup = new PdfTransparencyGroup();
        transGroup.put(PdfName.CS, PdfName.DEVICEGRAY);
        transGroup.setIsolated(true);
        transGroup.setKnockout(false);
        template.setGroup(transGroup);

        //Prepare graphic state
        PdfGState gState = new PdfGState();
        PdfDictionary maskDict = new PdfDictionary();
        maskDict.put(PdfName.TYPE, PdfName.MASK);
        maskDict.put(PdfName.S, new PdfName("Luminosity"));
        maskDict.put(new PdfName("G"), template.getIndirectReference());
        gState.put(PdfName.SMASK, maskDict);
        cb.setGState(gState);

        //Create gradation for mask
        for (int i = 1; i < gradationStep + 1; i++) {
            template.setLineWidth(gradationStep + 1 - i);
            template.setGrayStroke(gradationRatioList[gradationStep - i]);
            template.ellipse(0, 0, PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
            template.stroke();
        }

        //Place template
        cb.addTemplate(template, 0, 0);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    document.close();
}

From source file:questions.stamppages.AddCropbox.java

public static void main(String[] args) {
    try {//ww w.  jav  a2s.  c o m
        PdfImportedPage page;
        PdfDictionary pageDict;
        PdfReader reader = new PdfReader(RESOURCE);
        Document document = new Document(reader.getPageSizeWithRotation(1));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT2));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++) {
            page = writer.getImportedPage(reader, 1);
            cb.rectangle(0, 52, 612, 668);
            cb.clip();
            cb.newPath();
            cb.addTemplate(page, 0, 0);
            pageDict = reader.getPageN(i);
            PdfArray crop = new PdfRectangle(0, 52, 612, 720);
            pageDict.put(PdfName.CROPBOX, crop);
        }
        document.close();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT1));
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}