java - itext:将单词中的单个字母加粗

标签 java itext

是否可以在 itext 中将单词中的特定字母加粗?

考虑这句话,“这是一个词”。我可以把那句话的所有“我”都加粗吗?所以输出将是这样的: “这一个词”

所以为了让第一个词正确(this),我真的必须为一个词创建三个 block ,而不是将所有 block 加在一起吗?还是有另一种方法?

对于三个 block ,我的意思是这样的(我知道它不是工作代码,但为了可读性而这样做):

Chunk chunk1 = new Chunk("th");
Chunk chunk2 = new Chunk("i", bold);
Chunk chunk3 = new Chunk("s");

Paragraph p = new Paragraph(); 
p.add(chunk1); p.add(chunk2); p.add(chunk3)

必须有更好的方法。一些“字符串”我也想着色,比如字符串中的每个字母都有不同的颜色,我是否必须为每个字母创建 block 而不是组合它们?

使用 Android 中的 EditText 框来更改每个字母的颜色,我可以使用 SpannableString。但它与 itext 不同,因为它没有字符。我希望你们明白我的意思,我将在 editText 框中发布两种方法,说明我如何在 android 中解决问题。现在我只想要一个类似的方法来将彩色和粗体文本写入 pdf。

    public SpannableString colorizeText(String text) {
        SpannableString spannableColor = new SpannableString(text);
        int x = 1;
        for(char letter : text.toCharArray()){
            spannableColor.setSpan(new ForegroundColorSpan(returnCorrectColor(letter)), x-1, x, 0);
            x++;
        }
        return spannableColor;
    }

    private int returnCorrectColor(char letter) {
        String theLetter = Character.toString(letter);
        if (theLetter.contains("a")){
            return Color.BLUE;
        }
        else if(theLetter.contains("o")){
            return Color.GREEN;
        }
        return Color.RED;
    }

最佳答案

为什么你说你的代码示例不工作?我已将您的 iText 和 Android 示例合并为一个新示例:ColoredLetters

我写了一个类似于你的方法:

private Chunk returnCorrectColor(char letter) {
    if (letter == 'b'){
        return B;
    }
    else if(letter == 'g'){
        return G;
    }
    return new Chunk(String.valueOf(letter), RED_NORMAL);
}

请注意,我使用了一些常量:

public static final Font RED_NORMAL = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, BaseColor.RED);
public static final Font BLUE_BOLD = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLUE);
public static final Font GREEN_ITALIC = new Font(FontFamily.HELVETICA, 12, Font.ITALIC, BaseColor.GREEN);
public static final Chunk B = new Chunk("b", BLUE_BOLD);
public static final Chunk G = new Chunk("g", GREEN_ITALIC);

现在我可以构造一个 Paragraph像这样:

Paragraph p = new Paragraph();
String s = "all text is written in red, except the letters b and g; they are written in blue and green.";
for (int i = 0; i < s.length(); i++) {
    p.add(returnCorrectColor(s.charAt(i)));
}

结果如下所示:colored_letters.pdf

enter image description here

另一种方法是使用不同的字体,其中 fontB 仅包含字母 b,fontG 仅包含字母 g。然后您可以将这些字体声明为 FontSelector对象,以及普通字体。然后您可以处理字体。有关使用此替代方法的示例,请阅读我对以下问题的回答:iText changing the font color and size when using fontselector

根据评论中的其他问题进行更新

Could i specify the color as a HEX. for example something like BaseColor(#ffcd03); I understand that you can specify it like new BaseColor(0, 0, 255), however HEX would be more useful.

其实我自己更喜欢HEX表示法,因为某些颜色用HEX表示法更容易识别。这就是为什么我经常在 Java 中对整数使用 HEX 表示法,如下所示:new BaseColor(0xFF, 0xCD, 0x03);

此外,还有一个 decodeColor() HtmlUtilities 中的方法可以像这样使用的类:

BaseColor color = HtmlUtilities.decodeColor("#ffcd03");

is there a way to just define the color and leave the font to the default. Something like this: public static final Font RED_NORMAL = new Font(BaseColor.RED);

你可以这样创建字体:

new Font(FontFamily.UNDEFINED, 12, Font.UNDEFINED, BaseColor.RED);

现在当你添加 Chunk用这样的字体来Paragraph , 它应该采用在 Paragraph 级别定义的字体(这就是我第一次编写 iText 时的意图。我应该检查这是否仍然正确)。

My goal is to write a program that colorizes existing PDF files without changing the structure of the PDF. Do you think this approach will work for this purpose (performance etc)?

不,这行不通。如果你看了chapter 6 of the second edition of iText in Action的介绍,您将了解 PDF 不是为编辑而创建的格式。屏幕截图中显示的句子的内部语法如下所示:

BT
36 806 Td
0 -16 Td
/F1 12 Tf
1 0 0 rg
(all text is written in red, except the letters ) Tj
0 g
/F2 12 Tf
0 0 1 rg
(b) Tj
0 g
/F1 12 Tf
1 0 0 rg
( and ) Tj
0 g
/F3 12 Tf
0 1 0 rg
(g) Tj
0 g
/F1 12 Tf
1 0 0 rg
(; they are written in ) Tj
0 g
/F2 12 Tf
0 0 1 rg
(b) Tj
0 g
/F1 12 Tf
1 0 0 rg
(lue and ) Tj
0 g
/F3 12 Tf
0 1 0 rg
(g) Tj
0 g
/F1 12 Tf
1 0 0 rg
(reen.) Tj
0 g
0 0 Td
ET

如您所见,Tf运算符(operator)更改字体:

  • /F1是常规字体,
  • /F2是粗体,
  • /F3是斜体。

这些名称指的是 /Resources 中的字体页面的字典,您可以在其中找到对实际字体描述符的引用。

rg运算符(operator)更改颜色。当我们使用红色、绿色和蓝色时,您会认出运算符 1 0 0 , 0 1 00 0 1 .

这看起来相当简单,因为我们使用了 WINANSI 编码。可以解析内容流,查找所有 PDF 字符串(在 () 之间或在 <> 之间),然后引入 Tfrg运营商。

然而:内容流并不总是像这里一样微不足道。可以使用自定义编码,可以使用 Unicode,...看看我对以下问题的回答中的第二个屏幕截图:Can't get Czech characters while generating a PDF .您会明白,您计划更改现有 PDF 中的颜色是一项非常重要的任务。我们过去曾为我们的一位客户做过这样的项目。这是一个耗时数周的项目。

关于java - itext:将单词中的单个字母加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080565/

相关文章:

c# - ITextSharp : Text not wrapping around image within cell

c# - 使用 iTextSharp 将多个 TIFF 图像转换为 PDF

使用 iTextSharp XMLWorker 将 HTML 转换为 PDF 时,CSS 不适用

java - 如何使用 itext7 将页面从一个 PdfDocument 复制到另一个 PdfDocument

java - 将图像附加到 RTF 文档

java - 参数太少。预期 1 个错误 - 使用 MS Access 的 DateDiff 函数时

java - 添加 pacman 图像

java - 如何用gradle编译单个java文件?

java - 开发应用程序以 native 运行还是跨平台?

java - 使用字符串替换删除标点符号在 Java 中不起作用