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

  • 苹果电脑windows7版怎么进入PE系统
  • GHOST重装系统出现Decompression error Abort
  • U盘图标被启动工具修改,如何恢复为默认图标
  • win10如何进入WinRE恢复环境
  • 一键清理系统垃圾bat
  • 热门搜索
    根号怎么打出来 初级会计证书怎么领取 苹果6怎么下载软件 怎么投诉卖家 微波炉怎么做蛋糕 血压低头晕怎么办 ppt怎么加背景音乐 额头长痘痘怎么办 月经痛经怎么办 写信格式怎么写