C#使⽤ITextSharp操作pdf
在中没有很好操作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; t; }
public float StartY { get; t; }
public float RectWidth { get; t; }
public float RectHeight { get; t; }
#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>
/// </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;
}
}
el
{
percentage = containerRect.RectWidth / width;
if (height * percentage > containerRect.RectHeight)
{
percentage = containerRect.RectHeight / height;
}
}
return percentage;
}
#endregion
}
}
CanvasRectangle.cs
PdfBa.cs主要继承PdfPageEventHelper,并实现IPdfPageEvent接⼝的具体实现,其实也是在pdf的加载,分页等事件中可以重写⼀些具体操作。
;
pdf;
using System;
using System.Collections.Generic;
using System.IO;学校励志标语
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PDFReport
{
public class PdfBa : PdfPageEventHelper
{
#region 属性
private String _fontFilePathForHeaderFooter = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SIMHEI.TTF"); /// <summary>
/// 页眉/页脚所⽤的字体
/// </summary>
public String FontFilePathForHeaderFooter
{
get
{
return _fontFilePathForHeaderFooter;
t
{
_fontFilePathForHeaderFooter = value;
}
}
private String _fontFilePathForBody = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SIMSUN.TTC,1"); /// <summary>
/// 正⽂内容所⽤的字体
/// </summary>
public String FontFilePathForBody
{
get { return _fontFilePathForBody; }
t { _fontFilePathForBody = value; }
}
private PdfPTable _header;
/// <summary>
/// 页眉
/// </summary>
public PdfPTable Header
{
get { return _header; }
private t { _header = value; }
}
private PdfPTable _footer;
/// <summary>
/// 页脚
/// </summary>
public PdfPTable Footer
{
get { return _footer; }
private t { _footer = value; }
}
private BaFont _baFontForHeaderFooter;
/// <summary>
/// 页眉页脚所⽤的字体
/// </summary>
public BaFont BaFontForHeaderFooter
{
get { return _baFontForHeaderFooter; }
t { _baFontForHeaderFooter = value; }
}
private BaFont _baFontForBody;
/// <summary>
/// 正⽂所⽤的字体
/// </summary>
public BaFont BaFontForBody
{
get { return _baFontForBody; }
t { _baFontForBody = value; }
}
private Document _document;
/
// <summary>
/// PDF的Document
/// </summary>
public Document Document
get { return _document; }
private t { _document = value; }
}
#endregion
public override void OnOpenDocument(PdfWriter writer, Document document)
{
try
{
BaFontForHeaderFooter = BaFont.CreateFont(FontFilePathForHeaderFooter, BaFont.IDENTITY_H, BaFont.NOT_EMBEDDED); BaFontForBody = BaFont.CreateFont(FontFilePathForBody, BaFont.IDENTITY_H, BaFont.NOT_EMBEDDED);
document.Add(new Phra("\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 pdf.PdfWriter writer)
{
return null;
}
参保证明
#endregion
#region GenerateFooter
/// <summary>
/// ⽣成页脚
/// </summary>
/// <param name="writer"></param>
/// <returns></returns>
public virtual PdfPTable pdf.PdfWriter writer)
{
return null;
}
#endregion
public override void pdf.PdfWriter writer, Document document)
{
ba.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); }
}
}
PdfBa.cs
PdfImage.cs对图像⽂件的操作
;
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; t; }
/// <summary>
/// 图⽚域宽
/// </summary>
public float FitWidth { get; t; }
/
// <summary>
/// 图⽚域⾼
/// </summary>
public float FitHeight { get; t; }
/// <summary>
/// 绝对X坐标
/// </summary>
public float AbsoluteX { get; t; }
鲨鱼的画法/// <summary>
/// 绝对Y坐标
/// </summary>
public float AbsoluteY { get; t; }
/// <summary>
/// Img内容
/// </summary>
public byte[] ImgBytes { get; t; }
/// <summary>
/// 是否缩放
/// </summary>
public bool ScaleParent { get; t; }
/// <summary>
/// 画布对象
/
// </summary>
public CanvasRectangle ContainerRect { get; t; }
#endregion
#region PdfImage构造