对于将string字符串输出成pdf文件,首先导入一个包iTextSharp,可以在NuGet里面下载。
然后就是
using iTextSharp.text;
using iTextSharp.text.pdf;
在网上看到一些文章都有些繁琐,所以就写了这个直接明了的C#代码,只要输入字符串就可以了。
private static void create(string contentText)
{
Console.WriteLine("开始创建pdf文件");
Document document = new Document();//创建一个文件
PdfWriter.GetInstance(document, new FileStream(pdfName, FileMode.Create));//pdf的实
//例化到当前文件夹
BaseFont baseFont = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_CACHED);//找到基础字体
Font contentFont = new Font(baseFont, 12);//创建一个新的字体
document.Open();
document.Add(new Paragraph(contentText, contentFont));//写入一个段落
document.Close();
Console.WriteLine("创建pdf文件成功");
}