Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Chinese fonts? #114

Closed
HmgsLJ opened this issue May 6, 2024 · 4 comments
Closed

How to use Chinese fonts? #114

HmgsLJ opened this issue May 6, 2024 · 4 comments
Labels
forum Please use the Support forum to ask Support questions - https://forum.pdfsharp.net/ support

Comments

@HmgsLJ
Copy link

HmgsLJ commented May 6, 2024

First thanks to the author for providing a good library, but I had a problem with version 6.0, that is, using Chinese fonts will make an error, what can I do to solve it.

Error:

System.InvalidOperationException:“No appropriate font found for family name '宋体'. Implement IFontResolver and assign to 'GlobalFontSettings.FontResolver' to use fonts.”

Code:

           // 创建一个新的PDF文档
           using (PdfDocument document = new PdfDocument())
           {
               // 添加一页
               PdfPage page = document.AddPage();
               XGraphics gfx = XGraphics.FromPdfPage(page);
               // 加载字体
               //XFont font = new XFont("simsun.ttc", 12, XFontStyleEx.Regular);
               XFont font = new XFont("宋体", 12, XFontStyleEx.Regular);

               // 绘制中文文本
               gfx.DrawString("你好,世界!", font, XBrushes.Black, new XRect(50, 50, page.Width, page.Height), XStringFormats.TopLeft);

               // 保存PDF文档
               string outputPath = "D://output.pdf";
               document.Save(outputPath);
           }
@ThomasHoevel
Copy link
Member

The answer to your question is in the error message: Implement IFontResolver or use either the GDI or WPF build.

See also:
https://docs.pdfsharp.net/PDFsharp/Topics/Fonts/About.html

@ThomasHoevel ThomasHoevel added support forum Please use the Support forum to ask Support questions - https://forum.pdfsharp.net/ labels May 6, 2024
@HmgsLJ
Copy link
Author

HmgsLJ commented May 6, 2024

问题的答案在错误消息中:实现或使用 GDI 或 WPF 生成。IFontResolver

另请参阅:https://docs.pdfsharp.net/PDFsharp/Topics/Fonts/About.html

Yes, I saw it, felt my stupidity, the problem was solved, implementing IFontResolver fixed the problem

@HmgsLJ HmgsLJ closed this as completed May 6, 2024
@ThomasHoevel
Copy link
Member

No need to feel stupid. This is a breaking change that needs some explanation and we are trying to improve this error message even further.

@HmgsLJ
Copy link
Author

HmgsLJ commented May 6, 2024

No need to feel stupid. This is a breaking change that needs some explanation and we are trying to improve this error message even further.

Thank you .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forum Please use the Support forum to ask Support questions - https://forum.pdfsharp.net/ support
Projects
None yet
Development

No branches or pull requests

2 participants