【问题标题】:.NET C# - MigraDoc - How to change document charset?.NET C# - MigraDoc - 如何更改文档字符集?
【发布时间】:2011-12-10 23:49:13
【问题描述】:

我已经搜索了这个问题的解决方案,但仍然找不到答案。任何帮助将不胜感激。

    Document document = new Document();
    Section section = document.AddSection();

    Paragraph paragraph = section.AddParagraph();

    paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50);

    paragraph.AddText("ąčęėįųųūū");

    paragraph.Format.Font.Size = 9;
    paragraph.Format.Alignment = ParagraphAlignment.Center; 
    </b>

<...>

在上述示例中,导出的 pdf 中不显示字符“ąčęėįųųūū”。

如何设置“MigraDoc”字符集?

【问题讨论】:

    标签: c# .net pdf migradoc


    【解决方案1】:

    只需告诉渲染器创建一个 Unicode 文档:

    PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
    renderer.Document = document;
    

    PdfDocumentRenderer 的第一个参数必须为 true 才能获取 Unicode。 请注意,并非所有 True Type 字体都包含所有 Unicode 字符(但它应该适用于 Arial、Verdana 等)。

    完整示例请参见此处: http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

    【讨论】:

    • 非常感谢! :) 工作就像一个魅力。
    • 那么 WinANSI 是 MigraDoc 的默认字符集吗?
    • PDF 文件采用 PDF 编码(很像 ANSI)或 Unicode 编码。
    • 我下载文件到 Try Thai Font。它不仅适用于泰语字体,但其他语言都可以。
    【解决方案2】:

    如果你是 mixing PDFSharp and MigraDoc,就像我一样(这意味着你有一个 PdfSharp 对象 PdfDocument 文档 和一个 MigraDoc 对象 Document doc ,它被渲染为 document)的一部分),一切都没有那么简单。 PDFSharp 团队给出的示例仅在您单独使用 MigraDoc 时才有效。

    所以你应该这样使用它:

    • 确保在将 MigraDoc 对象渲染为 PDF 之前渲染 MigraDoc 文档 XGraphics gfx
    • 使用 hack 为 gfx 对象设置编码。

    XGraphics gfx = XGraphics.FromPdfPage(page);
            // HACK²
                gfx.MUH = PdfFontEncoding.Unicode;
                gfx.MFEH = PdfFontEmbedding.Always;
            // HACK²
      Document doc = new Document();
    
      PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
            pdfRenderer.Document = doc;
            pdfRenderer.RenderDocument();
    
      MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
            docRenderer.PrepareDocument();
            docRenderer.RenderObject(gfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), "12cm", para);
    

    对于 1.5.x-betax

     let gfx = XGraphics.FromPdfPage(page)
     gfx.MUH <- PdfFontEncoding.Unicode
     let doc = new Document()
    
     let pdfRenderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always)
     pdfRenderer.Document <- doc
     pdfRenderer.RenderDocument()
    
     let docRenderer = new DocumentRenderer(doc)
     docRenderer.PrepareDocument()
     docRenderer.RenderObject(gfx, XUnit.FromCentimeter 5, XUnit.FromCentimeter 10, "12cm", para)
    

    【讨论】:

    • 感谢您链接到特殊用例示例,但四年前的问题显然是关于标准用例的。
    • 从来没有注意到你明确指出的区别。感谢您的精彩提示。
    猜你喜欢
    • 2013-10-16
    • 1970-01-01
    • 2011-07-05
    • 2022-11-22
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2019-04-19
    相关资源
    最近更新 更多