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

MigraDoc custom fonts
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1789
Page 1 of 1

Author:  mitja.GTI [ Tue Sep 20, 2011 3:07 pm ]
Post subject:  MigraDoc custom fonts

Hi,

i'm using MigraDoc in ASP.NET 4.0 WebApplication. Because the server does not have all the necessary fonts installed, i'm trying to use custom fonts.
I've looked at this forum and also at samples, but there's only a small sample that is not verry helpfull. I did cache some forum topic that i should use WPF version, so i added the refference to WPF assemblies...

So far i have this (which is not working)
Code:
XPrivateFontCollection pfc = new XPrivateFontCollection();
Uri uri = new Uri("http://localhost:0000/design/fonts/MTCORSVA.TTF");
pfc.Add(uri, "#Monotype Corsiva");

PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
pdfRenderer.Document = CreatePDF();
pdfRenderer.DocumentRenderer.PrivateFonts = pfc;
pdfRenderer.RenderDocument();
System.IO.MemoryStream File = new System.IO.MemoryStream();
pdfRenderer.PdfDocument.Save(File, false);
//...
style = document.Styles.AddStyle("Header1", "Normal");
style.Font.Name = "Monotype Corsiva";
//...

Cannot get a matching glyph typeface for font 'Monotype Corsiva'.

Would someone be so kind and show me a small sample?

Thank you in advance.

Author:  Thomas Hoevel [ Tue Sep 20, 2011 3:27 pm ]
Post subject:  Re: MigraDoc custom fonts

A working PDFsharp sample is included:
http://www.pdfsharp.net/wiki/PrivateFonts-sample.ashx

Will take a closer look tomorrow.

Author:  mitja.GTI [ Tue Sep 20, 2011 3:56 pm ]
Post subject:  Re: MigraDoc custom fonts

Hi Thomas,

as i said before, i am aware of that sample and unfortunatly it doesn't help me.
I'm trying this for most of the day without any luck. I tried with PrivateFontCollection and XPrivateFontCollection, WPF and GDI.
I'd really appreciate if you could post some sample code (i guess this must be done in a few lines).

Thank you again.

Author:  Thomas Hoevel [ Wed Sep 21, 2011 7:25 am ]
Post subject:  Re: MigraDoc custom fonts

Private fonts work only with the WPF build of PDFsharp 1.31.

We only have code that loads the fonts from resources - that's what the sample does, that's what we need and we know it works.

You are trying to load a true type font file from HTTP. We never tried that and I don't have time to try it this week.

So you can load the fonts from resources like the sample does - or continue your approach with the TTF via HTTP, but I cannot help with that.

The task is adding a font to System.Windows.Media.FontFamily - this has nothing to do with PDFsharp, this is a .NET task. Maybe you can find suitable samples on the web.

We embed fonts in resources. If you can use the same approach, then you can rely on the sample provided.
Syntax will be slightly different if fonts are in a different assembly (not the main EXE):
Code:
if (privateFontCollection == null)
{
  privateFontCollection = new PdfSharp.Drawing.XPrivateFontCollection();

  Uri uri = new Uri("pack://application:,,,/");
  privateFontCollection.Add(uri, "Qbx.Reha.Customization.SampleAssembly;component/FrutigerFonts/#FrutigerLight");
  privateFontCollection.Add(uri, "Qbx.Reha.Customization.SampleAssembly;component/FrutigerFonts/#Frutiger");
}

Before ";component" you specify the name of the assembly, after ";component" you specify the location of the font inside the assembly. You don't specify the name(s) of the TTF files anywhere (fonts families normally consist of more than 1 TTF file).
Maybe that's why HTTP doesn't work: with "pack:" protocol you don't specify filenames, the system searches appropriate files; searching probably doesn't work with HTTP.

Author:  mitja.GTI [ Wed Sep 21, 2011 4:00 pm ]
Post subject:  Re: MigraDoc custom fonts

Hi,

thank you for your reply. I did take a look at http://msdn.microsoft.com/en-us/library/y505zzfw.aspx and managed to load the fonts with the code below.
There's only 1 question left. How would i check if the fonts are already loaded, because the second time i try to create the pdf i get ArgumentException: An entry with the specified family name already exists.?

Quote:
try
{
Uri uri = new Uri("file:///" + HttpContext.Current.Request.MapPath("/design/fonts/"));
XPrivateFontCollection.Global.Add(uri, "./#Monotype Corsiva");
}
catch (ArgumentException) { }

PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
pdfRenderer.Document = CreatePDF();
pdfRenderer.RenderDocument();
//...

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