Java Code Examples for org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState#setBlendMode()
The following examples show how to use
org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState#setBlendMode() .
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 |
/** * <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: PDTextAppearanceHandler.java From gcs with Mozilla Public License 2.0 | 5 votes |
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 |
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 |
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 |
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 |
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(); }