PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Jul 18, 2024 5:21 am

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: MigraDoc custom fonts
PostPosted: Tue Sep 20, 2011 3:07 pm 
Offline

Joined: Tue Mar 08, 2011 11:38 am
Posts: 6
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 20, 2011 3:27 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
A working PDFsharp sample is included:
http://www.pdfsharp.net/wiki/PrivateFonts-sample.ashx

Will take a closer look tomorrow.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 20, 2011 3:56 pm 
Offline

Joined: Tue Mar 08, 2011 11:38 am
Posts: 6
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 21, 2011 7:25 am 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 21, 2011 4:00 pm 
Offline

Joined: Tue Mar 08, 2011 11:38 am
Posts: 6
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();
//...


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 61 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