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

font installed on server
https://forum.pdfsharp.net/viewtopic.php?f=2&t=969
Page 1 of 1

Author:  mikesowerbutts [ Thu Dec 03, 2009 1:29 pm ]
Post subject:  font installed on server

Hi,

I am trying to use a font (for chinese characters) which may not be installed on the users local machine. I have included this font with the code ad am embedded resource and I load it into an XPrivateFontCollection. I would like to use this loaded font in my MigraDoc tables. How do I do this?

Mike

Author:  Thomas Hoevel [ Thu Dec 03, 2009 1:46 pm ]
Post subject:  Re: font installed on server

Hi!

We use private (ANSI) fonts in our application (WPF build).

Just add them to the private font collection and use them like any other font.

Private fonts are searched first, so a private font will hide a locally installed font with the same name.

The trickiest part is the string that identifies the resource.

Author:  mikesowerbutts [ Thu Dec 03, 2009 1:55 pm ]
Post subject:  Re: font installed on server

Hi Thomas,

thanks for the quick reply!

which PrivateFontCollection? is there one associated with the PDF?

Mike

Author:  Thomas Hoevel [ Thu Dec 03, 2009 2:47 pm ]
Post subject:  Re: font installed on server

You must pass your font collection to the renderer:
Code:
docRenderer = new DocumentRenderer(doc);
docRenderer.PrivateFonts = privateFontCollection;

Author:  mikesowerbutts [ Thu Dec 03, 2009 4:09 pm ]
Post subject:  Re: font installed on server

Hi Thomas,

I am doing this now, but I get an error message in a popup: "Value differs from information retrieved from font image."

I have never really come across an error like this whilst using PdfSharp/MigraDoc.

The font I am trying to add to my private fonts is "SimHei". I am loading it in using the following function:
Code:
public void LoadFonts(Boolean isEmbedded)
        {
            Stream fontStream;
            PrivateFonts = new XPrivateFontCollection();
            if (isEmbedded)
            {
                Assembly a = Assembly.GetExecutingAssembly();
                string[] resNames = a.GetManifestResourceNames();
                int streamLen = 0;
                int i = 0;
                foreach (string s in resNames)
                {
                    if (s.EndsWith(".ttf"))
                    {
                        fontStream = a.GetManifestResourceStream(s);
                        streamLen = Convert.ToInt32(fontStream.Length);
                        byte[] fontData = new byte[fontStream.Length];
                        fontStream.Read(fontData, 0, streamLen);
                        fontStream.Close();
                        PrivateFonts.AddMemoryFont(fontData, fontData.Length, resNames[i], false, false);
                    }
                    i++;
                }
            }
            else
            {
            }
        }

and I am ying to use it here:
Code:
Font _font = MigFont.Clone();
                    if (Utils.CheckIsChinese(element.text))
                    {
                        _font = new Font(PrivateFonts.PrivateFontCollection.Families[0].Name); // Here specifically
                    }
                    _font.Bold = element.format.bold;
                    _font.Italic = element.format.italic;
                    _font.Size = element.format.size;
                    _font.Underline = element.format.underlined;


I dont really understand why this is occuring? The font is fine when accessed from my machine (i.e. it is installed, rather than loading it in at run time through the PrivateFonts)

any ideas?

Mike

Author:  Thomas Hoevel [ Thu Dec 03, 2009 4:23 pm ]
Post subject:  Re: font installed on server

Is the message an Assertion Message? Then it won't come with a release build.
Ignore it if the font looks alright.

Author:  mikesowerbutts [ Thu Dec 03, 2009 4:48 pm ]
Post subject:  Re: font installed on server

Hi Thomas,

yes it was an Assertion message. I have avoided this message appearing by loading the font directly from the file like:
Code:
if (PrivateFonts.PrivateFontCollection == null)
                {
                    PrivateFonts.PrivateFontCollection = new System.Drawing.Text.PrivateFontCollection();
                }
                string _path = HttpContext.Current.Server.MapPath("Includes/Fonts/SimHei.ttf");
                if (File.Exists(_path))
                {
                    PrivateFonts.PrivateFontCollection.AddFontFile(_path);
                }

instead of using the AddMemoryFont() function.

cheers though!

Mike

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