原創(chuàng)|行業(yè)資訊|編輯:胡濤|2024-12-18 11:05:39.450|閱讀 81 次
概述:在本文中,您將學(xué)習(xí)如何使用Spire.PDF for .NET在 C# 中向 PDF 文檔添加頁碼。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在 PDF 文檔中添加頁碼不僅實(shí)用,而且美觀,因?yàn)樗峁┝祟愃朴趯I(yè)出版材料的精美外觀。無論您處理的是小說、報(bào)告還是任何其他類型的長(zhǎng)文檔的數(shù)字副本,添加頁碼都可以顯著提高其可讀性和實(shí)用性。在本文中,您將學(xué)習(xí)如何使用Spire.PDF for .NET在 C# 中向 PDF 文檔添加頁碼。
Spire.PDF for .NET 是一款獨(dú)立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫(kù),開發(fā)人員可以新建一個(gè) PDF 文檔或者對(duì)現(xiàn)有的 PDF 文檔進(jìn)行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
首先,您需要將 Spire.PDF for.NET 包中包含的 DLL 文件作為引用添加到您的 .NET 項(xiàng)目中。 可以從此鏈接下載 DLL 文件,也可以通過NuGet安裝。
PM> Install-Package Spire.PDF
當(dāng)使用 Spire.PDF for .NET 操作現(xiàn)有的 PDF 文檔時(shí),需要注意的是坐標(biāo)系的原點(diǎn)位于頁面的左上角。x 軸向右延伸,y 軸向下延伸。
通常,頁碼位于文檔的頁眉或頁腳部分。因此,在確定頁碼的適當(dāng)位置時(shí),考慮頁面大小和頁邊距至關(guān)重要。
在 Spire.PDF for .NET 庫(kù)中,有兩個(gè)可用的類:PdfPageNumberField和PdfPageCountField。這些類允許您在將當(dāng)前頁碼和總頁數(shù)添加到 PDF 文檔的頁面時(shí)檢索和顯示它們。如果您希望插入諸如“第 X 頁”或“第 X 頁,共 Y 頁”之類的文本,則可以使用PdfCompositeField類,該類使您能夠?qū)⑺栉谋九c一個(gè)或多個(gè)字段組合成單個(gè)字段。以下是使用 C# 在 PDF 頁腳中添加左對(duì)齊頁碼的步驟。
【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(); } } }
為了將頁腳部分的頁碼與中心對(duì)齊,動(dòng)態(tài)計(jì)算文本“第 X 頁,共 Y 頁”的寬度至關(guān)重要。此計(jì)算至關(guān)重要,因?yàn)樗鼪Q定了頁碼 ( PdfCompositeField ) 的 X 坐標(biāo)。要實(shí)現(xiàn)居中對(duì)齊,X 坐標(biāo)的計(jì)算方法是從頁面寬度中減去頁碼的寬度,然后將結(jié)果除以 2,如下所示:(PageWidth - PageNumberWidth)/2。
以下是使用 C# 在 PDF 頁腳中添加居中對(duì)齊頁碼的步驟。
【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(); } } }
要將頁碼定位在頁腳部分的右上角,還必須動(dòng)態(tài)計(jì)算文本“第 X 頁,共 Y 頁”的寬度。因?yàn)轫摯a ( PdfCompositeField ) 的 X 坐標(biāo)是通過從頁面寬度中減去頁碼和右頁邊距的組合寬度來確定的,如下所示:PageWidth - (PageNumberWidth + RightPageMargin)。
以下是使用 C# 在 PDF 頁腳中添加右對(duì)齊頁碼的步驟。
【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(); } } }
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢慧都在線客服 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn