使用iTextSharp,有没有办法让文本保持在一个矩形

本文关键字:一个 有没有 iTextSharp 文本 使用 | 更新日期: 2023-09-27 17:52:47

我有一个现有的PDF(不带表单字段-更多的是扫描文档),我正在使用PdfReader加载PDF"模板",以便我在上面写文本。

对于位置简单字段,我使用:

PdfReader reader = new PdfReader(templatePath);

Chunk chunk = new Chunk(text, fontToUse);
Phrase phrase = new Phrase();
phrase.Add(chunk);
PdfContentByte canvas = this.PdfWriter.DirectContent;
ColumnText.ShowTextAligned(this.PdfContentByte, alignment, phrase, left, top, 0);

我还需要写一些文本到一个特定的区域,这是一个400 x 200的矩形。由于文本的大小不同,它可能适合也可能不适合矩形。

是否有一种方法来写文本到矩形,如果文本太大,它根本不出现(像溢出隐藏将在HTML中工作)?

使用iTextSharp,有没有办法让文本保持在一个矩形

明白了!

Phrase myText = new Phrase(text);
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 300;
table.LockedWidth = true;
PdfPCell cell = new PdfPCell(myText);
cell.Border = 0;
cell.FixedHeight = 40;
table.AddCell(cell);
table.WriteSelectedRows
(
 0, 
 -1, 
 300,
 700,
 writer.DirectContent
);
相关文章: