a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C# 添加PDF頁眉/頁腳的示例代碼

2019-10-29 19:59:30
字體:
來源:轉載
供稿:網友

概述

頁眉頁腳是一篇完整、精致的文檔的重要組成部分。在頁眉頁腳處,可以呈現的內容很多,如公司名稱、頁碼、工作表名、日期、圖片,如LOGO、標記等。在下面的文章中,將介紹如何在PDF中添加頁眉頁腳。通過代碼測試,添加頁眉頁腳可以分兩種情況來實現效果:

1.通過添加新的一頁,在新建的頁面上來添加頁眉頁腳

2.通過給現有文檔直接添加頁眉頁腳

下面將根據這兩種情況介紹具體的C#代碼操作

使用工具

Free Spire.PDF for .NET 4.3(社區版)

示例代碼(供參考)

1.新建一頁來添加頁眉頁腳

1.1 添加頁眉

【C#】

using Spire.Pdf;using Spire.Pdf.Graphics;using System.Drawing;using System;namespace AddHeader_PDF{  class Program  {    static void Main(string[] args)    {      //新建一個PdfDocument類對象,并添加一頁      PdfDocument pdf = new PdfDocument();      PdfPageBase page = pdf.Pages.Add();      //設置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;      //調用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)    {      //初始化一個PdfPageTemplateElement對象,用于創建頁眉      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#,PDF,頁眉,頁腳,代碼

1.2添加頁腳

【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)    {      //新建一個PdfDocument類對象,添加一頁      PdfDocument doc = new PdfDocument();      PdfPageBase page = doc.Pages.Add();      //設置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;      //調用AddFooter()方法添加頁腳      AddFooter(doc, PdfPageSize.A4, margin);      //調用AddPageNumber()方法添加頁碼      AddPageNumber(doc, margin);      //保存并打開文檔      doc.SaveToFile("PDF頁腳.pdf");      System.Diagnostics.Process.Start("PDF頁腳.pdf");    }    static void AddFooter(PdfDocument doc, SizeF pageSize, PdfMargins margin)    {      //初始化一個PdfPageTemplateElement對象,用于創建頁腳      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#,PDF,頁眉,頁腳,代碼

2.給現有PDF文檔添加頁眉頁腳

【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)    {      //加載一個測試文檔      PdfDocument existingPdf = new PdfDocument();      existingPdf.LoadFromFile("Test.pdf");      //調用DrawHeader方法在現有文檔添加頁眉      DrawHeader(existingPdf);      //調用DrawFooter方法在現有文檔添加頁腳      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兩個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兩個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);                 //在每一頁的指定位置當前頁碼和總頁碼        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#,PDF,頁眉,頁腳,代碼

注意事項

安裝之后,添加引用Spire.PDF.dll文件到項目程序即可,dll文件可在安裝路徑下的Bin文件夾中獲取。

以上是本次關于C#添加PDF頁眉頁腳的全部內容,兩種情況中的示例方法,可以選擇參考使用。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊人国产精品 | 日韩三区| 国产乱精品一区二区三区视频了 | 99看片| 免费成人小视频 | 九九九久久国产免费 | 精品国产免费久久久久久尖叫 | 国产乱码精品一区二区三区中文 | 一区二区三区高清不卡 | 日韩av片免费看 | 欧美中文字幕在线 | 中文字幕亚洲欧美日韩在线不卡 | 亚洲国产日韩欧美 | 成人教育av | 国产一级片一区二区三区 | www国产精品| 噜噜噜天天躁狠狠躁夜夜精品 | 亚洲日韩欧美一区二区在线 | 日韩在线www| 日韩在线影视 | 91在线精品一区二区 | 日韩2区| 欧美视频网站 | 在线免费视频一区 | 中文字幕一区二区三区四区不卡 | 在线免费av观看 | 国产成人综合av | 久久在线| 国产精品爽 | 精品国产91久久久久久久 | 国内精品久久精品 | 日韩一区二区三区四区五区六区 | 爱爱视频在线观看 | 91精品一区二区三区久久久久久 | 久久精品一区二区三区四区 | 在线欧美日韩 | 亚洲一区二区精品视频 | 黄色免费影视 | 91精品久久久久久久久久入口 | 精品国产乱码久久久久久1区2区 | 成人精品视频99在线观看免费 |