org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB Java Examples
The following examples show how to use
org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB.
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: PDAppearanceCharacteristicsDictionary.java From gcs with Mozilla Public License 2.0 | 6 votes |
private PDColor getColor(COSName itemName) { COSBase c = this.getCOSObject().getItem(itemName); if (c instanceof COSArray) { PDColorSpace colorSpace; switch (((COSArray) c).size()) { case 1: colorSpace = PDDeviceGray.INSTANCE; break; case 3: colorSpace = PDDeviceRGB.INSTANCE; break; case 4: colorSpace = PDDeviceCMYK.INSTANCE; break; default: return null; } return new PDColor((COSArray) c, colorSpace); } return null; }
Example #2
Source File: PDAnnotation.java From gcs with Mozilla Public License 2.0 | 6 votes |
protected PDColor getColor(COSName itemName) { COSBase c = this.getCOSObject().getItem(itemName); if (c instanceof COSArray) { PDColorSpace colorSpace = null; switch (((COSArray) c).size()) { case 1: colorSpace = PDDeviceGray.INSTANCE; break; case 3: colorSpace = PDDeviceRGB.INSTANCE; break; case 4: colorSpace = PDDeviceCMYK.INSTANCE; break; default: break; } return new PDColor((COSArray) c, colorSpace); } return null; }
Example #3
Source File: PDDefaultAppearanceString.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * Process the font color operator. * * This is assumed to be an RGB color. * * @param operands the color components * @throws IOException in case of the color components not matching */ private void processSetFontColor(List<COSBase> operands) throws IOException { PDColorSpace colorSpace; switch (operands.size()) { case 1: colorSpace = PDDeviceGray.INSTANCE; break; case 3: colorSpace = PDDeviceRGB.INSTANCE; break; case 4: colorSpace = PDDeviceCMYK.INSTANCE; break; default: throw new IOException("Missing operands for set non stroking color operator " + Arrays.toString(operands.toArray())); } COSArray array = new COSArray(); array.addAll(operands); setFontColor(new PDColor(array, colorSpace)); }
Example #4
Source File: PDPageContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
private COSName getName(PDColorSpace colorSpace) throws IOException { if (colorSpace instanceof PDDeviceGray || colorSpace instanceof PDDeviceRGB || colorSpace instanceof PDDeviceCMYK) { return COSName.getPDFName(colorSpace.getName()); } else { return resources.add(colorSpace); } }
Example #5
Source File: PdfTextStyleTest.java From cat-boot with Apache License 2.0 | 5 votes |
@Test public void fromConstructor() { PdfTextStyle config = new PdfTextStyle("10.5,Times-Roman,#000000"); PDColor black = new PDColor(new float[] {0.0f, 0.0f, 0.0f}, PDDeviceRGB.INSTANCE); Assert.assertEquals(10.5, config.getFontSize(), delta); Assert.assertEquals("Times-Roman", config.getFont().getBasename()); Assert.assertEquals(black.getColorSpace(), config.getColor().getColorSpace()); }
Example #6
Source File: PdfTextStyle.java From cat-boot with Apache License 2.0 | 5 votes |
/** * This constructor is used by spring when creating a font from properties. * * @param config e.g. 10,Times-Roman,#000000 */ public PdfTextStyle(String config) { Assert.hasText(config); String[] split = config.split(","); Assert.isTrue(split.length == 3, "config must look like: 10,Times-Roman,#000000"); fontSize = Float.parseFloat(split[0]); font = PdfFont.getFont(split[1]); Color tempColor = new Color(Integer.valueOf(split[2].substring(1), 16)); float[] components = {tempColor.getRed(), tempColor.getGreen(), tempColor.getBlue()}; color = new PDColor(components, PDDeviceRGB.INSTANCE); }
Example #7
Source File: PDPageContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Set the non-stroking color in the DeviceRGB color space. Range is 0..255. * * @param r The red value. * @param g The green value. * @param b The blue value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameters are invalid. */ public void setNonStrokingColor(int r, int g, int b) throws IOException { if (isOutside255Interval(r) || isOutside255Interval(g) || isOutside255Interval(b)) { throw new IllegalArgumentException("Parameters must be within 0..255, but are " + String.format("(%d,%d,%d)", r, g, b)); } writeOperand(r / 255f); writeOperand(g / 255f); writeOperand(b / 255f); writeOperator(OperatorName.NON_STROKING_RGB); setNonStrokingColorSpaceStack(PDDeviceRGB.INSTANCE); }
Example #8
Source File: PDPageContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Set the non-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 setNonStrokingColor(Color color) throws IOException { float[] components = new float[] { color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f }; PDColor pdColor = new PDColor(components, PDDeviceRGB.INSTANCE); setNonStrokingColor(pdColor); }
Example #9
Source File: PDPageContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Set the stroking color in the DeviceRGB color space. Range is 0..255. * * @param r The red value * @param g The green value. * @param b The blue value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameters are invalid. */ public void setStrokingColor(int r, int g, int b) throws IOException { if (isOutside255Interval(r) || isOutside255Interval(g) || isOutside255Interval(b)) { throw new IllegalArgumentException("Parameters must be within 0..255, but are " + String.format("(%d,%d,%d)", r, g, b)); } writeOperand(r / 255f); writeOperand(g / 255f); writeOperand(b / 255f); writeOperator(OperatorName.STROKING_COLOR_RGB); setStrokingColorSpaceStack(PDDeviceRGB.INSTANCE); }
Example #10
Source File: PDPageContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * 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); }
Example #11
Source File: PDAbstractContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Set the non-stroking color in the DeviceRGB color space. Range is 0..255. * * @param r The red value. * @param g The green value. * @param b The blue value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameters are invalid. */ public void setNonStrokingColor(int r, int g, int b) throws IOException { if (isOutside255Interval(r) || isOutside255Interval(g) || isOutside255Interval(b)) { throw new IllegalArgumentException("Parameters must be within 0..255, but are " + String.format("(%d,%d,%d)", r, g, b)); } writeOperand(r / 255f); writeOperand(g / 255f); writeOperand(b / 255f); writeOperator(OperatorName.NON_STROKING_RGB); setNonStrokingColorSpaceStack(PDDeviceRGB.INSTANCE); }
Example #12
Source File: PDAbstractContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Set the non-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 setNonStrokingColor(Color color) throws IOException { float[] components = new float[] { color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f }; PDColor pdColor = new PDColor(components, PDDeviceRGB.INSTANCE); setNonStrokingColor(pdColor); }
Example #13
Source File: PDAbstractContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Set the stroking color in the DeviceRGB color space. Range is 0..255. * * @param r The red value * @param g The green value. * @param b The blue value. * @throws IOException If an IO error occurs while writing to the stream. * @throws IllegalArgumentException If the parameters are invalid. */ public void setStrokingColor(int r, int g, int b) throws IOException { if (isOutside255Interval(r) || isOutside255Interval(g) || isOutside255Interval(b)) { throw new IllegalArgumentException("Parameters must be within 0..255, but are " + String.format("(%d,%d,%d)", r, g, b)); } writeOperand(r / 255f); writeOperand(g / 255f); writeOperand(b / 255f); writeOperator(OperatorName.STROKING_COLOR_RGB); setStrokingColorSpaceStack(PDDeviceRGB.INSTANCE); }
Example #14
Source File: PDAbstractContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * 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); }
Example #15
Source File: PDAbstractContentStream.java From gcs with Mozilla Public License 2.0 | 5 votes |
protected COSName getName(PDColorSpace colorSpace) { if (colorSpace instanceof PDDeviceGray || colorSpace instanceof PDDeviceRGB || colorSpace instanceof PDDeviceCMYK) { return COSName.getPDFName(colorSpace.getName()); } else { return resources.add(colorSpace); } }
Example #16
Source File: JPEGFactory.java From gcs with Mozilla Public License 2.0 | 5 votes |
private static PDColorSpace getColorSpaceFromAWT(BufferedImage awtImage) { if (awtImage.getColorModel().getNumComponents() == 1) { // 256 color (gray) JPEG return PDDeviceGray.INSTANCE; } ColorSpace awtColorSpace = awtImage.getColorModel().getColorSpace(); if (awtColorSpace instanceof ICC_ColorSpace && !awtColorSpace.isCS_sRGB()) { throw new UnsupportedOperationException("ICC color spaces not implemented"); } switch (awtColorSpace.getType()) { case ColorSpace.TYPE_RGB: return PDDeviceRGB.INSTANCE; case ColorSpace.TYPE_GRAY: return PDDeviceGray.INSTANCE; case ColorSpace.TYPE_CMYK: return PDDeviceCMYK.INSTANCE; default: throw new UnsupportedOperationException("color space not implemented: " + awtColorSpace.getType()); } }
Example #17
Source File: PdfTextStyleTest.java From cat-boot with Apache License 2.0 | 5 votes |
@Test public void testFloatSize() { float fontSize = 10.5f; PDColor red = new PDColor(new float[] {1.0f, 0.0f, 0.0f}, PDDeviceRGB.INSTANCE); PdfTextStyle config = new PdfTextStyle(fontSize, PdfFont.TIMES_ROMAN, red, "regular"); Assert.assertEquals(fontSize, config.getFontSize(), delta); }
Example #18
Source File: PdfTextStyleTest.java From cat-boot with Apache License 2.0 | 5 votes |
@Test public void testTextWidth() { float fontSize = 0.5f; PDColor black = new PDColor(new float[] {0.0f, 0.0f, 0.0f}, PDDeviceRGB.INSTANCE); PdfTextStyle config = new PdfTextStyle(fontSize, PdfFont.COURIER, black, "bold"); Float textWidth = PdfBoxHelper.getTextWidth(config.getCurrentFontStyle(), config.getFontSize(), "Some text"); Assert.assertTrue(textWidth > 0); }
Example #19
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 4 votes |
private static PDColor toPDColor(final Color color) { float[] components = new float[] { color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f }; return new PDColor(components, PDDeviceRGB.INSTANCE); }
Example #20
Source File: LosslessFactory.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * Creates a new lossless encoded image XObject from a BufferedImage. * <p> * <u>New for advanced users from 2.0.12 on:</u><br> * If you created your image with a non standard ICC colorspace, it will be * preserved. (If you load images in java using ImageIO then no need to read * this segment) However a new colorspace will be created for each image. So * if you create a PDF with several such images, consider replacing the * colorspace with a common object to save space. This is done with * {@link PDImageXObject#getColorSpace()} and * {@link PDImageXObject#setColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace) PDImageXObject.setColorSpace()} * * @param document the document where the image will be created * @param image the BufferedImage to embed * @return a new image XObject * @throws IOException if something goes wrong */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException { if ((image.getType() == BufferedImage.TYPE_BYTE_GRAY && image.getColorModel().getPixelSize() <= 8) || (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1)) { return createFromGrayImage(image, document); } else { // We try to encode the image with predictor if (usePredictorEncoder) { PDImageXObject pdImageXObject = new PredictorEncoder(document, image).encode(); if (pdImageXObject != null) { if (pdImageXObject.getColorSpace() == PDDeviceRGB.INSTANCE && pdImageXObject.getBitsPerComponent() < 16 && image.getWidth() * image.getHeight() <= 50 * 50) { // also create classic compressed image, compare sizes PDImageXObject pdImageXObjectClassic = createFromRGBImage(image, document); if (pdImageXObjectClassic.getCOSObject().getLength() < pdImageXObject.getCOSObject().getLength()) { pdImageXObject.getCOSObject().close(); return pdImageXObjectClassic; } else { pdImageXObjectClassic.getCOSObject().close(); } } return pdImageXObject; } } // Fallback: We export the image as 8-bit sRGB and might loose color information return createFromRGBImage(image, document); } }
Example #21
Source File: JPEGFactory.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * Creates a new JPEG Image XObject from a byte array containing JPEG data. * * @param document the document where the image will be created * @param byteArray bytes of JPEG image * @return a new Image XObject * * @throws IOException if the input stream cannot be read */ public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray) throws IOException { // copy stream ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray); // read image Raster raster = readJPEGRaster(byteStream); byteStream.reset(); PDColorSpace colorSpace; switch (raster.getNumDataElements()) { case 1: colorSpace = PDDeviceGray.INSTANCE; break; case 3: colorSpace = PDDeviceRGB.INSTANCE; break; case 4: colorSpace = PDDeviceCMYK.INSTANCE; break; default: throw new UnsupportedOperationException("number of data elements not supported: " + raster.getNumDataElements()); } // create PDImageXObject from stream PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.DCT_DECODE, raster.getWidth(), raster.getHeight(), 8, colorSpace); if (colorSpace instanceof PDDeviceCMYK) { COSArray decode = new COSArray(); decode.add(COSInteger.ONE); decode.add(COSInteger.ZERO); decode.add(COSInteger.ONE); decode.add(COSInteger.ZERO); decode.add(COSInteger.ONE); decode.add(COSInteger.ZERO); decode.add(COSInteger.ONE); decode.add(COSInteger.ZERO); pdImage.setDecode(decode); } return pdImage; }
Example #22
Source File: LosslessFactory.java From gcs with Mozilla Public License 2.0 | 4 votes |
private PDImageXObject preparePredictorPDImage(ByteArrayOutputStream stream, int bitsPerComponent) throws IOException { int h = image.getHeight(); int w = image.getWidth(); ColorSpace srcCspace = image.getColorModel().getColorSpace(); int srcCspaceType = srcCspace.getType(); PDColorSpace pdColorSpace = srcCspaceType == ColorSpace.TYPE_CMYK ? PDDeviceCMYK.INSTANCE : (srcCspaceType == ColorSpace.TYPE_GRAY ? PDDeviceGray.INSTANCE : PDDeviceRGB.INSTANCE); // Encode the image profile if the image has one if (srcCspace instanceof ICC_ColorSpace) { ICC_Profile profile = ((ICC_ColorSpace) srcCspace).getProfile(); // We only encode a color profile if it is not sRGB if (profile != ICC_Profile.getInstance(ColorSpace.CS_sRGB)) { PDICCBased pdProfile = new PDICCBased(document); OutputStream outputStream = pdProfile.getPDStream() .createOutputStream(COSName.FLATE_DECODE); outputStream.write(profile.getData()); outputStream.close(); pdProfile.getPDStream().getCOSObject().setInt(COSName.N, srcCspace.getNumComponents()); pdProfile.getPDStream().getCOSObject().setItem(COSName.ALTERNATE, srcCspaceType == ColorSpace.TYPE_GRAY ? COSName.DEVICEGRAY : (srcCspaceType == ColorSpace.TYPE_CMYK ? COSName.DEVICECMYK : COSName.DEVICERGB)); pdColorSpace = pdProfile; } } PDImageXObject imageXObject = new PDImageXObject(document, new ByteArrayInputStream(stream.toByteArray()), COSName.FLATE_DECODE, w, h, bitsPerComponent, pdColorSpace); COSDictionary decodeParms = new COSDictionary(); decodeParms.setItem(COSName.BITS_PER_COMPONENT, COSInteger.get(bitsPerComponent)); decodeParms.setItem(COSName.PREDICTOR, COSInteger.get(15)); decodeParms.setItem(COSName.COLUMNS, COSInteger.get(w)); decodeParms.setItem(COSName.COLORS, COSInteger.get(srcCspace.getNumComponents())); imageXObject.getCOSObject().setItem(COSName.DECODE_PARMS, decodeParms); if (image.getTransparency() != Transparency.OPAQUE) { PDImageXObject pdMask = prepareImageXObject(document, alphaImageData, image.getWidth(), image.getHeight(), 8 * bytesPerComponent, PDDeviceGray.INSTANCE); imageXObject.getCOSObject().setItem(COSName.SMASK, pdMask); } return imageXObject; }
Example #23
Source File: LosslessFactory.java From gcs with Mozilla Public License 2.0 | 4 votes |
private static PDImageXObject createFromRGBImage(BufferedImage image, PDDocument document) throws IOException { int height = image.getHeight(); int width = image.getWidth(); int[] rgbLineBuffer = new int[width]; int bpc = 8; PDDeviceColorSpace deviceColorSpace = PDDeviceRGB.INSTANCE; byte[] imageData = new byte[width * height * 3]; int byteIdx = 0; int alphaByteIdx = 0; int alphaBitPos = 7; int transparency = image.getTransparency(); int apbc = transparency == Transparency.BITMASK ? 1 : 8; byte[] alphaImageData; if (transparency != Transparency.OPAQUE) { alphaImageData = new byte[((width * apbc / 8) + (width * apbc % 8 != 0 ? 1 : 0)) * height]; } else { alphaImageData = new byte[0]; } for (int y = 0; y < height; ++y) { for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width)) { imageData[byteIdx++] = (byte) ((pixel >> 16) & 0xFF); imageData[byteIdx++] = (byte) ((pixel >> 8) & 0xFF); imageData[byteIdx++] = (byte) (pixel & 0xFF); if (transparency != Transparency.OPAQUE) { // we have the alpha right here, so no need to do it separately // as done prior April 2018 if (transparency == Transparency.BITMASK) { // write a bit alphaImageData[alphaByteIdx] |= ((pixel >> 24) & 1) << alphaBitPos; if (--alphaBitPos < 0) { alphaBitPos = 7; ++alphaByteIdx; } } else { // write a byte alphaImageData[alphaByteIdx++] = (byte) ((pixel >> 24) & 0xFF); } } } // skip boundary if needed if (transparency == Transparency.BITMASK && alphaBitPos != 7) { alphaBitPos = 7; ++alphaByteIdx; } } PDImageXObject pdImage = prepareImageXObject(document, imageData, image.getWidth(), image.getHeight(), bpc, deviceColorSpace); if (transparency != Transparency.OPAQUE) { PDImageXObject pdMask = prepareImageXObject(document, alphaImageData, image.getWidth(), image.getHeight(), apbc, PDDeviceGray.INSTANCE); pdImage.getCOSObject().setItem(COSName.SMASK, pdMask); } return pdImage; }