在.NET中没有很好操作pdf的类库,如果你需要对pdf进行编辑,加密,模板打印等等都可以选择使用ITextSharp来实现。
第一步:可以点击这里下载,新版本的插件升级和之前对比主要做了这几项重大改变
1.初始化对汉字的支持
2.对页眉页脚的加载形式
第二步:制作pdf模板
可以下载Adobe Acrobat DC等任意一款pdf编辑工具,视图——工具——准备表单,可以在需要赋值的地方放上一个文本框,可以把名称修改为有意义的名称,后面在赋值时要用到。
第三步:建项目引入各个操作类
介于前段时间项目所需特意把ITextSharp进行了二次封装,使我们对pdf操作起来更加方便。
列一下各文件的作用:
CanvasRectangle.cs对Rectangle对象的基类支持,可以灵活定义一个Rectangle。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PDFReport
{
/// <summary>
/// 画布对象
/// </summary>
public class CanvasRectangle
{
#region CanvasRectangle属性
public float StartX { get; set; }
public float StartY { get; set; }
public float RectWidth { get; set; }
public float RectHeight { get; set; }
#endregion
#region 初始化Rectangle
/// <summary>
/// 提供rectangle信息
/// </summary>
/// <param name="startX">起点X坐标</param>
/// <param name="startY">起点Y坐标</param>
/// <param name="rectWidth">指定rectangle宽</param>
/// <param name="rectHeight">指定rectangle高</param>
public CanvasRectangle(float startX, float startY, float rectWidth, float rectHeight)
{
this.StartX = startX;
this.StartY = startY;
this.RectWidth = rectWidth;
this.RectHeight = rectHeight;
}
#endregion
#region 获取图形缩放百分比
/// <summary>
/// 获取指定宽高压缩后的百分比
/// </summary>
/// <param name="width">目标宽</param>
/// <param name="height">目标高</param>
/// <param name="containerRect">原始对象</param>
/// <returns>目标与原始对象百分比</returns>
public static float GetPercentage(float width, float height, CanvasRectangle containerRect)
{
float percentage = 0;
if (height > width)
{
percentage = containerRect.RectHeight / height;
if (width * percentage > containerRect.RectWidth)
{
percentage = containerRect.RectWidth / width;
}
}
else
{
percentage = containerRect.RectWidth / width;
if (height * percentage > containerRect.RectHeight)
{
percentage = containerRect.RectHeight / height;
}
}
return percentage;
}
#endregion
}
}
CanvasRectangle.cs
PdfBase.cs主要继承PdfPageEventHelper,并实现IPdfPageEvent接口的具体实现,其实也是在pdf的加载,分页等事件中可以重写一些具体操作。
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PDFReport
{
public class PdfBase : PdfPageEventHelper
{
#region 属性
private String _fontFilePathForHeaderFooter = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SIMHEI.TTF");
/// <summary>
/// 页眉/页脚所用的字体
/// </summary>
public String FontFilePathForHeaderFooter
{
get
{
return _fontFilePathForHeaderFooter;
}
set
{
_fontFilePathForHeaderFooter = value;
}
}
private String _fontFilePathForBody = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SIMSUN.TTC,1");
/// <summary>
/// 正文内容所用的字体
/// </summary>
public String FontFilePathForBody
{
get { return _fontFilePathForBody; }
set { _fontFilePathForBody = value; }
}
private PdfPTable _header;
/// <summary>
/// 页眉
/// </summary>
public PdfPTable Header
{
get { return _header; }
private set { _header = value; }
}
private PdfPTable _footer;
/// <summary>
/// 页脚
/// </summary>
public PdfPTable Footer
{
get { return _footer; }
private set { _footer = value; }
}
private BaseFont _baseFontForHeaderFooter;
/// <summary>
/// 页眉页脚所用的字体
/// </summary>
public BaseFont BaseFontForHeaderFooter
{
get { return _baseFontForHeaderFooter; }
set { _baseFontForHeaderFooter = value; }
}
private BaseFont _baseFontForBody;
/// <summary>
/// 正文所用的字体
/// </summary>
public BaseFont BaseFontForBody
{
get { return _baseFontForBody; }
set { _baseFontForBody = value; }
}
private Document _document;
/// <summary>
/// PDF的Document
/// </summary>
public Document Document
{
get { return _document; }
private set { _document = value; }
}
#endregion
public override void OnOpenDocument(PdfWriter writer, Document document)
{
try
{
BaseFontForHeaderFooter = BaseFont.CreateFont(FontFilePathForHeaderFooter, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFontForBody = BaseFont.CreateFont(FontFilePathForBody, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
document.Add(new Phrase("\n\n"));
Document = document;
}
catch (DocumentException de)
{
}
catch (System.IO.IOException ioe)
{
}
}
#region GenerateHeader
/// <summary>
/// 生成页眉
/// </summary>
/// <param name="writer"></param>
/// <returns></returns>
public virtual PdfPTable GenerateHeader(iTextSharp.text.pdf.PdfWriter writer)
{
return null;
}
#endregion
#region GenerateFooter
/// <summary>
/// 生成页脚
/// </summary>
/// <param name="writer"></param>
/// <returns></returns>
public virtual PdfPTable GenerateFooter(iTextSharp.text.pdf.PdfWriter writer)
{
return null;
}
#endregion
public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
base.OnEndPage(writer, document);
//输出页眉
Header = GenerateHeader(writer);
Header.TotalWidth = document.PageSize.Width - 20f;
///调用PdfTable的WriteSelectedRows方法。该方法以第一个参数作为开始行写入。
///第二个参数-1表示没有结束行,并且包含所写的所有行。
///第三个参数和第四个参数是开始写入的坐标x和y.
Header.WriteSelectedRows(0, -1, 10, document.PageSize.Height - 20, writer.DirectContent);
//输出页脚
Footer = GenerateFooter(writer);
Footer.TotalWidth = document.PageSize.Width - 20f;
Footer.WriteSelectedRows(0, -1, 10, document.PageSize.GetBottom(50), writer.DirectContent);
}
}
}
PdfBase.cs
PdfImage.cs对图像文件的操作
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PDFReport
{
/// <summary>
/// pdf图片操作类
/// </summary>
public class PdfImage
{
#region PdfImage属性
/// <summary>
/// 图片URL地址
/// </summary>
public string ImageUrl { get; set; }
/// <summary>
/// 图片域宽
/// </summary>
public float FitWidth { get; set; }
/// <summary>
/// 图片域高
/// </summary>
public float FitHeight { get; set; }
/// <summary>
/// 绝对X坐标
/// </summary>
public float AbsoluteX { get; set; }
/// <summary>
/// 绝对Y坐标
/// </summary>
public float AbsoluteY { get; set; }
/// <summary>
/// Img内容
/// </summary>
public byte[] ImgBytes { get; set; }
/// <summary>
/// 是否缩放
/// </summary>
public bool ScaleParent { get; set; }
/// <summary>
/// 画布对象
/// </summary>
public CanvasRectangle ContainerRect { get; set; }
#endregion
#region PdfImage构造
/// <summary>
/// 网络图片写入
/// </summary>
/// <param name="imageUrl">图片URL地址</param>
/// <param name="fitWidth"></param>
/// <param name="fitHeight"></param>
/// <param name="absolutX"></param>
/// <param name="absoluteY"></param>
/// <param name="scaleParent"></param>
public PdfImage(string imageUrl, float fitWidth, float fitHeight, float absolutX, float absoluteY, bool scaleParent)
{
this.ImageUrl = imageUrl;
this.FitWidth = fitWidth;
this.FitHeight = fitHeight;
this.AbsoluteX = absolutX;
this.AbsoluteY = absoluteY;
this.ScaleParent = scaleParent;
}
/// <summary>
/// 本地文件写入
/// </summary>