我正在尝试在 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/