Csharp/C#教程:C#使用iTextSharp封装的PDF文件操作类实例分享

本文实例讲述了C#使用iTextSharp封装的PDF文件操作类。分享给大家供大家参考。具体分析如下:

这个C#代码主要讲iTextSharp中用于操作PDF文件的方法进行了再次封装,可以更加方便的访问PDF文档,可以动态生成PDF文件、添加内容、设置段落、设置字体等。

usingSystem.IO; usingiTextSharp.text; usingiTextSharp.text.pdf; namespaceDotNet.Utilities { ///<summary> ///PDF文档操作类 ///</summary> //------------------调用-------------------------- //PDFOperationpdf=newPDFOperation(); //pdf.Open(newFileStream(path,FileMode.Create)); //pdf.SetBaseFont(@"C:WindowsFontsSIMHEI.TTF"); //pdf.AddParagraph("测试文档(生成时间:"+DateTime.Now+")",15,1,20,0,0); //pdf.Close(); //------------------------------- publicclassPDFOperation { #region构造函数 ///<summary> ///构造函数 ///</summary> publicPDFOperation() { rect=PageSize.A4; document=newDocument(rect); } ///<summary> ///构造函数 ///</summary> ///<paramname="type">页面大小(如"A4")</param> publicPDFOperation(stringtype) { SetPageSize(type); document=newDocument(rect); } ///<summary> ///构造函数 ///</summary> ///<paramname="type">页面大小(如"A4")</param> ///<paramname="marginLeft">内容距左边框距离</param> ///<paramname="marginRight">内容距右边框距离</param> ///<paramname="marginTop">内容距上边框距离</param> ///<paramname="marginBottom">内容距下边框距离</param> publicPDFOperation(stringtype,floatmarginLeft,floatmarginRight,floatmarginTop,floatmarginBottom) { SetPageSize(type); document=newDocument(rect,marginLeft,marginRight,marginTop,marginBottom); } #endregion #region私有字段 privateFontfont; privateRectanglerect;//文档大小 privateDocumentdocument;//文档对象 privateBaseFontbasefont;//字体 #endregion #region设置字体 ///<summary> ///设置字体 ///</summary> publicvoidSetBaseFont(stringpath) { basefont=BaseFont.CreateFont(path,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); } ///<summary> ///设置字体 ///</summary> ///<paramname="size">字体大小</param> publicvoidSetFont(floatsize) { font=newFont(basefont,size); } #endregion #region设置页面大小 ///<summary> ///设置页面大小 ///</summary> ///<paramname="type">页面大小(如"A4")</param> publicvoidSetPageSize(stringtype) { switch(type.Trim()) { case"A4": rect=PageSize.A4; break; case"A8": rect=PageSize.A8; break; } } #endregion #region实例化文档 ///<summary> ///实例化文档 ///</summary> ///<paramname="os">文档相关信息(如路径,打开方式等)</param> publicvoidGetInstance(Streamos) { PdfWriter.GetInstance(document,os); } #endregion #region打开文档对象 ///<summary> ///打开文档对象 ///</summary> ///<paramname="os">文档相关信息(如路径,打开方式等)</param> publicvoidOpen(Streamos) { GetInstance(os); document.Open(); } #endregion #region关闭打开的文档 ///<summary> ///关闭打开的文档 ///</summary> publicvoidClose() { document.Close(); } #endregion #region添加段落 ///<summary> ///添加段落 ///</summary> ///<paramname="content">内容</param> ///<paramname="fontsize">字体大小</param> publicvoidAddParagraph(stringcontent,floatfontsize) { SetFont(fontsize); Paragraphpra=newParagraph(content,font); document.Add(pra); } ///<summary> ///添加段落 ///</summary> ///<paramname="content">内容</param> ///<paramname="fontsize">字体大小</param> ///<paramname="Alignment">对齐方式(1为居中,0为居左,2为居右)</param> ///<paramname="SpacingAfter">段后空行数(0为默认值)</param> ///<paramname="SpacingBefore">段前空行数(0为默认值)</param> ///<paramname="MultipliedLeading">行间距(0为默认值)</param> publicvoidAddParagraph(stringcontent,floatfontsize,intAlignment,floatSpacingAfter,floatSpacingBefore,floatMultipliedLeading) { SetFont(fontsize); Paragraphpra=newParagraph(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> ///<paramname="path">图片路径</param> ///<paramname="Alignment">对齐方式(1为居中,0为居左,2为居右)</param> ///<paramname="newWidth">图片宽(0为默认值,如果宽度大于页宽将按比率缩放)</param> ///<paramname="newHeight">图片高</param> publicvoidAddImage(stringpath,intAlignment,floatnewWidth,floatnewHeight) { Imageimg=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> ///<paramname="Content">链接文字</param> ///<paramname="FontSize">字体大小</param> ///<paramname="Reference">链接地址</param> publicvoidAddAnchorReference(stringContent,floatFontSize,stringReference) { SetFont(FontSize); Anchorauc=newAnchor(Content,font); auc.Reference=Reference; document.Add(auc); } ///<summary> ///添加链接点 ///</summary> ///<paramname="Content">链接文字</param> ///<paramname="FontSize">字体大小</param> ///<paramname="Name">链接点名</param> publicvoidAddAnchorName(stringContent,floatFontSize,stringName) { SetFont(FontSize); Anchorauc=newAnchor(Content,font); auc.Name=Name; document.Add(auc); } #endregion } }

希望本文所述对大家的C#程序设计有所帮助。

您可能感兴趣的文章:C#利用Aspose.Words.dll将Word转成PDFC#使用itextsharp生成PDF文件的实现代码C#生转换网页为pdfC#中使用iTextSharp组件创建PDF的简单方法C#将jpg转换为pdf的方法C#使用iTextSharp从PDF文档获取内容的方法C#实现pdf导出.Net导出pdf文件C#实现HTML转WORD及WORD转PDF的方法用C#来解析PDF文件C#使用privatefont改变PDF文件的字体详解

标签: ext arp 封装 操作 ar ex

C/C++的浮点数在内存中的存储方式分析及实例

C语言 文件操作解析详解及实例代码

上述就是C#学习教程:C#使用iTextSharp封装的PDF文件操作类实例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906103.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐