新建控制台程序
添加包Syncfusion.Pdf.Net.Core包,当前官方的版本号为26.1.39
直接上代码
- Syncfusion.Pdf.PdfDocument pdfDocument = new Syncfusion.Pdf.PdfDocument();
- for (int i = 1; i <= 10; i++)
- {
- var page = pdfDocument.Pages.Add();
- PdfGraphics graphics = page.Graphics;
- PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
- graphics.DrawString($"pdf文件生成测试,第{i}页", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
- }
- MemoryStream stream = new MemoryStream();
- pdfDocument.Save(stream);
- File.WriteAllBytes("pdf.pdf", stream.ToArray());
生成的pdf有如下问题:
汉字丢失,有未注册的水印
添加注册代码,添加中文字体
- string key = "xxxxx";
- Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(key);
- Syncfusion.Pdf.PdfDocument pdfDocument = new Syncfusion.Pdf.PdfDocument();
- for (int i = 1; i <= 10; i++)
- {
- var page = pdfDocument.Pages.Add();
- PdfGraphics graphics = page.Graphics;
- //PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
- PdfFont font = new PdfTrueTypeFont("simsun.ttc", 20);
- graphics.DrawString($"pdf文件生成测试,第{i}页", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
- }
- MemoryStream stream = new MemoryStream();
- pdfDocument.Save(stream);
- File.WriteAllBytes("pdf.pdf", stream.ToArray());
中文字体到windows字体里复制即可
再次生成如下图
word与xls或ppt方法类似,此处代码省略,库名见下图