如何使用iText 2.1.7在PDF中垂直创建矩形并添加文本:

huangapple go评论72阅读模式
英文:

How to create rectangle vertically and add text to it in PDF using itext 2.1.7

问题

我的要求很简单,我想在PDF的所有页面上创建一个垂直定位的矩形,并从底部向上方向添加文本(基本上是一个90度的框)。

我尝试使用以下代码片段来实现,但我希望它被包含在特定尺寸的框内,而使用这种“ColumnText”方法无法控制该尺寸。

例如:

ColumnText.showTextAligned(canvas,PdfContentByte.ALIGN_LEFT,note,
.03F * pageWidth,.68F * pageHeight,90);

英文:

My requirement is straight, I want to create a rectangle positioned vertically and add text to it starting from bottom to going upward direction. (basically 90 degree box) in all pages of PDF

I tried achieving it using below code snap, but I want it to be enclosed within a specific dimension of box, which I cant control using this ColumnText approach

for example:

ColumnText.showTextAligned(canvas, PdfContentByte.ALIGN_LEFT, note,
                        .03F * pageWidth, .68F * pageHeight, 90);

答案1

得分: 1

对于类似你的任务,ColumnText 的静态便利方法是不够的,你需要实际创建并为一个完整的 ColumnText 实例设置参数。

例如像这样:

float width = Utilities.millimetersToPoints(10);
float height = Utilities.millimetersToPoints(100);
float x = Utilities.millimetersToPoints(15);
float y = Utilities.millimetersToPoints(150);
float fontHeight = Utilities.millimetersToPoints(4);
String content = "Some text to fill the box. There's nothing really to say, just a box to fill. So let's fill the box.";

PdfReader reader = new PdfReader(YOUR_SOURCE_FILE);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "RotatedBoxForAbbas.pdf")));
Rectangle cropBox = reader.getCropBox(1);
PdfContentByte canvas = stamper.getOverContent(1);
canvas.concatCTM(0, 1, -1, 0, cropBox.getLeft() + x + width, cropBox.getBottom() + y);
canvas.rectangle(0, 0, height, width);
canvas.stroke();
ColumnText columnText = new ColumnText(canvas);
columnText.addText(new Chunk(content, new Font(FontFamily.HELVETICA, fontHeight)));
columnText.setLeading(fontHeight);
columnText.setSimpleColumn(2, 0, height - 4, width);
columnText.go();
stamper.close();

(AddTextBox 测试 testRotatedBoxForAbbas)

(虽然此测试是为 iText 5 创建的,但在适应导入包后,它应该与 iText 2.1.7 和 OpenPdf 相同。)

你在这个问题中没有提到尺寸,但在你之前删除的问题中提到了以毫米为单位给出的尺寸,所以我在这里也使用了毫米。

在空白的源页面上的结果:

如何使用iText 2.1.7在PDF中垂直创建矩形并添加文本:

英文:

For a task like yours the static convenience methods of ColumnText don't suffice, you need to actually create and parameterize a full ColumnText instance.

For example like this:

float width = Utilities.millimetersToPoints(10);
float height = Utilities.millimetersToPoints(100);
float x = Utilities.millimetersToPoints(15);
float y = Utilities.millimetersToPoints(150);
float fontHeight = Utilities.millimetersToPoints(4);
String content = "Some text to fill the box. There's nothing really to say, just a box to fill. So let's fill the box.";

PdfReader reader = new PdfReader(YOUR_SOURCE_FILE);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "RotatedBoxForAbbas.pdf")));
Rectangle cropBox = reader.getCropBox(1);
PdfContentByte canvas = stamper.getOverContent(1);
canvas.concatCTM(0, 1, -1, 0, cropBox.getLeft() + x + width, cropBox.getBottom() + y);
canvas.rectangle(0, 0, height, width);
canvas.stroke();
ColumnText columnText = new ColumnText(canvas);
columnText.addText(new Chunk(content, new Font(FontFamily.HELVETICA, fontHeight)));
columnText.setLeading(fontHeight);
columnText.setSimpleColumn(2, 0, height - 4, width);
columnText.go();
stamper.close();

(AddTextBox test testRotatedBoxForAbbas)

<sup>(While this test has been created for iText 5, it should work identically with iText 2.1.7 and OpenPdf after adapting the import packages.)</sup>

You didn't mention dimensions in this question but in your previous, removed one you mentioned dimensions given in mm, so I also used millimeters here.

The result on an empty source page:

如何使用iText 2.1.7在PDF中垂直创建矩形并添加文本:

huangapple
  • 本文由 发表于 2020年10月7日 16:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64239746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定