Java Code Examples for org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState#setNonStrokingAlphaConstant()

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState#setNonStrokingAlphaConstant() . 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: PDAbstractAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
void setOpacity(PDAppearanceContentStream contentStream, float opacity) throws IOException
{
    if (opacity < 1)
    {
        PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
        gs.setStrokingAlphaConstant(opacity);
        gs.setNonStrokingAlphaConstant(opacity);

        contentStream.setGraphicsStateParameters(gs);
    }
}
 
Example 2
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void drawCircles(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 20);

    // strategy used by Adobe:
    // 1) add small circle in white using /ca /CA 0.6 and width 1
    // 2) fill
    // 3) add small circle in one direction
    // 4) add large circle in other direction
    // 5) stroke + fill
    // with square width 20 small r = 6.36, large r = 9.756

    float smallR = 6.36f;
    float largeR = 9.756f;

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    contentStream.saveGraphicsState();
    contentStream.setLineWidth(1);
    PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
    gs.setAlphaSourceFlag(false);
    gs.setStrokingAlphaConstant(0.6f);
    gs.setNonStrokingAlphaConstant(0.6f);
    gs.setBlendMode(BlendMode.NORMAL);
    contentStream.setGraphicsStateParameters(gs);
    contentStream.setNonStrokingColor(1f);
    drawCircle(contentStream, bbox.getWidth() / 2, bbox.getHeight() / 2, smallR);
    contentStream.fill();
    contentStream.restoreGraphicsState();

    contentStream.setLineWidth(0.59f); // value from Adobe
    drawCircle(contentStream, bbox.getWidth() / 2, bbox.getHeight() / 2, smallR);
    drawCircle2(contentStream, bbox.getWidth() / 2, bbox.getHeight() / 2, largeR);
    contentStream.fillAndStroke();
}
 
Example 3
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private void drawHelp(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 20);

    float min = Math.min(bbox.getWidth(), bbox.getHeight());

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(0.59f); // value from Adobe

    // Adobe first fills a white circle with CA ca 0.6, so do we
    contentStream.saveGraphicsState();
    contentStream.setLineWidth(1);
    PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
    gs.setAlphaSourceFlag(false);
    gs.setStrokingAlphaConstant(0.6f);
    gs.setNonStrokingAlphaConstant(0.6f);
    gs.setBlendMode(BlendMode.NORMAL);
    contentStream.setGraphicsStateParameters(gs);
    contentStream.setNonStrokingColor(1f);
    drawCircle2(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.fill();
    contentStream.restoreGraphicsState();

    contentStream.saveGraphicsState();
    // rescale so that "?" fits into circle and move "?" to circle center
    // values gathered by trial and error
    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 2.25f, 0.001f * min / 2.25f));
    contentStream.transform(Matrix.getTranslateInstance(500, 375));

    // we get the shape of an Helvetica bold "?" and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    GeneralPath path = PDType1Font.HELVETICA_BOLD.getPath("question");
    addPath(contentStream, path);
    contentStream.restoreGraphicsState();
    // draw the outer circle counterclockwise to fill area between circle and "?"
    drawCircle2(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.fillAndStroke();
}
 
Example 4
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private void drawParagraph(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 20);

    float min = Math.min(bbox.getWidth(), bbox.getHeight());

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(0.59f); // value from Adobe

    // Adobe first fills a white circle with CA ca 0.6, so do we
    contentStream.saveGraphicsState();
    contentStream.setLineWidth(1);
    PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
    gs.setAlphaSourceFlag(false);
    gs.setStrokingAlphaConstant(0.6f);
    gs.setNonStrokingAlphaConstant(0.6f);
    gs.setBlendMode(BlendMode.NORMAL);
    contentStream.setGraphicsStateParameters(gs);
    contentStream.setNonStrokingColor(1f);
    drawCircle2(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.fill();
    contentStream.restoreGraphicsState();

    contentStream.saveGraphicsState();
    // rescale so that "?" fits into circle and move "?" to circle center
    // values gathered by trial and error
    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 3, 0.001f * min / 3));
    contentStream.transform(Matrix.getTranslateInstance(850, 900));

    // we get the shape of an Helvetica "?" and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    GeneralPath path = PDType1Font.HELVETICA.getPath("paragraph");
    addPath(contentStream, path);
    contentStream.restoreGraphicsState();
    contentStream.fillAndStroke();
    drawCircle(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.stroke();
}
 
Example 5
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private void drawRightArrow(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 20);

    float min = Math.min(bbox.getWidth(), bbox.getHeight());

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(0.59f); // value from Adobe

    // Adobe first fills a white circle with CA ca 0.6, so do we
    contentStream.saveGraphicsState();
    contentStream.setLineWidth(1);
    PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
    gs.setAlphaSourceFlag(false);
    gs.setStrokingAlphaConstant(0.6f);
    gs.setNonStrokingAlphaConstant(0.6f);
    gs.setBlendMode(BlendMode.NORMAL);
    contentStream.setGraphicsStateParameters(gs);
    contentStream.setNonStrokingColor(1f);
    drawCircle2(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.fill();
    contentStream.restoreGraphicsState();

    contentStream.saveGraphicsState();
    // rescale so that the glyph fits into circle and move it to circle center
    // values gathered by trial and error
    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 1.3f, 0.001f * min / 1.3f));
    contentStream.transform(Matrix.getTranslateInstance(200, 300));

    // we get the shape of a Zapf Dingbats right arrow (0x2794) and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    GeneralPath path = PDType1Font.ZAPF_DINGBATS.getPath("a160");
    addPath(contentStream, path);
    contentStream.restoreGraphicsState();
    // surprisingly, this one not counterclockwise.
    drawCircle(contentStream, min / 2, min / 2, min / 2 - 1);
    contentStream.fillAndStroke();
}
 
Example 6
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private void drawComment(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
             throws IOException
{
    adjustRectAndBBox(annotation, 18, 18);

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(200);

    // Adobe first fills a white rectangle with CA ca 0.6, so do we
    contentStream.saveGraphicsState();
    contentStream.setLineWidth(1);
    PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
    gs.setAlphaSourceFlag(false);
    gs.setStrokingAlphaConstant(0.6f);
    gs.setNonStrokingAlphaConstant(0.6f);
    gs.setBlendMode(BlendMode.NORMAL);
    contentStream.setGraphicsStateParameters(gs);
    contentStream.setNonStrokingColor(1f);
    contentStream.addRect(0.3f, 0.3f, 18-0.6f, 18-0.6f);
    contentStream.fill();
    contentStream.restoreGraphicsState();

    contentStream.transform(Matrix.getScaleInstance(0.003f, 0.003f));
    contentStream.transform(Matrix.getTranslateInstance(500, -300));

    // outer shape was gathered from Font Awesome by "printing" comment.svg
    // into a PDF and looking at the content stream
    contentStream.moveTo(2549, 5269);
    contentStream.curveTo(1307, 5269, 300, 4451, 300, 3441);
    contentStream.curveTo(300, 3023, 474, 2640, 764, 2331);
    contentStream.curveTo(633, 1985, 361, 1691, 357, 1688);
    contentStream.curveTo(299, 1626, 283, 1537, 316, 1459);
    contentStream.curveTo(350, 1382, 426, 1332, 510, 1332);
    contentStream.curveTo(1051, 1332, 1477, 1558, 1733, 1739);
    contentStream.curveTo(1987, 1659, 2261, 1613, 2549, 1613);
    contentStream.curveTo(3792, 1613, 4799, 2431, 4799, 3441);
    contentStream.curveTo(4799, 4451, 3792, 5269, 2549, 5269);
    contentStream.closePath();

    // can't use addRect: if we did that, we wouldn't get the donut effect
    contentStream.moveTo(0.3f / 0.003f - 500, 0.3f / 0.003f + 300);
    contentStream.lineTo(0.3f / 0.003f - 500, 0.3f / 0.003f + 300 + 17.4f / 0.003f);
    contentStream.lineTo(0.3f / 0.003f - 500 + 17.4f / 0.003f, 0.3f / 0.003f + 300 + 17.4f / 0.003f);
    contentStream.lineTo(0.3f / 0.003f - 500 + 17.4f / 0.003f, 0.3f / 0.003f + 300);

    contentStream.closeAndFillAndStroke();
}
 
Example 7
Source File: NativePdfBoxVisibleSignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void setAlpha(PDPageContentStream cs, float alpha) throws IOException {
	PDExtendedGraphicsState gs = new PDExtendedGraphicsState();
	gs.setNonStrokingAlphaConstant(alpha / OPAQUE_VALUE);
	cs.setGraphicsStateParameters(gs);
}