org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState. 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: OverlayDocuments.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://issues.apache.org/jira/browse/PDFBOX-4797">
 * Overlayed PDF file do not shows the difference
 * </a>
 * <br/>
 * <a href="https://issues.apache.org/jira/secure/attachment/12996277/10.pdf">
 * 10.pdf
 * </a>
 * <br/>
 * <a href="https://issues.apache.org/jira/secure/attachment/12996276/114.pdf">
 * 114.pdf
 * </a>
 * <p>
 * This test demonstrates how to use the blend mode when overlaying documents
 * for comparison.
 * </p>
 */
@Test
public void testOverlayWithMultiply() throws IOException {
    try (   InputStream file1 = getClass().getResourceAsStream("10.pdf");
            InputStream file2 = getClass().getResourceAsStream("114.pdf");
            PDDocument document1 = Loader.loadPDF(file1);
            PDDocument document2 = Loader.loadPDF(file2);
            Overlay overlayer = new Overlay()) {
        overlayer.setInputPDF(document1);
        overlayer.setAllPagesOverlayPDF(document2);
        try (   PDDocument result = overlayer.overlay(Collections.emptyMap()) ) {
            result.save(new File(RESULT_FOLDER, "10and114.pdf"));

            try (   PDPageContentStream canvas = new PDPageContentStream(result, result.getPage(5), AppendMode.PREPEND, false, false)) {
                PDExtendedGraphicsState extGState = new PDExtendedGraphicsState();
                extGState.setBlendMode(BlendMode.MULTIPLY);
                canvas.setGraphicsStateParameters(extGState);
            }
            result.save(new File(RESULT_FOLDER, "10and114multiply.pdf"));
        }
    }
}
 
Example #2
Source File: SetGraphicsStateParameters.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    if (arguments.isEmpty())
    {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName))
    {
        return;
    }
    
    // set parameters from graphics state parameter dictionary
    COSName graphicsName = (COSName) base0;
    PDExtendedGraphicsState gs = context.getResources().getExtGState(graphicsName);
    if (gs == null)
    {
        LOG.error("name for 'gs' operator not found in resources: /" + graphicsName.getName());
        return;
    }
    gs.copyIntoGraphicsState( context.getGraphicsState() );
}
 
Example #3
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDExtendedGraphicsState getExtGState(COSObject indirect)
{
    SoftReference<PDExtendedGraphicsState> extGState = extGStates.get(indirect);
    if (extGState != null)
    {
        return extGState.get();
    }
    return null;
}
 
Example #4
Source File: PDFRenderer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private boolean hasBlendMode(PDPage page)
{
    // check the current resources for blend modes
    PDResources resources = page.getResources();
    if (resources == null)
    {
        return false;
    }
    for (COSName name : resources.getExtGStateNames())
    {
        PDExtendedGraphicsState extGState = resources.getExtGState(name);
        if (extGState == null)
        {
            // can happen if key exists but no value 
            // see PDFBOX-3950-23EGDHXSBBYQLKYOKGZUOVYVNE675PRD.pdf
            continue;
        }
        BlendMode blendMode = extGState.getBlendMode();
        if (blendMode != BlendMode.NORMAL)
        {
            return true;
        }
    }
    return false;
}
 
Example #5
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 #6
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 #7
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 #8
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);
}
 
Example #9
Source File: UseSoftMask.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/60198179/creating-a-transparency-group-or-setting-graphics-state-soft-mask-with-pdfbox">
 * Creating a transparency group or setting graphics state soft mask with PDFBox
 * </a>
 * <br/>
 * <a href="https://i.stack.imgur.com/rh9kL.png">
 * rh9kL.png
 * </a> as "Nicolas_image.png"
 * <br/>
 * <a href="https://i.stack.imgur.com/2UoKr.png">
 * 2UoKr.png
 * </a> as "Nicolas_mask.png"
 * <p>
 * This test demonstrates how one can apply transparency group
 * soft masks in extended graphics state parameters.
 * </p>
 */
@Test
public void testSoftMaskedImageAndRectangle() throws IOException {
    try (   PDDocument document = new PDDocument()  ) {
        final PDImageXObject image;
        try (   InputStream imageResource = getClass().getResourceAsStream("Nicolas_image.png")) {
            image = PDImageXObject.createFromByteArray(document, IOUtils.toByteArray(imageResource), "image");
        }

        final PDImageXObject mask;
        try (   InputStream imageResource = getClass().getResourceAsStream("Nicolas_mask.png")) {
            mask = PDImageXObject.createFromByteArray(document, IOUtils.toByteArray(imageResource), "mask");
        }

        PDTransparencyGroupAttributes transparencyGroupAttributes = new PDTransparencyGroupAttributes();
        transparencyGroupAttributes.getCOSObject().setItem(COSName.CS, COSName.DEVICEGRAY);

        PDTransparencyGroup transparencyGroup = new PDTransparencyGroup(document);
        transparencyGroup.setBBox(PDRectangle.A4);
        transparencyGroup.setResources(new PDResources());
        transparencyGroup.getCOSObject().setItem(COSName.GROUP, transparencyGroupAttributes);
        try (   PDFormContentStream canvas = new PDFormContentStream(transparencyGroup)   ) {
            canvas.drawImage(mask, new Matrix(400, 0, 0, 400, 100, 100));
        }

        COSDictionary softMaskDictionary = new COSDictionary();
        softMaskDictionary.setItem(COSName.S, COSName.LUMINOSITY);
        softMaskDictionary.setItem(COSName.G, transparencyGroup);

        PDExtendedGraphicsState extendedGraphicsState = new PDExtendedGraphicsState();
        extendedGraphicsState.getCOSObject().setItem(COSName.SMASK, softMaskDictionary);

        PDPage page = new PDPage(PDRectangle.A4);
        document.addPage(page);
        try (   PDPageContentStream canvas = new PDPageContentStream(document, page)   ) {
            canvas.saveGraphicsState();
            canvas.setGraphicsStateParameters(extendedGraphicsState);
            canvas.setNonStrokingColor(Color.BLACK);
            canvas.addRect(100, 100, 400, 400);
            canvas.fill();
            canvas.drawImage(image, new Matrix(400, 0, 0, 300, 100, 150));
            canvas.restoreGraphicsState();
        }

        document.save(new File(RESULT_FOLDER, "SoftMaskedImageAndRectangle.pdf"));
    }
}
 
Example #10
Source File: DummyResourceCache.java    From pdfcompare with Apache License 2.0 4 votes vote down vote up
@Override
public void put(final COSObject indirect, final PDExtendedGraphicsState extGState) {}
 
Example #11
Source File: DummyResourceCache.java    From pdfcompare with Apache License 2.0 4 votes vote down vote up
@Override
public PDExtendedGraphicsState getExtGState(final COSObject indirect) {
    return null;
}
 
Example #12
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void put(COSObject indirect, PDExtendedGraphicsState extGState)
{
    extGStates.put(indirect, new SoftReference<PDExtendedGraphicsState>(extGState));
}
 
Example #13
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 #14
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 #15
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 #16
Source File: PDShadingPattern.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * This will set the external graphics state for this pattern.
 * @param extendedGraphicsState The new extended graphics state for this pattern.
 */
public void setExtendedGraphicsState(PDExtendedGraphicsState extendedGraphicsState)
{
    this.extendedGraphicsState = extendedGraphicsState;
    getCOSObject().setItem(COSName.EXT_G_STATE, extendedGraphicsState);
}
 
Example #17
Source File: ResourceCache.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Puts the given indirect extended graphics state resource in the cache.
 * 
 * @param indirect the indirect object of the resource.
 * @param extGState the extended graphics state resource.
 */
void put(COSObject indirect, PDExtendedGraphicsState extGState);
 
Example #18
Source File: ResourceCache.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the extended graphics state resource for the given indirect object, if it is in the cache.
 * 
 * @param indirect the indirect object
 * 
 * @return the extended graphics resource of the given indirect object.
 */
PDExtendedGraphicsState getExtGState(COSObject indirect);
 
Example #19
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Set an extended graphics state.
 * 
 * @param state The extended graphics state.
 * @throws IOException If the content stream could not be written.
 */
public void setGraphicsStateParameters(PDExtendedGraphicsState state) throws IOException
{
    writeOperand(resources.add(state));
    writeOperator(OperatorName.SET_GRAPHICS_STATE_PARAMS);
}
 
Example #20
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Set an extended graphics state.
 * 
 * @param state The extended graphics state.
 * @throws IOException If the content stream could not be written.
 */
public void setGraphicsStateParameters(PDExtendedGraphicsState state) throws IOException
{
    writeOperand(resources.add(state));
    writeOperator(OperatorName.SET_GRAPHICS_STATE_PARAMS);
}
 
Example #21
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Sets the extended graphics state resource with the given name.
 *
 * @param name the name of the resource
 * @param extGState the extended graphics state to be added
 */
public void put(COSName name, PDExtendedGraphicsState extGState)
{
    put(COSName.EXT_G_STATE, name, extGState);
}
 
Example #22
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Adds the given extended graphics state to the resources of the current page and returns the
 * name for the new resources. Returns the existing resource name if the given item already exists.
 *
 * @param extGState the extended graphics state to add
 * @return the name of the resource in the resources dictionary
 */
public COSName add(PDExtendedGraphicsState extGState)
{
    return add(COSName.EXT_G_STATE, "gs", extGState);
}