PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jul 19, 2024 3:26 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Embedding Fonts
PostPosted: Wed Feb 15, 2012 7:34 pm 
Offline

Joined: Wed Feb 08, 2012 6:36 pm
Posts: 8
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Embedding Fonts
PostPosted: Thu Feb 16, 2012 8:28 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Embedding Fonts
PostPosted: Thu Feb 16, 2012 3:54 pm 
Offline

Joined: Wed Feb 08, 2012 6:36 pm
Posts: 8
I've added in Unicode and Always, and am still having this font come out as Arial.


Top
 Profile  
Reply with quote  
 Post subject: Re: Embedding Fonts
PostPosted: Wed Feb 22, 2012 9:32 pm 
Offline

Joined: Wed Feb 08, 2012 6:36 pm
Posts: 8
Bump.


Top
 Profile  
Reply with quote  
 Post subject: Re: Embedding Fonts
PostPosted: Thu Feb 23, 2012 9:19 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 41 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group