2

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

我尝试使用下面的代码快照来实现它,但我希望它被包含在盒子的特定尺寸内,我无法使用这种ColumnText方法控制它

例如:

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

1 回答 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();

添加文本框测试testRotatedBoxForAbbas

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

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

空源页面上的结果:

截屏

于 2020-10-09T14:39:58.943 回答