iTextSharp使用入门(三)【将txt转换为PDF】
2017-12-28 16:09
232 查看
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace TxtConvert
{
class Program
{
static void Main(string[] args)
{
//第一个参数是txt文件物理路径
string[] lines = System.IO.File.ReadAllLines(args[0], Encoding.GetEncoding("utf-8"));
Document doc = new Document(iTextSharp.text.PageSize.A4, 50, 20, 20, 20);
PdfWriter pdfwr = PdfWriter.GetInstance(doc,new FileStream(args[0].ToString().Substring(0, args[0].ToString().Length - 4) + ".pdf",
FileMode.Create));
doc.Open();
//创建我的基础字体
BaseFont base_font = BaseFont.CreateFont("simkai.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//创建我的字体
Font my_font = new Font(base_font, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
Paragraph paragraph;
foreach (string line in lines)
{
paragraph = new Paragraph(line, my_font);
doc.Add(paragraph);
}
doc.Close();
System.Console.WriteLine("TXT转换PDF完成! \n");
System.Console.ReadKey();
}
}
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/14/bc7e3f9a848528557683267632ca6a71)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/14/3492fcc235ea3fe7576a37f7297f6643)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202007/14/98b518c23a7bde856e18fe1d449690c8)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace TxtConvert
{
class Program
{
static void Main(string[] args)
{
//第一个参数是txt文件物理路径
string[] lines = System.IO.File.ReadAllLines(args[0], Encoding.GetEncoding("utf-8"));
Document doc = new Document(iTextSharp.text.PageSize.A4, 50, 20, 20, 20);
PdfWriter pdfwr = PdfWriter.GetInstance(doc,new FileStream(args[0].ToString().Substring(0, args[0].ToString().Length - 4) + ".pdf",
FileMode.Create));
doc.Open();
//创建我的基础字体
BaseFont base_font = BaseFont.CreateFont("simkai.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//创建我的字体
Font my_font = new Font(base_font, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
Paragraph paragraph;
foreach (string line in lines)
{
paragraph = new Paragraph(line, my_font);
doc.Add(paragraph);
}
doc.Close();
System.Console.WriteLine("TXT转换PDF完成! \n");
System.Console.ReadKey();
}
}
}
运行结果如图:
相关文章推荐
- iTextSharp使用入门(三)【将txt转换为PDF】
- iTextSharp使用入门(一)【JPG转换成PDF】
- iTextSharp使用入门(一)【JPG转换成PDF】
- java - 使用itext把txt转换pdf
- 使用开源包pdfbox将pdf文件批量转换成txt文件
- pc系统能使用的txt转换成pdf方法
- 使用Linux命令行将txt格式文件转换成pdf
- ubuntu 使用命令行将txt转换为pdf
- 把txt转换成pdf的广泛使用方法
- 将PDF文件转换为TXT文件
- 使用iTextSharp实现PDF文件套打功能
- (开源,GPLv3)EbookCopier 实现(3) - 使用 iTextSharp 库生成 PDF 文档
- VS2008 使用itextsharp生成PDF
- 利用iTextSharp创建/修改/转换/加密PDF文件
- lucene入门-解析pdf(使用xpdf解析中文PDF详细过程)
- lucene入门-解析pdf(使用pdfbox解析英文PDF)
- (开源,GPLv3)EbookCopier 实现(3) - 使用 iTextSharp 库生成 PDF 文档
- Latex中使用dvipdfm转换方式hyperref包生成PDF中文书签乱码的解决方案
- iText使用入门,打开加密的pdf文件(itext Bad user Password的解决方法)
- 关于Protel99使用ADobe PDF将sch转换为PDF的说明