List of usage examples for org.apache.pdfbox.pdmodel.graphics.state PDExtendedGraphicsState setNonStrokingAlphaConstant
public void setNonStrokingAlphaConstant(Float alpha)
From source file:helper.pdfpreprocessing.pdf.TextHighlight.java
License:Apache License
public void highlight(final Pattern searchText, final Pattern markingPattern, Color color, int pageNr, boolean withId, String comment) { if (textCache == null || document == null) { throw new IllegalArgumentException("TextCache was not initialized"); }/*w w w . j a va 2s . c om*/ try { boolean found = false; final PDPage page = document.getPages().get(pageNr - 1); PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true); PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); graphicsState.setNonStrokingAlphaConstant(0.5f); contentStream.setGraphicsStateParameters(graphicsState); for (Match searchMatch : textCache.match(pageNr, searchText)) { if (textCache.match(searchMatch.positions, markingPattern).size() > 0) { for (Match markingMatch : textCache.match(searchMatch.positions, markingPattern)) { if (markupMatch(color, contentStream, markingMatch, 10, withId, page, comment, false)) { found = true; } } } else { System.out .println("Cannot highlight: " + markingPattern.pattern() + " on page " + (pageNr - 1)); } if (found) { break; } } contentStream.close(); } catch (Exception e) { e.printStackTrace(); } catch (Error e1) { e1.printStackTrace(); throw e1; } }
From source file:helper.pdfpreprocessing.pdf.TextHighlight.java
License:Apache License
public void highlight(int startIndex, int stopIndex, Color color, int pageNr, int boxHeight, boolean hasLineOffset, boolean withId, String comment, boolean commentOnly) { if (textCache == null || document == null) { throw new IllegalArgumentException("TextCache was not initialized"); }//from w w w .j a va2 s . com try { final PDPage page = document.getPages().get(pageNr - 1); PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true); PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); graphicsState.setNonStrokingAlphaConstant(0.5f); contentStream.setGraphicsStateParameters(graphicsState); List<TextPosition> pos = textCache.getTextPositions(pageNr); int numberOfLines = 0; if (hasLineOffset) { numberOfLines = textCache.getText(pageNr).substring(0, stopIndex).split("\\n").length - 1; } pos = pos.subList(Math.min(numberOfLines + startIndex, pos.size()), Math.min(numberOfLines + stopIndex, pos.size())); Match m = new Match(pageNr + "-" + startIndex, pos); markupMatch(color, contentStream, m, boxHeight, withId, page, comment, commentOnly); contentStream.close(); } catch (Exception e) { e.printStackTrace(); } catch (Error e1) { e1.printStackTrace(); throw e1; } }
From source file:info.informationsea.venn.graphics.VennDrawPDF.java
License:Open Source License
public static <T> void draw(VennFigure<T> vennFigure, PDDocument doc, PDPage page, PDFont font, PDRectangle rect) throws IOException { PDPageContentStream contents = new PDPageContentStream(doc, page); Rectangle2D drawRect = vennFigure.drawRect(str -> stringBoundingBox(font, str, FONT_SIZE)); PointConverter.JoinedConverter pointConverter = new PointConverter.JoinedConverter(); pointConverter//from ww w. j a v a2 s .c om .addLast(new PointConverter.Translate(-drawRect.getMinX() + MARGIN, -drawRect.getMinY() + MARGIN)); pointConverter.addLast(new PointConverter.Scale(1, -1)); pointConverter.addLast(new PointConverter.Translate(0, rect.getHeight())); // fill first for (VennFigure.Shape<T> shape : vennFigure.getShapes()) { if (shape instanceof VennFigure.Oval) { VennFigure.Oval<T> oval = (VennFigure.Oval<T>) shape; Color fillColor = VennDrawGraphics2D.decodeColor(oval.getColor()); if (fillColor.getAlpha() == 0) continue; //COSName graphicsStateName = page.getResources().add(graphicsState); List<VennFigure.Point> polygon = oval.toPolygon(); VennFigure.Point converted = pointConverter.convert(polygon.get(0)); contents.moveTo((float) converted.getX(), (float) converted.getY()); polygon.add(polygon.remove(0)); // move to last for (VennFigure.Point p : polygon) { converted = pointConverter.convert(p); contents.lineTo((float) converted.getX(), (float) converted.getY()); } if (fillColor.getAlpha() != 255) { PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); graphicsState.setNonStrokingAlphaConstant(fillColor.getAlpha() / 255.f); contents.saveGraphicsState(); contents.setGraphicsStateParameters(graphicsState); } contents.setNonStrokingColor(fillColor); contents.fill(); if (fillColor.getAlpha() != 255) { contents.restoreGraphicsState(); } } } for (VennFigure.Shape<T> shape : vennFigure.getShapes()) { if (shape instanceof VennFigure.Oval) { VennFigure.Oval<T> oval = (VennFigure.Oval<T>) shape; List<VennFigure.Point> polygon = oval.toPolygon(); VennFigure.Point converted = pointConverter.convert(polygon.get(0)); contents.moveTo((float) converted.getX(), (float) converted.getY()); polygon.add(polygon.remove(0)); // move to last for (VennFigure.Point p : polygon) { converted = pointConverter.convert(p); contents.lineTo((float) converted.getX(), (float) converted.getY()); } contents.setNonStrokingColor(Color.BLACK); contents.stroke(); } else if (shape instanceof VennFigure.Text) { VennFigure.Text<T> text = (VennFigure.Text<T>) shape; Rectangle2D boundingBox = stringBoundingBox(font, text.getText(), FONT_SIZE); VennFigure.Point converted = pointConverter.convert(text.getCenter()); float xOffset; switch (text.getJust()) { case CENTER: xOffset = (float) (-boundingBox.getWidth() / 2); break; case RIGHT: xOffset = (float) (-boundingBox.getWidth()); break; default: xOffset = 0; break; } contents.beginText(); contents.setNonStrokingColor(Color.BLACK); contents.setFont(font, FONT_SIZE); contents.newLineAtOffset((float) (converted.getX() + xOffset), (float) (converted.getY() - boundingBox.getHeight() / 2)); contents.showText(text.getText()); contents.endText(); } } contents.saveGraphicsState(); contents.close(); }