C# 在PDF頁面添加打印按鈕

簡述

在文檔中設置按鈕給我們提供了一種快速操作文檔的方式,簡潔省事,應用於程序中能夠有效的提升客戶滿意度。在前一篇文章中講述了如何在PDF文檔中設置頁面的跳轉按鈕,包括跳轉至指定頁,包括首頁、下一頁、上一頁、最後一頁,同時也可以自定義跳轉頁。本篇文章將介紹如何在PDF文檔頁面中添加打印按鈕。

使用工具

  • Spire.PDF for .NET 版本 4.0
    :安裝該類庫後,注意在項目程序中添加引用Spire.Pdf.dll。dll文件可以在安裝路徑下的Bin文件夾中獲取。

C# 在PDF頁面添加打印按鈕

代碼示例(供參考)
步驟 1 :添加using指令

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

步驟 2 :加載文檔,獲取指定頁

//加載PDF文檔
PdfDocument doc = new PdfDocument("test.pdf");
doc.AllowCreateForm = true;
//獲取第一頁
PdfPageBase page = doc.Pages[0];

步驟 3 :設置打印按鈕及其屬性

//在第一頁創建一個PdfButtonField實例,併爲按鈕設置屬性
PdfButtonField button = new PdfButtonField(page, "Print");
button.Bounds = new RectangleF(450, 600, 50, 20);
button.BorderColor = new PdfRGBColor(Color.White);
button.BorderStyle = PdfBorderStyle.Solid;
button.ForeColor = new PdfRGBColor(Color.White);
button.BackColor = new PdfRGBColor(Color.LightGray);
button.ToolTip = "Print";
button.Text = "Print";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);

步驟 4 :應用按鈕到頁面

//添加打印功能到按鈕
button.AddPrintAction();
//添加按鈕到頁面
doc.Form.Fields.Add(button);

步驟 5 :保存文檔
doc.SaveToFile("Output.pdf");
System.Diagnostics.Process.Start("Output.pdf");

完成代碼後,調試運行程序,生成文檔(如下圖所示)。生成的文檔中,鼠標點擊打印按鈕,即可彈出打印對話框,在對話框中設置打印需求,點擊打印即可完成打印。
C# 在PDF頁面添加打印按鈕

(本文完)
如需轉載,請註明出處

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章