PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Embedding Fonts
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1909
Page 1 of 1

Author:  Cory Dee [ Wed Feb 15, 2012 7:34 pm ]
Post subject:  Embedding Fonts

I've tried several methods of embedding a font into a PDF and have had no luck so far.

I'm using a mix of MigraDoc to create a document, then using PDFSharp to draw in some images after the fact and rendering.

Code:
//page setup
var document = new Document();

style = document.Styles["Heading1"];
style.Font.Name = "Clarendon LT Bold";
style.Font.Size = 20;
style.Font.Color = Color.FromRgbColor((byte)255, Color.Parse("0x43545E"));
style.Font.Bold = true;

//page content
var sec = document.AddSection();

var p = sec.AddParagraph("Title In Clarendon", "Heading1");
p.AddLineBreak();

p = sec.AddParagraph("Normal stuff here, in Arial");
p.Style = "Normal";


//render
byte[] bytes;

var pdf = new PdfDocument();

var options = new XPdfFontOptions(PdfFontEmbedding.Always);
var font = new XFont("Clarendon LT Bold", 20, XFontStyle.Regular, options);
var docRenderer = new DocumentRenderer(doc);

docRenderer.PrepareDocument();
for (int p = 1; p <= docRenderer.FormattedDocument.PageCount; p++)
{
       var page = pdf.AddPage();
       page.Size = PdfSharp.PageSize.Letter;

       var g = XGraphics.FromPdfPage(page);
       DrawPageHeader(ref docRenderer, ref pdf, g);//draw in a header graphic

       docRenderer.RenderPage(g, p);
}

using (var ms = new MemoryStream())
{
       pdf.Save(ms, true);
       bytes = ms.ToArray();
}

return bytes;


As I said, I've tried 2 or 3 different ways to get the Clarendon font to go with the PDF and work on any computer, but all I can get is for it to show up on a computer that has it installed. It is installed on the server running this code.

Any ideas?

Author:  Thomas Hoevel [ Thu Feb 16, 2012 8:28 am ]
Post subject:  Re: Embedding Fonts

For simple MigraDoc documents, try the way shown in the HelloWorld sample:
http://www.pdfsharp.net/wiki/MigraDocHe ... ample.ashx

For documents mixing MigraDoc and PDFsharp, follow this sample:
http://www.pdfsharp.net/wiki/MixMigraDo ... ample.ashx
Especially these lines:
Code:
gfx.MUH = PdfFontEncoding.Unicode;
gfx.MFEH = PdfFontEmbedding.Default;

IIRC you have to use PdfFontEmbedding.Always if you don't use Unicode. So maybe it's enough to simply activate Unicode.

Author:  Cory Dee [ Thu Feb 16, 2012 3:54 pm ]
Post subject:  Re: Embedding Fonts

I've added in Unicode and Always, and am still having this font come out as Arial.

Author:  Cory Dee [ Wed Feb 22, 2012 9:32 pm ]
Post subject:  Re: Embedding Fonts

Bump.

Author:  Thomas Hoevel [ Thu Feb 23, 2012 9:19 am ]
Post subject:  Re: Embedding Fonts

Our samples work.

It could be a problem with your fonts (which we don't have) or with your code (which we don't have) or with PDFsharp (which we cannot replicate yet).

If you give us a working Visual Studio project then we can examine the problem (attach to this thread or send it via e-mail).

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/