如何使用 iText 为我的 (pdf-) 文本添加背景颜色以使用 Java 创建它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12020658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add a background color to my (pdf-) text using iText to create it with Java
提问by Waylander
At first: My knowledge with frameworks which you can use for pdf creation isn't the best, please keep this in mind when answering.
首先:我对可用于创建 pdf 的框架的了解不是最好的,请在回答时记住这一点。
I needed a framework/library with which I can create pdf-files in java and (important!) place text at certain x and y coordinates. After a lot of research I experienced that I can realize this with iText.
我需要一个框架/库,我可以用它在 Java 中创建 pdf 文件并(重要!)将文本放置在特定的 x 和 y 坐标处。经过大量研究,我发现我可以通过 iText 实现这一点。
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)
这是一个简单的代码片段,它基本上显示了我现在在 iText 中使用文本所做的事情。您可以简单地将它复制到您的编程环境中,您只需要 iText jar(可在此处下载: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:
我已经添加了将我的数据源中的文本添加到此方法的可能性,它真的很好用。结果对我来说非常有希望,所以目前我只剩下一项任务:
I need to add specfic background colors to my text (not font color!) that I'm moving and placing in the method shown above.
我需要将特定的背景颜色添加到我正在移动并放置在上面显示的方法中的文本中(不是字体颜色!)。
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.
感谢您的每一个回答,并认为您正在与我分享。
采纳答案by Waylander
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);
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.
您在一个块中定义文本(块只是最小的文本块),并为该块指定文本以及字体(其中还包括文本的大小)。在您的块上,您可以设置背景颜色,总体而言,您将使用显示的“showTextAligned”-方法将其添加到文档中。
x and y here specify the coordinates and cb stands for the PdfContentByte.
x 和 y 在这里指定坐标,cb 代表 PdfContentByte。
回答by Kumar Vivek Mitra
Use setBackgroundColor()
method
使用setBackgroundColor()
方法
See this example here :
在此处查看此示例: