C#代码实现PDF文档操作类
程序员文章站
2023-02-21 15:07:51
本文纯干货,贴上pdf文档操作类c#代码,需要添加itextsharp.dll引用才可以正常通过编译。
废话不多说了,直接给大家贴代码了。
代码如下:
usi...
本文纯干货,贴上pdf文档操作类c#代码,需要添加itextsharp.dll引用才可以正常通过编译。
废话不多说了,直接给大家贴代码了。
代码如下:
using system.io; using itextsharp.text; using itextsharp.text.pdf; namespace dotnet.utilities { /// <summary> /// pdf文档操作类 /// </summary> //------------------------------------调用-------------------------------------------- //pdfoperation pdf = new pdfoperation(); //pdf.open(new filestream(path, filemode.create)); //pdf.setbasefont(@"c:\windows\fonts\simhei.ttf"); //pdf.addparagraph("测试文档(生成时间:" + datetime.now + ")", 15, 1, 20, 0, 0); //pdf.close(); //------------------------------------------------------------------------------------- public class pdfoperation { #region 构造函数 /// <summary> /// 构造函数 /// </summary> public pdfoperation() { rect = pagesize.a4; document = new document(rect); } /// <summary> /// 构造函数 /// </summary> /// <param name="type">页面大小(如"a4")</param> public pdfoperation(string type) { setpagesize(type); document = new document(rect); } /// <summary> /// 构造函数 /// </summary> /// <param name="type">页面大小(如"a4")</param> /// <param name="marginleft">内容距左边框距离</param> /// <param name="marginright">内容距右边框距离</param> /// <param name="margintop">内容距上边框距离</param> /// <param name="marginbottom">内容距下边框距离</param> public pdfoperation(string type, float marginleft, float marginright, float margintop, float marginbottom) { setpagesize(type); document = new document(rect, marginleft, marginright, margintop, marginbottom); } #endregion #region 私有字段 private font font; private rectangle rect; //文档大小 private document document;//文档对象 private basefont basefont;//字体 #endregion #region 设置字体 /// <summary> /// 设置字体 /// </summary> public void setbasefont(string path) { basefont = basefont.createfont(path, basefont.identity_h, basefont.not_embedded); } /// <summary> /// 设置字体 /// </summary> /// <param name="size">字体大小</param> public void setfont(float size) { font = new font(basefont, size); } #endregion #region 设置页面大小 /// <summary> /// 设置页面大小 /// </summary> /// <param name="type">页面大小(如"a4")</param> public void setpagesize(string type) { switch (type.trim()) { case "a4": rect = pagesize.a4; break; case "a8": rect = pagesize.a8; break; } } #endregion #region 实例化文档 /// <summary> /// 实例化文档 /// </summary> /// <param name="os">文档相关信息(如路径,打开方式等)</param> public void getinstance(stream os) { pdfwriter.getinstance(document, os); } #endregion #region 打开文档对象 /// <summary> /// 打开文档对象 /// </summary> /// <param name="os">文档相关信息(如路径,打开方式等)</param> public void open(stream os) { getinstance(os); document.open(); } #endregion #region 关闭打开的文档 /// <summary> /// 关闭打开的文档 /// </summary> public void close() { document.close(); } #endregion #region 添加段落 /// <summary> /// 添加段落 /// </summary> /// <param name="content">内容</param> /// <param name="fontsize">字体大小</param> public void addparagraph(string content, float fontsize) { setfont(fontsize); paragraph pra = new paragraph(content, font); document.add(pra); } /// <summary> /// 添加段落 /// </summary> /// <param name="content">内容</param> /// <param name="fontsize">字体大小</param> /// <param name="alignment">对齐方式(1为居中,0为居左,2为居右)</param> /// <param name="spacingafter">段后空行数(0为默认值)</param> /// <param name="spacingbefore">段前空行数(0为默认值)</param> /// <param name="multipliedleading">行间距(0为默认值)</param> public void addparagraph(string content, float fontsize, int alignment, float spacingafter, float spacingbefore, float multipliedleading) { setfont(fontsize); paragraph pra = new paragraph(content, font); pra.alignment = alignment; if (spacingafter != 0) { pra.spacingafter = spacingafter; } if (spacingbefore != 0) { pra.spacingbefore = spacingbefore; } if (multipliedleading != 0) { pra.multipliedleading = multipliedleading; } document.add(pra); } #endregion #region 添加图片 /// <summary> /// 添加图片 /// </summary> /// <param name="path">图片路径</param> /// <param name="alignment">对齐方式(1为居中,0为居左,2为居右)</param> /// <param name="newwidth">图片宽(0为默认值,如果宽度大于页宽将按比率缩放)</param> /// <param name="newheight">图片高</param> public void addimage(string path, int alignment, float newwidth, float newheight) { image img = image.getinstance(path); img.alignment = alignment; if (newwidth != 0) { img.scaleabsolute(newwidth, newheight); } else { if (img.width > pagesize.a4.width) { img.scaleabsolute(rect.width, img.width * img.height / rect.height); } } document.add(img); } #endregion #region 添加链接、点 /// <summary> /// 添加链接 /// </summary> /// <param name="content">链接文字</param> /// <param name="fontsize">字体大小</param> /// <param name="reference">链接地址</param> public void addanchorreference(string content, float fontsize, string reference) { setfont(fontsize); anchor auc = new anchor(content, font); auc.reference = reference; document.add(auc); } /// <summary> /// 添加链接点 /// </summary> /// <param name="content">链接文字</param> /// <param name="fontsize">字体大小</param> /// <param name="name">链接点名</param> public void addanchorname(string content, float fontsize, string name) { setfont(fontsize); anchor auc = new anchor(content, font); auc.name = name; document.add(auc); } #endregion } }
友情提醒需要注意点:需要添加itextsharp.dll引用才可以正常通过编译。