用C1PDF控件从应用程序创建支持AcroForm的PDF文件

本文介绍如何使用C1PDF控件,从应用程序创建支持AcroForm的PDF文件。步骤如下:1.创建C1PdfDocument2.C1PDF支持AcroForms3.保存PDF文档生成如下的PDF文档:<p><a href="http://www.grapecity.com.cn/image.ashx?picture=PDF.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="PDF" src="http://www.grapecity.com.cn/image.ashx?picture=PDF_thumb.png" alt="PDF" width="384" height="386" border="0" /></a></p>

发布于 2014/05/27 00:00

ComponentOne Enterprise

本文介绍如何使用C1PDF控件,从应用程序创建支持AcroForm的PDF文件。

C1PDF控件将内容添加到文档所使用的命令与.NET Graphics类中的命令类似。同时,PDF for .NET控件提供安全、压缩、大纲、超链接,以及附件等功能。

创建C1PdfDocument

首先从工具箱拖拽或是用code生成C1PdfDocument。生成代码如下:

private C1.C1Pdf.C1PdfDocument _c1pdf = new C1.C1Pdf.C1PdfDocument();

其次,将内容添加到PDF文档,这时候所使用的命令与在WinForms的Grphics类中的命令相似,它可以添加文本、图像、线条、矩形、椭圆、扇形、弧形、圆角矩形、多边形,贝塞尔曲线,以及更多。 可以参考C1PdfDocument的DrawString,DrawImage,DrawLine等方法。

C1PDF支持AcroForms

可以使用AddField方法将Acrobat表单字段添加到PDF文件。PDF for .NET控件支持如下字段类型:文本框、复选框、单选按钮、下压按钮、组合框、列表框,以及签名栏。在本文的Demo文件中,有对所有类型的支持代码。

现在用文本框举例,如果添加文本框到PDF文件,则需要:

首先创建文本框字段,代码如下:

// text box field
            rc = new RectangleF(rc.X, rc.Y + rc.Height / 10, rc.Width / 3, rc.Height / 30);
            PdfTextBox textBox1 = RenderTextBox("TextBox Sample", fieldFont, rc);
            textBox1.BorderWidth = FieldBorderWidth.Thick;
            textBox1.BorderStyle = FieldBorderStyle.Inset;
            textBox1.BorderColor = Color.Green;

然后把文本框字段使用AddField方法添加到PDF文档。具体代码如下:

 // add text box field for fields of the PDF document
        // with common parameters and default names.
        // 
        internal PdfTextBox RenderTextBox(string text, Font font, RectangleF rc, Color back, string toolTip)
        {
            // create
            string name = string.Format("ACFTB{0}", _textBoxCount + 1);
            PdfTextBox textBox = new PdfTextBox();

            // default border
            //textBox.BorderWidth = 3f / 4;
            textBox.BorderStyle = FieldBorderStyle.Solid;
            textBox.BorderColor = SystemColors.ControlDarkDark;

            // parameters
            textBox.Font = font;
            textBox.Name = name;
            textBox.DefaultText = text;
            textBox.Text = text;
            textBox.ToolTip = string.IsNullOrEmpty(toolTip) ? string.Format("{0} ({1})", text, name) : toolTip;
            if (back != Color.Transparent && !back.IsEmpty)
            {
                textBox.BackColor = back;
            }

            // add
            _c1pdf.AddField(textBox, rc);
            _textBoxCount++;

            // done
            return textBox;
        }
        internal PdfTextBox RenderTextBox(string text, Font font, RectangleF rc, Color back)
        {
            return RenderTextBox(text, font, rc, back, null);
        }
        internal PdfTextBox RenderTextBox(string text, Font font, RectangleF rc)
        {
            return RenderTextBox(text, font, rc, Color.Transparent, null);
        }

保存PDF文档

最后可以保存在应用程序中生成的PDF文件,使用C1PdfDocument下的Save方法,保存在IO.Stream中或是保存在文件里。该方法如下所示:

	//
        // 摘要:
        //     Saves the Pdf document to a System.IO.Stream.
        public void Save(Stream stream);
        //
        // 摘要:
        //     Saves the Pdf document to a file.     
        public void Save(string fileName);

运行本文附件中的Demo,点击Form上的按钮,生成如下的PDF文档:

PDF

本文Demo的源代码如下: 

PdfAcroform.7z (395.52 kb)

ComponentOne Enterprise | 下载试用

ComponentOne 是一套专注于企业 .NET开发、支持 .NET Core 平台,并完美集成于 Visual Studio 的第三方控件集,包含 300 多种 .NET开发控件,提供表格数据管理、数据可视化、报表和文档、日程安排、输入和编辑、导航和布局、系统提升工具等七大功能,被誉为“.NET开发的‘瑞士军刀’”。

ComponentOne 为您提供专业的产品咨询服务,并由技术支持工程师为您1对1解答。>> 发帖提问

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态