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

2

7

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 ?

Bureaucrat answered 19/10, 2011 at 14:27 Comment(0)
B
11

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

Basophil answered 20/10, 2011 at 9:34 Comment(4)
Thanks a lot! :) Worked like a charm.Bureaucrat
So is WinANSI the default charset for MigraDoc?Peacetime
PDF files have PDF encoding (much like ANSI) or Unicode encoding.Basophil
I download the file to Try Thai Font. It doesn't work only Thai font but the rest languages are ok .Generalship
M
4

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)
Mayhew answered 28/2, 2016 at 14:20 Comment(2)
Thanks for linking to the special use case sample, but the four-year old question obviously was about the standard use case.Basophil
Never noticed the distinction you clearly pointed out. Thanks for a great hint.Jorie

© 2022 - 2024 — McMap. All rights reserved.