欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年08月31日 09:09:25   作者:Miaonly  
這篇文章主要介紹了C# 添加PDF頁眉/頁腳的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

概述

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

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

2.通過給現(xiàn)有文檔直接添加頁眉頁腳

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

使用工具

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

示例代碼(供參考)

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();

      //設(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)
    {
      //初始化一個PdfPageTemplateElement對象,用于創(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);

    }
  }
}

頁眉添加效果:

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();

      //設(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)
    {
      //初始化一個PdfPageTemplateElement對象,用于創(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);
      }

    }
  }
}

頁腳添加效果:

2.給現(xiàn)有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");

      //調(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兩個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);         
        //在每一頁的指定位置當(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);
      }
    }
  }
}

測試效果:

注意事項

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

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

相關(guān)文章

  • c#通用登錄模塊分享

    c#通用登錄模塊分享

    這是一款簡單的ASP.NETC#注冊登錄模塊制作思路,非常簡單實用,雖然沒怎么考慮登陸的安全性,但作為C#的朋友學(xué)習(xí)交流使用。
    2016-07-07
  • c#定期刪除文件的實操方法

    c#定期刪除文件的實操方法

    在本篇文章里小編給大家分享了關(guān)于c#定期刪除文件的方法和步驟,有需要的朋友們可以學(xué)習(xí)下。
    2019-02-02
  • C# Entity Framework中的IQueryable和IQueryProvider詳解

    C# Entity Framework中的IQueryable和IQueryProvider詳解

    這篇文章主要介紹了C# Entity Framework中的IQueryable和IQueryProvider詳解,本文使用實例分析這兩個接口的內(nèi)部實現(xiàn),需要的朋友可以參考下
    2015-01-01
  • C#?函數(shù)返回多個值的方法詳情

    C#?函數(shù)返回多個值的方法詳情

    這篇文章主要介紹了C#函數(shù)返回多個值的方法詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • winform實現(xiàn)可拖動的自定義Label控件

    winform實現(xiàn)可拖動的自定義Label控件

    這篇文章主要為大家詳細(xì)介紹了winform實現(xiàn)可拖動的自定義Label控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法

    C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法

    這篇文章主要介紹了C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法,結(jié)合實例形式簡單分析了C#字符數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)字符數(shù)組的具體實現(xiàn)技巧,需要的朋友可以參考下
    2017-02-02
  • C#實現(xiàn)動態(tài)創(chuàng)建接口并調(diào)用的實例

    C#實現(xiàn)動態(tài)創(chuàng)建接口并調(diào)用的實例

    這篇文章介紹了C#實現(xiàn)動態(tài)創(chuàng)建接口并調(diào)用,文中通過實例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • C#利用異或算法實現(xiàn)加密解密

    C#利用異或算法實現(xiàn)加密解密

    這篇文章主要為大家詳細(xì)介紹了C#如何利用異或算法實現(xiàn)加密解密的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-01-01
  • C#實現(xiàn)影院售票系統(tǒng)

    C#實現(xiàn)影院售票系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)影院售票系統(tǒng),解析了售票系統(tǒng)的難點,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • C#中四種定時器的用法詳解

    C#中四種定時器的用法詳解

    日常項目開發(fā)中,很多時候都需要用到定時器來處理一些問題,那么c#中各種定時器應(yīng)該怎么用呢?下面來簡單介紹下C#中4種定時器的使用方法說明,感興趣的朋友可以參考下
    2024-04-04

最新評論