原創|行業資訊|編輯:胡濤|2024-12-18 11:05:39.450|閱讀 81 次
概述:在本文中,您將學習如何使用Spire.PDF for .NET在 C# 中向 PDF 文檔添加頁碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在 PDF 文檔中添加頁碼不僅實用,而且美觀,因為它提供了類似于專業出版材料的精美外觀。無論您處理的是小說、報告還是任何其他類型的長文檔的數字副本,添加頁碼都可以顯著提高其可讀性和實用性。在本文中,您將學習如何使用Spire.PDF for .NET在 C# 中向 PDF 文檔添加頁碼。
Spire.PDF for .NET 是一款獨立 PDF 控件,用于 .NET 程序中創建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發人員可以新建一個 PDF 文檔或者對現有的 PDF 文檔進行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
首先,您需要將 Spire.PDF for.NET 包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。 可以從此鏈接下載 DLL 文件,也可以通過NuGet安裝。
PM> Install-Package Spire.PDF
當使用 Spire.PDF for .NET 操作現有的 PDF 文檔時,需要注意的是坐標系的原點位于頁面的左上角。x 軸向右延伸,y 軸向下延伸。
通常,頁碼位于文檔的頁眉或頁腳部分。因此,在確定頁碼的適當位置時,考慮頁面大小和頁邊距至關重要。
在 Spire.PDF for .NET 庫中,有兩個可用的類:PdfPageNumberField和PdfPageCountField。這些類允許您在將當前頁碼和總頁數添加到 PDF 文檔的頁面時檢索和顯示它們。如果您希望插入諸如“第 X 頁”或“第 X 頁,共 Y 頁”之類的文本,則可以使用PdfCompositeField類,該類使您能夠將所需文本與一個或多個字段組合成單個字段。以下是使用 C# 在 PDF 頁腳中添加左對齊頁碼的步驟。
【C#】
using Spire.Pdf.AutomaticFields; using Spire.Pdf.Graphics; using Spire.Pdf; using System.Drawing; using Spire.Pdf.License; namespace AddPageNumbersToLeftCorner { class Program { static void Main(string[] args) { // Apply your license key LicenseProvider.SetLicenseKey("License Key"); // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Create font, brush and pen, which determine the appearance of the page numbers to be added PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true); PdfBrush brush = PdfBrushes.Black; PdfPen pen = new PdfPen(brush, 1.0f); // Create a PdfPageNumberField object and a PdfPageCountField object PdfPageNumberField pageNumberField = new PdfPageNumberField(); PdfPageCountField pageCountField = new PdfPageCountField(); // Create a PdfCompositeField object to combine page count field and page number field in a single field PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField); // Get the page size SizeF pageSize = doc.Pages[0].Size; // Set the location of the composite field compositeField.Location = new PointF(72, pageSize.Height - 45); // Iterate through the pages in the document for (int i = 0; i < doc.Pages.Count; i++) { // Get a specific page PdfPageBase page = doc.Pages[i]; // Draw a line at the specified position page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50); // Draw the composite field on the page compositeField.Draw(page.Canvas); } // Save to a different PDF file doc.SaveToFile("AddPageNumbersToLeftCorner.pdf"); // Dispose resources doc.Dispose(); } } }
為了將頁腳部分的頁碼與中心對齊,動態計算文本“第 X 頁,共 Y 頁”的寬度至關重要。此計算至關重要,因為它決定了頁碼 ( PdfCompositeField ) 的 X 坐標。要實現居中對齊,X 坐標的計算方法是從頁面寬度中減去頁碼的寬度,然后將結果除以 2,如下所示:(PageWidth - PageNumberWidth)/2。
以下是使用 C# 在 PDF 頁腳中添加居中對齊頁碼的步驟。
【C#】
using Spire.Pdf.AutomaticFields; using Spire.Pdf.Graphics; using Spire.Pdf; using System.Drawing; using Spire.Pdf.License; namespace AddPageNumbersToCenter { class Program { static void Main(string[] args) { // Apply your license key LicenseProvider.SetLicenseKey("License Key"); // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Create font, brush and pen, which determine the appearance of the page numbers to be added PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true); PdfBrush brush = PdfBrushes.Black; PdfPen pen = new PdfPen(brush, 1.0f); // Create a PdfPageNumberField object and a PdfPageCountField object PdfPageNumberField pageNumberField = new PdfPageNumberField(); PdfPageCountField pageCountField = new PdfPageCountField(); // Create a PdfCompositeField object to combine page count field and page number field in a single field PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField); // Iterate through the pages in the document for (int i = 0; i < doc.Pages.Count; i++) { // Get a specific page PdfPageBase page = doc.Pages[i]; // Get the page size SizeF pageSize = doc.Pages[i].Size; // Draw a line at the specified position page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50); // Measure the size the "Page X of Y" SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count)); // Set the location of the composite field compositeField.Location = new PointF((pageSize.Width - pageNumberSize.Width) / 2, pageSize.Height - 45); // Draw the composite field on the page compositeField.Draw(page.Canvas); } // Save to a different PDF file doc.SaveToFile("AddPageNumbersToCenter.pdf"); // Dispose resources doc.Dispose(); } } }
要將頁碼定位在頁腳部分的右上角,還必須動態計算文本“第 X 頁,共 Y 頁”的寬度。因為頁碼 ( PdfCompositeField ) 的 X 坐標是通過從頁面寬度中減去頁碼和右頁邊距的組合寬度來確定的,如下所示:PageWidth - (PageNumberWidth + RightPageMargin)。
以下是使用 C# 在 PDF 頁腳中添加右對齊頁碼的步驟。
【C#】
using Spire.Pdf.AutomaticFields; using Spire.Pdf.Graphics; using Spire.Pdf; using System.Drawing; using Spire.Pdf.License; namespace AddPageNumbersToRigthCorner { class Program { static void Main(string[] args) { // Apply your license key LicenseProvider.SetLicenseKey("License Key"); // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Create font, brush and pen, which determine the appearance of the page numbers to be added PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true); PdfBrush brush = PdfBrushes.Black; PdfPen pen = new PdfPen(brush, 1.0f); // Create a PdfPageNumberField object and a PdfPageCountField object PdfPageNumberField pageNumberField = new PdfPageNumberField(); PdfPageCountField pageCountField = new PdfPageCountField(); // Create a PdfCompositeField object to combine page count field and page number field in a single field PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField); // Iterate through the pages in the document for (int i = 0; i < doc.Pages.Count; i++) { // Get a specific page PdfPageBase page = doc.Pages[i]; // Get the page size SizeF pageSize = doc.Pages[i].Size; // Draw a line at the specified position page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50); // Measure the size the "Page X of Y" SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count)); // Set the location of the composite field compositeField.Location = new PointF(pageSize.Width - pageNumberSize.Width - 72, pageSize.Height - 45); // Draw the composite field on the page compositeField.Draw(page.Canvas); } // Save to a different PDF file doc.SaveToFile("AddPageNumbersToRigthCorner.pdf"); // Dispose resources doc.Dispose(); } } }
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢慧都在線客服 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn