Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 838
C.L. Avatar asked Oct 19 '11 14:10

C.L.


2 Answers

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

like image 61
I liked the old Stack Overflow Avatar answered Oct 02 '22 16:10

I liked the old Stack Overflow


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)
like image 43
Leo Kolezhuk Avatar answered Oct 02 '22 17:10

Leo Kolezhuk