本文将介绍如何通过Spire.PDF在PDF文档中添加具有打印功能的按钮。
代码:
C#
//加载PDF文档
PdfDocument doc = new PdfDocument("Input.pdf");
doc.AllowCreateForm = true;
//在第一页创建一个PdfButtonField实例,并为按钮设置属性
PdfPageBase page = doc.Pages[0];
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.Text = "Print";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
//将打印功能添加到按钮中
button.AddPrintAction();
//添加按钮
doc.Form.Fields.Add(button);
//保存文档
doc.SaveToFile("Output.pdf");
VB.NET
'加载PDF文档 Dim doc As New PdfDocument("Input.pdf") doc.AllowCreateForm = True '在第一页创建一个PdfButtonField实例,并为按钮设置属性
Dim page As PdfPageBase = doc.Pages(0)
Dim button As 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.Text = "Print" button.Font = New PdfFont(PdfFontFamily.Helvetica,9F) '将打印功能添加到按钮中
button.AddPrintAction()
'添加按钮 doc.Form.Fields.Add(button) '保存文档
doc.SaveToFile("Output.pdf")
效果图: