一、簡介
PDFSharp是一款用於生成和編輯PDF文檔的開源工具。它提供了許多有用的功能,包括創建新文檔、添加圖像、文本、表格和頁面等,同時還支持加密、添加書籤、頁碼,並具有一些其他高級功能,例如向PDF文檔添加數字簽名。PDFSharp基於.NET標準,可以在各種操作系統和平台上使用。
二、創建PDF文檔
使用PDFSharp可以輕鬆地創建新PDF文檔。首先,創建一個新的PDF文檔對象,指定文檔屬性,例如作者、主題和創建日期:
PdfDocument document = new PdfDocument(); document.Info.Author = "John Doe"; document.Info.Title = "My Title"; document.Info.Subject = "My Subject"; document.Info.Keywords = "PDFSharp, PDF, API"; document.Info.CreationDate = DateTime.Now;
接下來,可以為文檔添加一個或多個頁面。頁面是PDF文檔的基本單位,可以包含文本、圖像、表格等元素。以下是創建一個新頁面的示例:
PdfPage page = document.AddPage();
然後,可以在頁面上添加各種元素,例如文字和圖像。例如,下面是向頁面添加一個簡單文本框的代碼:
XFont font = new XFont("Verdana", 20, XFontStyle.Bold); XBrush brush = new XSolidBrush(XColor.FromKnownColor(KnownColor.Black)); gfx.DrawString("Hello, World!", font, brush, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
三、添加圖像
PDFSharp支持多種圖像格式,包括BMP、JPG、PNG、GIF和TIFF等。以下是添加圖像的示例:
XImage image = XImage.FromFile("picture.jpg"); gfx.DrawImage(image, 0, 0, page.Width, page.Height);
四、添加表格
PDFSharp可以輕鬆地在頁面上添加表格。以下是創建一個新表格並向其中添加數據的示例代碼:
PdfTable table = new PdfTable(); table.Style = "Table"; table.Rows.Add(new PdfTableRow { Cells = { new PdfTableCell { Value = "Name" }, new PdfTableCell { Value = "Age" } } }); table.Rows.Add(new PdfTableRow { Cells = { new PdfTableCell { Value = "John Doe" }, new PdfTableCell { Value = "30" } } }); table.Rows.Add(new PdfTableRow { Cells = { new PdfTableCell { Value = "Jane Doe" }, new PdfTableCell { Value = "28" } } }); table.Draw(gfx, new XRect(0, 0, page.Width, page.Height));
五、數字簽名
PDFSharp支持向PDF文檔添加數字簽名,以確保文檔的完整性和真實性。以下是向PDF文檔添加數字簽名的示例代碼:
PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); X509Certificate2 cert = new X509Certificate2("certificate.pfx", "password"); PdfSignatureOptions options = new PdfSignatureOptions { Certificate = cert, ContactInfo = "John Doe", Location = "My Location", Reason = "My Reason", SignatureFormat = PdfSignatureFormat.Pkcs7 }; options.DocumentPermissions = PdfDocumentPermissions.AllowPrinting; PdfSignature signature = new PdfSignature(options); signature.SignatureAppearance = new PdfSignatureAppearance { CustomAppearanceText = "My Custom Text", }; signature.Sign(page, gfx);
六、加密
PDFSharp也支持對PDF文檔進行加密。以下是對PDF文檔進行加密的示例代碼:
PdfEncoder encoder = new PdfEncoder(); encoder.SetEncryption(new PdfEncryptionOptions { UserPassword = "password", OwnerPassword = "owner", Permissions = PdfDocumentPermissions.AllowPrinting }); document.Save("document.pdf", encoder);
七、書籤與目錄
PDFSharp還支持在PDF文檔中添加書籤和目錄。以下是創建一個新目錄並向其中添加條目的示例:
PdfOutline root = document.Outlines.Add(null, "My Outline", true, PdfOutlineStyle.Bold, XColors.Red); PdfOutline child = document.Outlines.Add(root, "My Child", false, PdfOutlineStyle.Regular, XColors.Blue); PdfOutline subChild = document.Outlines.Add(child, "My Sub Child", false, PdfOutlineStyle.Italic, XColors.Green);
八、總結
PDFSharp是一個強大的PDF生成工具,可以幫助開發人員輕鬆地創建、編輯和加密PDF文檔。它支持許多有用的功能,如添加圖像、表格、數字簽名和書籤等,而且還相對容易學習。如果需要生成PDF文檔,PDFSharp是一個值得考慮的工具。
原創文章,作者:BVNY,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/131442.html