小編給大家分享一下C#在PDF文檔中如何插入頁眉頁腳,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
專注于為中小企業(yè)提供成都網(wǎng)站制作、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)普蘭店免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
【C#】
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System;
namespace AddHeader_PDF
{
class Program
{
static void Main(string[] args)
{
//新建一個(gè)PdfDocument類對(duì)象,并添加一頁
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
//設(shè)置margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
//調(diào)用AddHeader()方法添加頁眉
AddHeader(pdf, PdfPageSize.A4, margin);
//保存并打開文檔
pdf.SaveToFile("PDF頁眉.pdf");
System.Diagnostics.Process.Start("PDF頁眉.pdf");
}
static void AddHeader(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
//初始化一個(gè)PdfPageTemplateElement對(duì)象,用于創(chuàng)建頁眉
PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
headerSpace.Foreground = true;
doc.Template.Top = headerSpace;
//在頁眉部分繪入文字
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
String headerText = "WORLD TRADE ORGANIZATION, WTO \n THE INTERNATIONAL ORGANIZATION THAT REGULATES INTERNATIONAL TRADE";
float x = PdfPageSize.A4.Width;
float y = 0;
headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format);
//在頁眉部分繪入圖片
PdfImage headerImage = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\1.png");
float width = headerImage.Width / 2;
float height = headerImage.Height / 3;
headerSpace.Graphics.DrawImage(headerImage, 0, 0, width, height);
}
}
}頁眉添加效果:
【C#】
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System;
using Spire.Pdf.AutomaticFields;
namespace AddFooter_PDF
{
class Program
{
static void Main(string[] args)
{
//新建一個(gè)PdfDocument類對(duì)象,添加一頁
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//設(shè)置margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
//調(diào)用AddFooter()方法添加頁腳
AddFooter(doc, PdfPageSize.A4, margin);
//調(diào)用AddPageNumber()方法添加頁碼
AddPageNumber(doc, margin);
//保存并打開文檔
doc.SaveToFile("PDF頁腳.pdf");
System.Diagnostics.Process.Start("PDF頁腳.pdf");
}
static void AddFooter(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
//初始化一個(gè)PdfPageTemplateElement對(duì)象,用于創(chuàng)建頁腳
PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
footerSpace.Foreground = true;
doc.Template.Bottom = footerSpace;
//在頁腳部分繪入文字
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
String headerText = "Website : www.wto.org";
float x = PdfPageSize.A4.Width / 2;
float y = 0;
footerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format);
}
static void AddPageNumber(PdfDocument doc, PdfMargins margin)
{
//添加頁碼到頁腳部分
foreach (PdfPageBase page in doc.Pages)
{
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Left);
int x1 = Convert.ToInt32(page.Canvas.ClientSize.Width / 2);
int y1 = Convert.ToInt32(page.Canvas.ClientSize.Height - margin.Bottom + 20);
Rectangle bounds = new Rectangle(x1, y1, 20, 20);
PdfPageNumberField field = new PdfPageNumberField();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true);
field.Font = font;
field.StringFormat = format1;
field.Brush = PdfBrushes.Black;
field.Bounds = bounds;
field.Draw(page.Canvas);
}
}
}
}頁腳添加效果:
【C#】
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
namespace PdfHeader
{
class Program
{
static void Main(string[] args)
{
//加載一個(gè)測(cè)試文檔
PdfDocument existingPdf = new PdfDocument();
existingPdf.LoadFromFile("Test.pdf");
//調(diào)用DrawHeader方法在現(xiàn)有文檔添加頁眉
DrawHeader(existingPdf);
//調(diào)用DrawFooter方法在現(xiàn)有文檔添加頁腳
DrawFooter(existingPdf);
//保存并打開文檔
existingPdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");
}
//在頁面上方空白部位繪制頁眉
static void DrawHeader(PdfDocument doc)
{
//獲取頁面大小
SizeF pageSize = doc.Pages[0].Size;
//聲明x,y兩個(gè)float型變量
float x = 90;
float y = 20;
for (int i = 0; i < doc.Pages.Count; i++)
{
//在每一頁的指定位置繪制圖片
PdfImage headerImage = PdfImage.FromFile("logo.png");
float width = headerImage.Width / 7;
float height = headerImage.Height / 7;
doc.Pages[i].Canvas.DrawImage(headerImage, x, y, width, height);
//在每一頁的指定位置繪制橫線
PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
doc.Pages[i].Canvas.DrawLine(pen, x, y + height + 2, pageSize.Width - x, y + height + 2);
}
}
//在頁面下方空白部位繪制頁腳
static void DrawFooter(PdfDocument doc)
{
//獲取頁面大小
SizeF pageSize = doc.Pages[0].Size;
//聲明x,y兩個(gè)float型變量
float x = 90;
float y = pageSize.Height - 72;
for (int i = 0; i < doc.Pages.Count; i++)
{
//在每一頁的指定位置繪制橫線
PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
doc.Pages[i].Canvas.DrawLine(pen, x, y, pageSize.Width - x, y);
//在每一頁的指定位置繪制文字
y = y + 5;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("黑體", 10f, FontStyle.Bold), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
String footerText = " Website\n https://g20.org/";
doc.Pages[i].Canvas.DrawString(footerText, font, PdfBrushes.Black, x, y, format);
//在每一頁的指定位置當(dāng)前頁碼和總頁碼
PdfPageNumberField number = new PdfPageNumberField();
PdfPageCountField count = new PdfPageCountField();
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "{0}/{1}", number, count);
compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
SizeF size = font.MeasureString(compositeField.Text);
compositeField.Bounds = new RectangleF(pageSize.Width - x - size.Width, y, size.Width, size.Height);
compositeField.Draw(doc.Pages[i].Canvas);
}
}
}
}測(cè)試效果:
以上是“C#在PDF文檔中如何插入頁眉頁腳”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱:C#在PDF文檔中如何插入頁眉頁腳
路徑分享:http://www.chinadenli.net/article2/gisdoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、品牌網(wǎng)站制作、定制開發(fā)、小程序開發(fā)、靜態(tài)網(wǎng)站、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)