/*
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();
}
}
}
輸出: