PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Sat Apr 04, 2026 5:14 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: font installed on server
PostPosted: Thu Dec 03, 2009 1:29 pm 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 03, 2009 1:46 pm 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 03, 2009 1:55 pm 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
Hi Thomas,

thanks for the quick reply!

which PrivateFontCollection? is there one associated with the PDF?

Mike


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 03, 2009 2:47 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3144
Location: Cologne, Germany
You must pass your font collection to the renderer:
Code:
docRenderer = new DocumentRenderer(doc);
docRenderer.PrivateFonts = privateFontCollection;

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 03, 2009 4:09 pm 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 03, 2009 4:23 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3144
Location: Cologne, Germany
Is the message an Assertion Message? Then it won't come with a release build.
Ignore it if the font looks alright.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 03, 2009 4:48 pm 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 762 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