对于像您这样的任务,静态便捷方法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 相同。)
你没有在这个问题中提到尺寸,但在你之前的文章中,删除了你提到的以毫米为单位的尺寸,所以我在这里也使用了毫米。
空源页面上的结果: