【问题标题】:iTextSharp read pdf template from hard drive and write textiTextSharp 从硬盘读取 pdf 模板并写入文本
【发布时间】:2010-01-11 00:59:25
【问题描述】:

我有一个基本的 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 的情况下完成此任务的任何想法?我只需要控制允许文本换行的区域的字体/字体大小/字体颜色/前导/和宽度。

【问题讨论】:

    标签: c# templates itextsharp


    【解决方案1】:

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

    这篇文章帮助了我:

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

    编辑: PdfPTables?

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

    查看this page 了解有关 PdfPTables 的更多信息。

    【讨论】:

    • 感谢您的回复。我不想填写表单字段,我只想在 PDF 上取一个区域,比如 100 像素 x 100 像素并覆盖一个包含在 100 x 100 区域内的文本区域
    猜你喜欢
    • 1970-01-01
    • 2018-10-14
    • 2011-01-09
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多