c# - 使用 MigraDoc DocumentRenderer 在 PDF 中显示 Unicode 字符

标签 c# pdf unicode pdfsharp migradoc

我正在尝试在 PDF 生成器中使用 Unicode 字符。我首先使用 PDFsharp 创建页面的主布局(例如每个页面上的水印和形状),然后尝试使用 MigraDoc 添加文本。虽然在 PDFsharp 中字体一切顺利且代码运行良好,但 MigraDoc 会创建空字符。简单的代码如下所示:

//with PDFsharp
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();

XGraphics gfx = XGraphics.FromPdfPage(page);
string s = "ĄŻŹĆĘŁÓążźęćłó";
XFont font16 = new XFont("Calibri", 16, XFontStyle.Regular);
gfx.DrawString(s, font16, XBrushes.Black, 70, 50, XStringFormats.Default);

//with MigraDoc
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();
para.Format.Font.Name = "Calibri";
para.Format.Font.Size = 16;
para.AddText(s);

MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
docRenderer.RenderPage(gfx, 1);
//

document.Save("test.pdf");
Process.Start("test.pdf");

我尝试了不同的字体,但没有任何效果。

最佳答案

在调用任何 MigraDoc 代码之前,尝试为 gfx 设置 gfx.MUH = PdfFontEncoding.Unicode;

给初学者阅读的注意事项:如果使用 PdfDocumentRenderer,生活会更简单:
https://stackoverflow.com/a/48192026/162529
您可以仅使用 MigraDoc 功能将图像或 PDF 页面用作页面背景,而无需混合使用 MigraDoc 和 PDFsharp。

DocumentRenderer 允许混合 PDFsharp 和 MigraDoc - 实现超出 MigraDoc 单独功能的功能。在这种情况下,您必须使用上面显示的 MigraDoc Unicode Hack MUH 来启用 Unicode 支持。 PDFsharp 网站上的官方混合示例中也使用了 MUH:
http://www.pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx PDFsharp 论坛上发布的示例中也使用了它:
http://forum.pdfsharp.net/viewtopic.php?f=8&t=3172

关于c# - 使用 MigraDoc DocumentRenderer 在 PDF 中显示 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48624618/

相关文章:

c# - 垂直平铺windows c#

java - 打开 PDF 的 Intent 在显示文件之前立即关闭

c#/Access 2010 - 从任务计划程序运行 DoCmd.OutputTo 错误

javascript - 如何在reactjs前端正确地将pdf字符串转换为pdf? (带有 puppeteer 的空白pdf)

MySQL WHERE `character` = 'a' 匹配 a、A、à 等。为什么?

sql - 奇怪的 SQL 行为,为什么这个查询什么都不返回?

c# - 如何使用 bool 条件使用 C# LINQ 从列表中获取对象?

c# - 如何通知多个 View 模型变量更改?

perl - perl 的 File::Glob 是否应该始终通过 utf8::decode 进行后过滤?

c# - 声明一个同时支持 Void 和 IEnumerator 的函数