温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#如何添加PDF页眉/页脚

发布时间:2021-05-25 10:17:47 来源:亿速云 阅读:169 作者:小新 栏目:编程语言

这篇文章主要介绍C#如何添加PDF页眉/页脚,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

概述

页眉页脚是一篇完整、精致的文档的重要组成部分。在页眉页脚处,可以呈现的内容很多,如公司名称、页码、工作表名、日期、图片,如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#是什么

C#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的首选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。

以上是“C#如何添加PDF页眉/页脚”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pdf
AI