如何使用iText为我的(pdf-)文本添加背景颜色以使用Java创建它 [英] How can I add a background color to my (pdf-) text using iText to create it with Java

查看:1103
本文介绍了如何使用iText为我的(pdf-)文本添加背景颜色以使用Java创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:我对用于创建pdf的框架的了解并不是最好的,请在回答时牢记这一点。

At first: My knowledge with frameworks which you can use for pdf creation isn't the best, please keep this in mind when answering.

我需要一个框架/库,我可以用java创建pdf文件,并且(重要的!)将文本放在某个x和y坐标处。经过大量研究后,我发现我可以通过iText实现这一点。

这是一个简单的代码片段,基本上显示了我现在正在做的事情用我在iText的文字。您只需将其复制到您的编程环境中,您只需要iText jar(可在此下载: http://sourceforge.net/projects/itext/files/latest/download?source=files

Here is a simple code snippet that basically shows what I'm doing right now with my text in iText. You can simply copy it into your programming environment, all you need is the iText jar (downloadable for you here: http://sourceforge.net/projects/itext/files/latest/download?source=files)

import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFTesting {

public static void main(String[] args) {
    Document document = new Document();
    try {
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("output.pdf"));
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        int x = 100; 
        int y = 500; 

        String text = "Hello"; 

        // now we can place content elements on the page
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.saveState();
        cb.beginText();
        cb.moveText(x, y);
        cb.setFontAndSize(bf, 12);
        cb.showText(text);
        cb.endText();
        cb.restoreState();

    } catch (Exception e) {
    }

    document.close();
}
}

我添加了从我的数据源添加文本的可能性这个方法真的很棒。结果对我来说非常有希望,因此目前我只剩下一项任务:

I have added possibilities to add my text from my datasources to this method and it's really working great. The result looks very promising to me and so is only one task for me left at the moment:

我需要在文本中添加特定的背景颜色(不是字体颜色!)我正在移动并放置在上面显示的方法中。

我的研究没有向我提供任何关于此的初学友好信息任务,所以如果你能帮我解决这个问题,我会非常高兴。

My research didn't provide me any beginner-friendly information about this task so I would be really happy if you could help me solve this.

如果可能的话:你可以通过添加背景颜色的方式修改我的示例吗?添加的示例文本?我想我和其他人(可能在将来阅读这个帖子有相同的问题)将从中受益最多。

If possible: can you modify my example in a way that adds a background color to the added example-text? I guess me and others (who may be reading this thread in the future having the same problem) would benefit most from this.

如果您需要更多信息或有其他建议,请随时与我联系。

If you need further information or have additional suggestions for me please feel free to contact me as well.

感谢您的回答,并认为您与我分享。

Thanks for every answer and thought you're sharing with me.

推荐答案

我找到了问题的解决方案。

I have found the solution to my question.

Chunk textAsChunk = new Chunk(text, textFont);
textAsChunk.setBackground(new BaseColor(120, 200, 50));

ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(textAsChunk), x, y, 0);

你在一个块中定义你的文本(Chunk只是最小的一段文本)并给出这个块您的文本以及字体(还包括文本的大小)。在您的块上,您可以设置backgroundcolor,并且您将整体添加到Document中,并显示showTextAligned-Method。

You define your text in a chunk (Chunk is just the smallest piece of text) and give this chunk your text as well as the Font (which also includes the size of your text). On your chunk you can setup the backgroundcolor and overall you're adding this to the Document with the shown "showTextAligned"-Method.

x和y在这里指定坐标,cb代表PdfContentByte。

x and y here specify the coordinates and cb stands for the PdfContentByte.

这篇关于如何使用iText为我的(pdf-)文本添加背景颜色以使用Java创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆