Search code examples
c#pdfpdf-generationemojimigradoc

How to get emoji to appear in the generated PDF using MigraDoc


I'm trying to generate a PDF that contains some emojis but with no success. To make it simple and reproduce-able here is the code:

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


    Paragraph paragraph = section.AddParagraph();
    paragraph.AddFormattedText("👀 😀 💨 ♡", new Font("Segoe UI Emoji"));


    PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
    pdfRenderer.Document = document;
    pdfRenderer.RenderDocument();


    var memoryStream = new MemoryStream();
    var path = Path.GetTempFileName() + ".pdf";
    pdfRenderer.PdfDocument.Save(path);
    Process.Start(path);

But unfortunately empty rectangles appear instead:

��

Solution

  • Update: PDFsharp 6.0.0 and higher supports surrogate pairs and emojis will show.

    This is a known limitation of PDFsharp 1.5. Surrogate pairs do not work yet and Unicode chars that require two 16-bit values (above 0xffff) will not show correctly.

    See also:
    https://github.com/empira/PDFsharp-1.5/issues/63