C#如何添加PDF頁眉/頁腳-創(chuàng)新互聯(lián)

這篇文章主要介紹C#如何添加PDF頁眉/頁腳,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元雞澤做網(wǎng)站,已為上家服務(wù),為雞澤各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108

概述

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

    }
  }
}

頁眉添加效果:

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

      //設(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);
      }

    }
  }
}

頁腳添加效果:

C#如何添加PDF頁眉/頁腳

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

測試效果:

C#如何添加PDF頁眉/頁腳

注意事項(xiàng)

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

C#是什么

C#是一個簡單、通用、面向?qū)ο蟮木幊陶Z言,它由微軟Microsoft開發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡單的可視化操作和C++的高運(yùn)行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語法風(fēng)格、創(chuàng)新的語言特性和便捷的面向組件編程從而成為.NET開發(fā)的選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因?yàn)镃#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。

以上是“C#如何添加PDF頁眉/頁腳”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前名稱:C#如何添加PDF頁眉/頁腳-創(chuàng)新互聯(lián)
文章路徑:http://bm7419.com/article40/cdecho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、靜態(tài)網(wǎng)站網(wǎng)站改版、服務(wù)器托管、網(wǎng)站營銷手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作