[英]how set custom color using itext 7
I cannot find a solution for a very simple question, how can I set a custom color for a text/line/etc.我找不到一个非常简单的问题的解决方案,如何为文本/行/等设置自定义颜色。 using iText7 in java code?
在java代码中使用iText7?
I found this reply for iText5 but in version 7 there is no BaseColor class... 我找到了 iText5 的回复,但在版本 7中没有 BaseColor类...
I use this code to customize the text color:我使用此代码自定义文本颜色:
com.itextpdf.kernel.color.Color myColor = new DeviceRgb(255, 100, 20);
Paragraph colorPara = new Paragraph("text with color").setFontColor(myColor);
One option is to use the ColorConstants
.一种选择是使用
ColorConstants
。 It is located in the kernel dependency.它位于内核依赖项中。
PdfCanvas canvas = new PdfCanvas(pdfPage);
canvas.setColor(ColorConstants.DARK_GRAY, true);
I found the following solution after some try-and-fail loop:经过一些尝试和失败循环后,我找到了以下解决方案:
float[] col = new float[]{0,0.5f,0};
Color szin = Color.makeColor(Color.GREEN.getColorSpace(), col);
Canvas canvas = new Canvas(pdfCanvas, pdfDoc, page.getPageSize());
canvas.setProperty(Property.FONT_COLOR, szin);
At first, I had no idea about how can I get/set that color space, what was required as first parameter of the makeColor method.起初,我不知道如何获取/设置该颜色空间,即 makeColor 方法的第一个参数需要什么。 After logging out the following
注销后如下
LOGGER.info(Color.GREEN.getColorSpace().getPdfObject());
I saw, it is an RGB related info, so maybe I should specify the second float[] with 3 elements (not 4, like cmyk).我看到了,这是一个 RGB 相关信息,所以也许我应该用 3 个元素(而不是 4 个,如 cmyk)指定第二个 float[]。
Info: 2464035 [http-listener-1(3)] INFO fornax.hu.pdf.generate.PdfCreator2 - /DeviceRGB
信息:2464035 [http-listener-1(3)] 信息 fornax.hu.pdf.generate.PdfCreator2 - /DeviceRGB
The other big problem was, how should I set the float values.另一个大问题是,我应该如何设置浮点值。 Logical tip was for a dark green is 62,172,62, but I saw nothing.
逻辑提示是深绿色是 62,172,62,但我什么也没看到。 I had to realize, 0 acting as 0, but any number greater than 1 act as 255 in the result color, so tried to set values between 0 and 1, and I got the JACKPOT!
我必须意识到,0 充当 0,但任何大于 1 的数字在结果颜色中都充当 255,因此尝试将值设置在 0 和 1 之间,我得到了大奖!
test color 1 with {1,0.5f,0} test color 2 with {0,0.5f,0}使用 {1,0.5f,0}测试颜色 1使用 {0,0.5f,0}测试颜色 2
Special thanks for iText7 documentation writers, who were unable to insert any example for this very very basic stuff for noobs like me.特别感谢 iText7 文档编写者,他们无法为像我这样的菜鸟插入这个非常基本的东西的任何示例。
Cell hcell = new Cell();
Paragraph paragraph = new Paragraph("Your Text").setTextAlignment(TextAlignment.CENTER).setFontSize(8);
hcell.add(paragraph);
Color color = WebColors.getRGBColor("red"); // Color name to RGB
hcell.setBackgroundColor(color);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.