C#使用iTextSharp封装的PDF文件操作类实例
程序员文章站
2023-12-12 15:15:10
本文实例讲述了c#使用itextsharp封装的pdf文件操作类。分享给大家供大家参考。具体分析如下:
这个c#代码主要讲itextsharp中用于操作pdf文件的方法进...
本文实例讲述了c#使用itextsharp封装的pdf文件操作类。分享给大家供大家参考。具体分析如下:
这个c#代码主要讲itextsharp中用于操作pdf文件的方法进行了再次封装,可以更加方便的访问pdf文档,可以动态生成pdf文件、添加内容、设置段落、设置字体等。
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 } }
希望本文所述对大家的c#程序设计有所帮助。