C# 添加PDF頁(yè)眉/頁(yè)腳的示例代碼
概述
頁(yè)眉頁(yè)腳是一篇完整、精致的文檔的重要組成部分。在頁(yè)眉頁(yè)腳處,可以呈現(xiàn)的內(nèi)容很多,如公司名稱、頁(yè)碼、工作表名、日期、圖片,如LOGO、標(biāo)記等。在下面的文章中,將介紹如何在PDF中添加頁(yè)眉頁(yè)腳。通過(guò)代碼測(cè)試,添加頁(yè)眉頁(yè)腳可以分兩種情況來(lái)實(shí)現(xiàn)效果:
1.通過(guò)添加新的一頁(yè),在新建的頁(yè)面上來(lái)添加頁(yè)眉頁(yè)腳
2.通過(guò)給現(xiàn)有文檔直接添加頁(yè)眉頁(yè)腳
下面將根據(jù)這兩種情況介紹具體的C#代碼操作
使用工具
Free Spire.PDF for .NET 4.3(社區(qū)版)
示例代碼(供參考)
1.新建一頁(yè)來(lái)添加頁(yè)眉頁(yè)腳
1.1 添加頁(yè)眉
【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ì)象,并添加一頁(yè)
      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()方法添加頁(yè)眉
      AddHeader(pdf, PdfPageSize.A4, margin);
      //保存并打開文檔
      pdf.SaveToFile("PDF頁(yè)眉.pdf");
      System.Diagnostics.Process.Start("PDF頁(yè)眉.pdf");
    }
    static void AddHeader(PdfDocument doc, SizeF pageSize, PdfMargins margin)
    {
      //初始化一個(gè)PdfPageTemplateElement對(duì)象,用于創(chuàng)建頁(yè)眉
      PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
      headerSpace.Foreground = true;
      doc.Template.Top = headerSpace;
      //在頁(yè)眉部分繪入文字
      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);
      //在頁(yè)眉部分繪入圖片
      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);
    }
  }
}
頁(yè)眉添加效果:
1.2添加頁(yè)腳
【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ì)象,添加一頁(yè)
      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()方法添加頁(yè)腳
      AddFooter(doc, PdfPageSize.A4, margin);
      //調(diào)用AddPageNumber()方法添加頁(yè)碼
      AddPageNumber(doc, margin);
      //保存并打開文檔
      doc.SaveToFile("PDF頁(yè)腳.pdf");
      System.Diagnostics.Process.Start("PDF頁(yè)腳.pdf");
    }
    static void AddFooter(PdfDocument doc, SizeF pageSize, PdfMargins margin)
    {
      //初始化一個(gè)PdfPageTemplateElement對(duì)象,用于創(chuàng)建頁(yè)腳
      PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
      footerSpace.Foreground = true;
      doc.Template.Bottom = footerSpace;
      //在頁(yè)腳部分繪入文字
      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)
    {
      //添加頁(yè)碼到頁(yè)腳部分
      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);
      }
    }
  }
}
頁(yè)腳添加效果:
2.給現(xiàn)有PDF文檔添加頁(yè)眉頁(yè)腳
【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)有文檔添加頁(yè)眉
      DrawHeader(existingPdf);
      //調(diào)用DrawFooter方法在現(xiàn)有文檔添加頁(yè)腳
      DrawFooter(existingPdf);
      //保存并打開文檔
      existingPdf.SaveToFile("output.pdf");
      System.Diagnostics.Process.Start("output.pdf");
    }
    //在頁(yè)面上方空白部位繪制頁(yè)眉
    static void DrawHeader(PdfDocument doc)
    {
      //獲取頁(yè)面大小
      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++)
      {
        //在每一頁(yè)的指定位置繪制圖片
        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);
        //在每一頁(yè)的指定位置繪制橫線
        PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
        doc.Pages[i].Canvas.DrawLine(pen, x, y + height + 2, pageSize.Width - x, y + height + 2);
      }
    }
    //在頁(yè)面下方空白部位繪制頁(yè)腳
    static void DrawFooter(PdfDocument doc)
    {
      //獲取頁(yè)面大小
      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++)
      {
        //在每一頁(yè)的指定位置繪制橫線
        PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
        doc.Pages[i].Canvas.DrawLine(pen, x, y, pageSize.Width - x, y);
        //在每一頁(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);         
        //在每一頁(yè)的指定位置當(dāng)前頁(yè)碼和總頁(yè)碼
        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è)試效果:
注意事項(xiàng)
安裝之后,添加引用Spire.PDF.dll文件到項(xiàng)目程序即可,dll文件可在安裝路徑下的Bin文件夾中獲取。
以上是本次關(guān)于C#添加PDF頁(yè)眉頁(yè)腳的全部?jī)?nèi)容,兩種情況中的示例方法,可以選擇參考使用。
上一篇:C#中緩存的基本使用方法
欄 目:C#教程
下一篇:C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別
本文標(biāo)題:C# 添加PDF頁(yè)眉/頁(yè)腳的示例代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5098.html
您可能感興趣的文章
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
 - 01-10C#中DataGridView動(dòng)態(tài)添加行及添加列的方法
 - 01-10C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
 - 01-10C#代碼實(shí)現(xiàn)PDF文檔操作類
 - 01-10C#編程實(shí)現(xiàn)DataTable添加行的方法
 - 01-10C#編程實(shí)現(xiàn)發(fā)送郵件的方法(可添加附件)
 - 01-10C#給圖片添加水印完整實(shí)例
 - 01-10C#怎么給PDF添加背景圖片
 - 01-10C#實(shí)現(xiàn)PDF文件添加圖片背景
 - 01-10C#圖片添加水印的實(shí)現(xiàn)代碼
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10delphi制作wav文件的方法
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 04-02jquery與jsp,用jquery
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10C#中split用法實(shí)例總結(jié)
 


