.NET C# - MigraDoc - How to change document charset?

南笙酒味 提交于 2020-01-14 09:39:49

问题


I've searched for solution to this problem, but still cannot find the answer. Any help would be appreciated.

    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>

<...>

In example above characters "ąčęėįųųūū" are not displayed in exported pdf.

How can I set 'MigraDoc' character set ?


回答1:


Just tell the Renderer to create an Unicode document:

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

The first parameter of PdfDocumentRenderer must be true to get Unicode. Please note that not all True Type fonts include all Unicode characters (but it should work with Arial, Verdana, etc.).

See here for a complete sample: http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx




回答2:


If you are mixing PDFSharp and MigraDoc, as I do ( it means that you have a PdfSharp object PdfDocument document and a MigraDoc object Document doc, which is rendered as a part of document), everything is not that simple. The example, that PDFSharp Team has given works only when you are using MigraDoc separately.

So you should use it this way:

  • Make sure you are rendering your MigraDoc doc earlier than rendering the MigraDoc object to the PDF sharp XGraphics gfx.
  • Use the hack to set encoding for the gfx object.

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);

For 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)


来源:https://stackoverflow.com/questions/7822852/net-c-sharp-migradoc-how-to-change-document-charset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!