iTextSharp从硬盘读取pdf模板并写入文本

时间:2010-01-11 00:59:25

标签: c# templates itextsharp

我有一个基本的PDF文件,我有5个不同的空白内容区域,我想用iTextSharp写文本。问题是我不确定实现这一目标的最佳方法。我试图使用ColumnText来完成此任务,但我似乎无法添加多个ColumnText对象。

            ColumnText tagColumn = new ColumnText(pdfContentByte);
            tagColumn.SetSimpleColumn(460, 100, 620, 160);
            string[] tagColors = bt.DealerTagColor.Split('|');
            Font tagFont = FontFactory.GetFont(bt.DealerTagFont, bt.DealerTagSize, Font.NORMAL, new BaseColor(int.Parse(tagColors[0]), int.Parse(tagColors[1]), int.Parse(tagColors[2])));
            Paragraph p = new Paragraph(dto.Tag, tagFont);
            p.Leading = bt.DealerTagSize;
            tagColumn.AddElement(p);
            tagColumn.Go();

我喜欢ColumnText,它允许我必须定义文本区域的高度/宽度和位置。

有关如何使用或不使用ColumnText实现此目的的任何想法?我只需要控制区域的字体/字体大小/字体颜色/前导/宽度,这将允许文本换行。

1 个答案:

答案 0 :(得分:3)

最简单的方法是在模板PDF上创建和使用表单字段。这样,您可以按名称引用表单字段,并通过简单的函数调用设置其值。

这篇文章帮助了我:

Fill in PDF Form Fields using the Open Source iTextSharp Dynamic Link Library

编辑: PdfPTables?

如果您的ColumnTexts以表格形式排列,请考虑使用PdfPTable。我已经使用PdfPTables来填充从空白模板表单生成的表单中的数据行。您需要找出每个列的x坐标,并在将PdfPCells添加到表中时使用这些值,但它确实有效。我从来没有必要设置PdfPCell高度的上限,但是我可以用它做图像。

检查this page以获取有关PdfPTables的更多信息。