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

Embedding fonts using PdfSharp GDI+
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1763
Page 1 of 1

Author:  dtc [ Tue Aug 30, 2011 10:19 am ]
Post subject:  Embedding fonts using PdfSharp GDI+

Im using the GDI+ version of PdfSharp on a project that generates PDF from user input.
The system needs to have custom fonts available which will need to be embedded into the PDF, all these fonts are already preinstalled on the server.

The trouble I'm having is that on every font I use the PDF generated says that it has correctly embedded (in font preferences) but the actual Glyphs Embedded are ones from the AdobeSans Font.

I have already defined both: gfx.MFEH = PdfFontEmbedding.Automatic and New XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always).

All the fonts that I am using have the "installable" embed settings.

Before generating the PDF I am creating a low-res jpeg of the PDF fonts in this appear correctly, so i know the fonts are all correctly installed.

I have tried Type1, TrueType and OpenTypes fonts all with the same issue.

Is there anything I'm missing about fonts that are allowed to be embedded?

I've attached the Preflight Font Inventory that uses a this font http://www.dafont.com/black-adder2.font as you can see the Glyphs are completely wrong.

Thanks in advance

dtc

Attachments:
fontInventry.PNG
fontInventry.PNG [ 106.53 KiB | Viewed 12320 times ]

Author:  Thomas Hoevel [ Tue Aug 30, 2011 11:30 am ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

I installed the font on my computer and ran a slightly modified MigraDoc HelloWorld sample - and the font was embedded.

Code:
paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50); // Original line from HelloWorld sample
paragraph.Format.Font.Name = "BlackAdderII"; // Line added for testing


Maybe insufficient privileges on your server, maybe an error in your code, maybe something else.

Author:  dtc [ Tue Aug 30, 2011 11:53 am ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

Thanks for the response.

As I say im using the PDFSharp rather than MigraDoc dont know if this makes any difference. The code im using is:

Code:
 Dim brush = New XSolidBrush(c)
Dim tf = New XTextFormatter(gfx)
Dim options = New XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always)
font = New Xfont(face, size)
tf.Alignment = GetIntFromStringAlignmentPdf(alinment)
tf.DrawString(cont.Text, font, brush, New XRect(x, y, w, h), XStringFormats.TopLeft)

Author:  Thomas Hoevel [ Tue Aug 30, 2011 12:47 pm ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

MigraDoc uses PDFsharp internally, so if it works with MigraDoc then it works with PDFsharp.

Maybe it works better if you pass your options to the XFont constructor: :wink:
Code:
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.WinAnsi, PdfFontEmbedding.Always);
// Create a font
XFont font = new XFont("BlackAdderII", 20, XFontStyle.Regular, options);

It worked for me ...

Author:  dtc [ Tue Aug 30, 2011 1:02 pm ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

Good plan. Still didn't work :( i removed it from there after applying
Code:
gfx.MFEH = PdfFontEmbedding.Automatic


Do they both need to be applied or is it just a case of one or the other?

I also noticed the added PdfFontEncoding.WinAnsi and wondered if that could be the culprit but still no joy.

Im still getting the pdf telling me that the font is embedded but as you can see from the attached image when i download the pdf from the server it just doesnt seem to embed the correct glyths.

Im using dll version 1.0.1870.19498 just in case your using a different version.

Attachments:
font.PNG
font.PNG [ 14.34 KiB | Viewed 12314 times ]

Author:  Thomas Hoevel [ Tue Aug 30, 2011 1:20 pm ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

I'm using PDFsharp 1.31.

I tried the PDFsharp HelloWorld sample, but changed the font name and included the option (no MFEH used).

The document properties of the PDF file will tell you if fonts were embedded:
Attachment:
EmbeddedFonts.png
EmbeddedFonts.png [ 9.89 KiB | Viewed 12313 times ]

I don't know if it works with 1.0 (but I think it should work).

When the correct font is used to determine the positions of the words but a different font is used for display, you'll get either big gaps or overlapping text.
The problem is with the embedding options.

Author:  dtc [ Tue Aug 30, 2011 1:32 pm ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

The screenshot you provided is exactly what i get once i have downloaded the pdf. I have had one or two pdfs that also had Actual Font: AdobeSans MS.

What do you mean by embedding options, i have set embedding as always or is there other options hidden away somewhere?

I'll try and download the latest build and see if that works.

Author:  dtc [ Tue Aug 30, 2011 1:37 pm ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

Just updated to 1.31 still no joy.

It's probably more useful to you if i send you the URL to the issue:

http://esptest.digitaltradingco.co.uk/w ... museo.aspx

The first page shows the jpeg that it being generated with the correct font, when clicking Next the PDF will be generated and shown in an iframe.

Author:  Thomas Hoevel [ Tue Aug 30, 2011 2:09 pm ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

Probably a security problem (insufficient privileges for application pool).

Which account do you use for your application pool?
Try System (or Local System) to see if that works.

PDFsharp can get the font metrics, but not the font data (funny MS framework won't throw an exception, instead it silently returns a Sans Serif font).

Author:  dtc [ Tue Aug 30, 2011 2:47 pm ]
Post subject:  Re: Embedding fonts using PdfSharp GDI+

Ah that sounds exactly like whats happening here, the site is currently running under NETWORK SERVICES.

Just tried updating it to both LocalServices and LocalSystem (don't have system as an option) and it still has the same problem. I restarted IIS both times.

I've just tried getting the Administrator to take ownership of the fonts directory and set the website to use connect through the administrator account. Again still no luck.

EDIT:
Success! By changing the font folder owner permissions to network services and changing the registry entry to have network services permissions via (http://www.sevenforums.com/general-disc ... ssion.html) it didn't work until i restarted the server. A warning to anyone else going through the same thing I don't know if resetting owner permissions will have any ill effects on anything else. Thanks for you help.

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