C# 创建PDF文本域并限制输入字体、字号、颜色

时间:2026-02-14 10:13:32

1、下载安装后,在编辑代码时,请注意添加引用Spire.Pdf.dll(dll文件可在安装路径下的Bin文件夹下获取)

C# 创建PDF文本域并限制输入字体、字号、颜色

1、using Spire.Pdf;

using Spire.Pdf.Fields;

using Spire.Pdf.Graphics;

using System.Drawing;

namespace FixTextSize_PDF

{

    class Program

    {

        static void Main(string[] args)

        {

            //创建PdfDocument实例

            PdfDocument doc = new PdfDocument();

            //添加一页

            PdfPageBase page = doc.Pages.Add();

            //初始化PdfTextBoxField类的对象

            PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox");

            //指定文本框在页面中的位置及大小

            textbox.Bounds = new RectangleF(30, 20, 200, 60);

            //指定文本框边框样式

            textbox.BorderWidth = 0.75f;

            textbox.BorderStyle = PdfBorderStyle.Solid;

            textbox.BorderColor = Color.DarkGreen;

            //设置可输入多行(自动换行)

            textbox.Multiline = true;

            //指定文本框中字体、字号、字体颜色

            textbox.Font = new PdfTrueTypeFont(new Font("宋体", 10f, FontStyle.Regular), true);

            textbox.ForeColor = Color.Blue;

            //添加文本框到PDF

            doc.Form.Fields.Add(textbox);

            //保存文档

            doc.SaveToFile("output.pdf");

            System.Diagnostics.Process.Start("output.pdf");

        }

    }

}

2、完成代码后,调试程序,生成文档。如下图:

C# 创建PDF文本域并限制输入字体、字号、颜色

© 2026 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com