/* ide: vs 2022 17.5 .net 8.0 QuestPDF 23.12.4 from: https://github.com/QuestPDF/QuestPDF/discussions/560 */ namespace ConsoleAppFontPdfDemo { using QuestPDF; using QuestPDF.Fluent; using QuestPDF.Infrastructure; using QuestPDF.Previewer; using QuestPDF.Helpers; using Microsoft.Win32.SafeHandles; using System.ComponentModel; using System.IO; using System.Security; using System.Diagnostics; internal class Program { static void Main(string[] args) { //这两行必须写 QuestPDF.Settings.License = LicenseType.Community; Settings.CheckIfAllTextGlyphsAreAvailable = false; Console.WriteLine("Hello,CSharp World! Geovin Du,geovindu,涂聚文!"); string baseurl = Environment.CurrentDirectory.ToString() + "\\"; var fontCourierNewPath = baseurl+@"font\MCuteHK-Light.TTF"; //自定义字体 QuestPDF.Drawing.FontManager.RegisterFontWithCustomName("MCuteHK-Light", File.OpenRead(fontCourierNewPath)); var titleStyle = TextStyle.Default.FontSize(36).SemiBold().FontColor(Colors.Blue.Medium); //生成的文件 var pdffile = "geovindu" + DateTime.Now.ToString("yyyyMMHHmmss") + ".pdf"; // code in your main method Document.Create(container => { container.Page(page => { page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.PageColor(Colors.White); page.DefaultTextStyle(x => x.FontSize(20).FontFamily("MCuteHK-Light")); page.Header() .Text("Hello PDF!涂聚文,你好,欢迎你!") .Style(titleStyle); page.Content() .PaddingVertical(1, Unit.Centimetre) .Column(x => { x.Spacing(20); x.Item().Text("语言成了邀功尽责的功臣,还需要行为每日值班吗?\r\n\r\n励学篇\r\n宋·赵恒\r\n\r\n富家不用买良田,书中自有千钟粟。\r\n安居不用架高堂,书中自有黄金屋。\r\n出门莫恨无人随,书中车马多如簇。\r\n娶妻莫恨无良媒,书中自有颜如玉。\r\n男儿欲遂平生志,五经勤向窗前读。\r\n"); x.Item().Image(Placeholders.Image(200, 100)); }); page.Footer() .AlignCenter() .Text(x => { x.Span("第 "); x.CurrentPageNumber(); x.Span("页/共"); x.TotalPages(); x.Span("页"); }); }); }) .GeneratePdf(pdffile); //预览文件 var process = new Process { StartInfo = new ProcessStartInfo(pdffile) { UseShellExecute = true } }; process.Start(); process.WaitForExit(); } } }