此条经验将介绍如何通过C#编程在PDF中创建文本框,同时并设置用户在文本框中输入文本时,限制其输入文本的字体、字号、字体颜色、文本换行等。
工具/原料
Free Spire.PDF for .NET
Visual Studio
dll引用
1、下载安装后,在编辑代码时,请注意添加引用Spire.Pdf.dll(dll文件可在安装路径下的Bin文件夹下获取)
代码示例(供参考)
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、完成代码后,调试程序,生成文档。如下图: