使用的方式为,读取TXT记事本的内容,然后写入创建的PDF文件。
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(); }
示例下载:txtTopdf.zip