一、前言
最近项目开发涉及到一个打印pdf报表的功能需求,从nuget github等开源库上找了老半天确定了 PdfSharpCore 开源类库,据说目前处于 Alpha测试状态,生成使用也没太大问题。
二、使用步骤
1、创建项目
创建.netcore 控制台项目,选择.netcore3.x 或则 .net 5.0 都行,不做过多展示了。
2、引用PadSharpCore
neget 引入PdfSharpCore库:
3、代码测试
static void Main(string[] args)
{
//1、创建PDF文档
PdfDocument tmpDoc = new PdfDocument();
//2、添加空白页
PdfPage tmpPage = tmpDoc.AddPage();
//3、定义字体:这地方用的是常规的 黑体
XFont tmpFont = new XFont("SimHei", 24);
//4、准备绘图工具
XGraphics tmpGraphics = XGraphics.FromPdfPage(tmpPage);
//5、在空白页顶部中间绘制标题文字
tmpGraphics.DrawString("中文英文I hate you shadiao123", tmpFont, XBrushes.Black, new XRect(0, 0, tmpPage.Width, 40), XStringFormats.Center);
//6、创建pdf文件
tmpDoc.Save("test.pdf");
//7、打开刚才创建的pdf文件
Process process = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo("test.pdf");
process.StartInfo = processStartInfo;
process.StartInfo.UseShellExecute = true;
process.Start();
}
4、注意字体商用版权
目前很多字体都是有版权的,商用要特别注意,可以去网上下载免费字体配套使用,本案例用的是黑体,据说版权归属者中易倒闭了,可以大胆用…
三、总结
还有其他好多版本的辅助类可供选择,针对复制的内容布局尚未尝试。