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

2025-10-31 00:27:31

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文本域并限制输入字体、字号、颜色

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢