700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C#实现将TXT文件转换为PDF文件

C#实现将TXT文件转换为PDF文件

时间:2019-04-19 01:37:21

相关推荐

C#实现将TXT文件转换为PDF文件

下面是.txt文件转换成.pdf文件的主要代码:

[html] view plain copyusingiTextSharp.text;usingiTextSharp.text.pdf;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceTxtToPDF{classProgram{staticvoidMain(string[]args){//要转换的文件的路径stringpath="D:\\txts\\測試.txt";//第一个参数是txt文件物理路径string[]lines=System.IO.File.ReadAllLines(path,Encoding.GetEncoding("utf-8"));//iTextSharp.text.PageSize.A4自定义页面大小Documentdoc=newDocument(iTextSharp.text.PageSize.A4,50,20,20,20);PdfWriterpdfwriter=PdfWriter.GetInstance(doc,newFileStream(path.ToString().Substring(0,path.ToString().Length-4)+".pdf",FileMode.Create));doc.Open();//创建我的基本字体BaseFontbaseFont=BaseFont.CreateFont("c:\\windows\\fonts\\Arial.TTF","Identity-H",false);//创建字体字体大小,字体粗細字体颜色Fontfont=newFont(baseFont,11,iTextSharp.text.Font.NORMAL,BaseColor.BLACK);Paragraphparagraph;foreach(stringlineinlines){paragraph=newParagraph(line,font);doc.Add(paragraph);}//关闭文件doc.Close();Console.WriteLine("txt轉換PDF完成!");Console.ReadKey();}}}

运行前的.txt文件:

运行后,生成了.pdf文件:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。