使用C#将txt文本文件转换为pdf文件
[C#] 纯文本查看 复制代码 static void Main(string[] args)
{
const string txtFile = "D:\\1.txt";
const string pdfFile = "D:\\1.pdf";
var document = new Document(PageSize.A4, 30f, 30f, 30f, 30f);
PdfWriter.getInstance(document, new FileStream(pdfFile, FileMode.Create));
document.Open();
var bfSun = BaseFont.createFont(@"C:\Windows\Fonts\simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
var font = new Font(bfSun, 12f);
var objReader = new StreamReader(txtFile, Encoding.GetEncoding("gb2312"));
var str = "";
while (str != null)
{
str = objReader.ReadLine();
if (str != null)
{
document.Add(new Paragraph(str, font));
}
}
objReader.Close();
document.Close();
}
|