使用的方式為,讀取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