C#将打印按钮添加到PDF文档

在Spire.PDF中可以向PDF文档添加打印按钮,单击该按钮时,将打开打印对话框。在本教程中,我将向您展示如何使用Spire.PDF向现有pdf文档添加打印按钮。

要完成此任务,首先需要创建PdfButtonField类的实例。此类还允许您定义按钮的外观。例如,您可以设置按钮的文本,文本颜色,背景颜色,边框颜色等。其次,您需要通过调用AddPrintAction()方法将打印操作添加到按钮。最后,将按钮添加到文档并将文档保存到文件。以下代码示例解释相同。

代码片段

步骤1:加载PDF文档并启用表单创建。

PdfDocument doc = new PdfDocument("Input.pdf");
doc.AllowCreateForm = true;

第2步:获取第一页。

PdfPageBase page = doc.Pages[0];


第3步:创建PdfButtonField实例并设置按钮的属性。

PdfButtonField button = new PdfButtonField(page, "Print");
button.Bounds = new RectangleF(280, 600, 50, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ForeColor = new PdfRGBColor(Color.White);
button.BackColor = new PdfRGBColor(Color.Blue);
button.ToolTip = "Print";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);


第4步:向按钮添加打印操作。

button.AddPrintAction();

第5步:将按钮添加到文档中。

doc.Form.Fields.Add(button);

第6步:保存文档。

doc.SaveToFile("Output.pdf");


得到的文件如下所示:

在C#中将打印按钮添加到PDF文档

完整代码

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;

namespace Add_print_button
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the PDF document
            PdfDocument doc = new PdfDocument("Input.pdf");
            //Enable form creation
            doc.AllowCreateForm = true;
            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Create a PdfButtonField instance
            PdfButtonField button = new PdfButtonField(page, "Print");
            //Set button properties
            button.Bounds = new RectangleF(280, 600, 50, 20);
            button.BorderColor = new PdfRGBColor(Color.AliceBlue);
            button.BorderStyle = PdfBorderStyle.Solid;
            button.ForeColor = new PdfRGBColor(Color.White);
            button.BackColor = new PdfRGBColor(Color.Blue);
            button.ToolTip = "Print";
            button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);

            //Add print action to the button
            button.AddPrintAction();

            //Add the button to document
            doc.Form.Fields.Add(button);            

            //Save the document
            doc.SaveToFile("Output.pdf");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值