/** * Set the stroking color in the DeviceGray color space. Range is 0..255. * * @param g The gray value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameter is invalid. * @deprecated Use {@link #setStrokingColor(float)} instead. */ @Deprecated public void setStrokingColor(int g) throws IOException { if (isOutside255Interval(g)) { throw new IllegalArgumentException("Parameter must be within 0..255, but is " + g); } setStrokingColor(g / 255f); }
PDDocument doc = new PDDocument(); PDPage pdpage = new PDPage(); doc.addPage(pdpage); PDPageContentStream stream = new PDPageContentStream(doc, pdpage, false, false); // move stuff a bit up and right stream.concatenate2CTM(AffineTransform.getTranslateInstance(100, 400)); stream.setStrokingColorSpace(PDDeviceRGB.INSTANCE); stream.setStrokingColor(Color.red); stream.setLineWidth(10f); stream.moveTo(1, 1); stream.addBezier32(111, 075, 200, 200); stream.addBezier32(352, 124, 80, 80); stream.addBezier32(432, 333, 300, 333); stream.stroke(); stream.close(); doc.save(filename); doc.close();
/** * Set the stroking color in the DeviceCMYK color space. Range is 0..255. * * @param c The cyan value. * @param m The magenta value. * @param y The yellow value. * @param k The black value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameters are invalid. * @deprecated Use {@link #setStrokingColor(float, float, float, float)} instead. */ @Deprecated public void setStrokingColor(int c, int m, int y, int k) throws IOException { if (isOutside255Interval(c) || isOutside255Interval(m) || isOutside255Interval(y) || isOutside255Interval(k)) { throw new IllegalArgumentException("Parameters must be within 0..255, but are " + String.format("(%d,%d,%d,%d)", c, m, y, k)); } setStrokingColor(c / 255f, m / 255f, y / 255f, k / 255f); }
/** * Set the stroking color in the DeviceGray color space. Range is 0..1. * * @param g The gray value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameter is invalid. * @deprecated use {@link #setStrokingColor(float) setStrokingColor(float)} */ @Deprecated public void setStrokingColor(double g) throws IOException { setStrokingColor((float) g); }
public void setStrokingColor(float c, float m, float y, float k) { try { cs.setStrokingColor(c, m, y, k); } catch (IOException e) { logAndThrow("setStrokingColor(CMYK)", e); } }
public void setStrokingColor(int r, int g, int b) { try { cs.setStrokingColor(r, g, b); } catch (IOException e) { logAndThrow("setStrokingColor", e); } }
public void setStrokingColor(int r, int g, int b) { try { cs.setStrokingColor(r, g, b); } catch (IOException e) { logAndThrow("setStrokingColor", e); } }
public void setStrokingColor(float c, float m, float y, float k) { try { cs.setStrokingColor(c, m, y, k); } catch (IOException e) { logAndThrow("setStrokingColor(CMYK)", e); } }
/** * Set the stroking color using an AWT color. Conversion uses the default sRGB color space. * * @param color The color to set. * @throws IOException If an IO error occurs while writing to the stream. */ public void setStrokingColor(Color color) throws IOException { float[] components = new float[] { color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f }; PDColor pdColor = new PDColor(components, PDDeviceRGB.INSTANCE); setStrokingColor(pdColor); }
/** * Set the stroking color in the DeviceGray color space. Range is 0..255. * * @param g The gray value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameter is invalid. * @deprecated Use {@link #setStrokingColor(float)} instead. */ @Deprecated public void setStrokingColor(int g) throws IOException { if (isOutside255Interval(g)) { throw new IllegalArgumentException("Parameter must be within 0..255, but is " + g); } setStrokingColor(g / 255f); }
/** * Set the stroking color in the DeviceGray color space. Range is 0..255. * * @param g The gray value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameter is invalid. * @deprecated Use {@link #setStrokingColor(float)} instead. */ @Deprecated public void setStrokingColor(int g) throws IOException { if (isOutside255Interval(g)) { throw new IllegalArgumentException("Parameter must be within 0..255, but is " + g); } setStrokingColor(g / 255f); }
/** * Set the stroking color in the DeviceCMYK color space. Range is 0..255. * * @param c The cyan value. * @param m The magenta value. * @param y The yellow value. * @param k The black value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameters are invalid. * @deprecated Use {@link #setStrokingColor(float, float, float, float)} instead. */ @Deprecated public void setStrokingColor(int c, int m, int y, int k) throws IOException { if (isOutside255Interval(c) || isOutside255Interval(m) || isOutside255Interval(y) || isOutside255Interval(k)) { throw new IllegalArgumentException("Parameters must be within 0..255, but are " + String.format("(%d,%d,%d,%d)", c, m, y, k)); } setStrokingColor(c / 255f, m / 255f, y / 255f, k / 255f); }
/** * Set the stroking color in the DeviceCMYK color space. Range is 0..255. * * @param c The cyan value. * @param m The magenta value. * @param y The yellow value. * @param k The black value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameters are invalid. * @deprecated Use {@link #setStrokingColor(float, float, float, float)} instead. */ @Deprecated public void setStrokingColor(int c, int m, int y, int k) throws IOException { if (isOutside255Interval(c) || isOutside255Interval(m) || isOutside255Interval(y) || isOutside255Interval(k)) { throw new IllegalArgumentException("Parameters must be within 0..255, but are " + String.format("(%d,%d,%d,%d)", c, m, y, k)); } setStrokingColor(c / 255f, m / 255f, y / 255f, k / 255f); }
void drawRect(PDPageContentStream content, Color color, Rectangle rect, boolean fill) { content.addRect(rect.x, rect.y, rect.width, rect.height); if (fill) { content.setNonStrokingColor(color); content.fill(); } else { content.setStrokingColor(color); content.stroke(); } }
@SuppressWarnings("WeakerAccess") protected void applyAsStrokingColor(Color color, PaintApplierState state) throws IOException { PDPageContentStream contentStream = state.contentStream; IPdfBoxGraphics2DColorMapper colorMapper = state.colorMapper; contentStream.setStrokingColor(colorMapper.mapColor(contentStream, color)); contentStream.setNonStrokingColor(colorMapper.mapColor(contentStream, color)); int alpha = color.getAlpha(); if (alpha < 255) { /* * This is semitransparent */ state.ensureExtendedState(); Float strokingAlphaConstant = state.pdExtendedGraphicsState.getStrokingAlphaConstant(); if (strokingAlphaConstant == null) strokingAlphaConstant = 1f; state.pdExtendedGraphicsState.setStrokingAlphaConstant(strokingAlphaConstant * (alpha / 255f)); Float nonStrokingAlphaConstant = state.pdExtendedGraphicsState.getNonStrokingAlphaConstant(); if (nonStrokingAlphaConstant == null) nonStrokingAlphaConstant = 1f; state.pdExtendedGraphicsState.setNonStrokingAlphaConstant(nonStrokingAlphaConstant * (alpha / 255f)); } }
PDDocument document = PDDocument.load("0006-sun1-4.pdf"); List<PDPage> allPages = document.getDocumentCatalog().getAllPages(); PDPage firstPage = allPages.get(0); PDRectangle pageSize = firstPage.findMediaBox(); PDPageContentStream contentStream = new PDPageContentStream(document, firstPage, true, true); contentStream.setStrokingColor(Color.red); contentStream.beginText(); contentStream.moveTextPositionByAmount(300/.24f, (pageSize.getUpperRightY() - 420 - 10)/.24f); contentStream.setFont(PDType1Font.HELVETICA_BOLD, 10/.24f); contentStream.drawString("MIDDLE"); contentStream.endText(); contentStream.close(); document.save("0006-sun1-4-scaledAdd.pdf"); document.close();
PDDocument document = PDDocument.load("0006-sun1-4.pdf"); List<PDPage> allPages = document.getDocumentCatalog().getAllPages(); PDPage firstPage = allPages.get(0); PDRectangle pageSize = firstPage.findMediaBox(); PDStream contents = firstPage.getContents(); PDFStreamParser parser = new PDFStreamParser(contents.getStream()); parser.parse(); List<Object> tokens = parser.getTokens(); tokens.add(0, PDFOperator.getOperator("q")); tokens.add(PDFOperator.getOperator("Q")); PDStream updatedStream = new PDStream(document); OutputStream out = updatedStream.createOutputStream(); ContentStreamWriter tokenWriter = new ContentStreamWriter(out); tokenWriter.writeTokens(tokens); firstPage.setContents(updatedStream); PDPageContentStream contentStream = new PDPageContentStream(document, firstPage, true, true); contentStream.setStrokingColor(Color.red); contentStream.beginText(); contentStream.moveTextPositionByAmount(300, pageSize.getUpperRightY() - 420 - 10); contentStream.setFont(PDType1Font.HELVETICA_BOLD, 10); contentStream.drawString("MIDDLE"); contentStream.endText(); contentStream.close(); document.save("0006-sun1-4-restoredAdd.pdf"); document.close();
private void printRect(final PDPageContentStream contentStream, final PDRectangle rect) throws IOException { contentStream.setStrokingColor(Color.YELLOW); contentStream.drawLine(rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getLowerLeftX(), rect.getUpperRightY()); // left contentStream.drawLine(rect.getLowerLeftX(), rect.getUpperRightY(), rect.getUpperRightX(), rect.getUpperRightY()); // top contentStream.drawLine(rect.getUpperRightX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getUpperRightY()); // right contentStream.drawLine(rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getLowerLeftY()); // bottom contentStream.setStrokingColor(Color.BLACK); }
@Override public void commit(PDPageContentStream stream) throws IOException { stream.setStrokingColor(style.color()); stream.setLineWidth(toFloat(style.width())); stream.moveTo(toFloat(x1), toFloat(y1)); stream.lineTo(toFloat(x2), toFloat(y2)); stream.stroke(); } }
/** * <p> * Provides ability to set different line styles (line width, dotted line, * dashed line) * </p> * * @param stream * The {@link PDPageContentStream} where drawing will be applied. * @param line * The {@link LineStyle} that would be applied * @throws IOException If the content stream could not be written or the line color cannot be retrieved. */ public static void setLineStyles(final PDPageContentStream stream, final LineStyle line) throws IOException { stream.setNonStrokingColor(line.getColor()); stream.setStrokingColor(line.getColor()); stream.setLineWidth(line.getWidth()); stream.setLineCapStyle(0); if (line.getDashArray() != null) { stream.setLineDashPattern(line.getDashArray(), line.getDashPhase()); } else { stream.setLineDashPattern(new float[] {}, 0.0f); } } }