PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 16, 2024 10:03 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Aug 18, 2015 11:27 pm 
Offline

Joined: Mon Oct 13, 2014 3:42 pm
Posts: 10
I've tried my snippet of code below and it doesn't appear to embed any font i use into the pdf. The font's used in this example are from google fonts and then one custom one.

How can i get these fonts to embed into the PDF since the user open the pdf most likely doesn't have these fonts? I've looked around online for a long time an no one appears to have a working solution that i've come across. This is using pdfsharp 1.32.2608.0

Code:
public static void DefaultPDF()
        {
            // Template Brushes
            XBrush orangeBrush = new XSolidBrush(XColor.FromArgb(241, 95, 59));
            XBrush blackBrush = new XSolidBrush(XColor.FromArgb(0,0,0));

            // Template Fonts
            XFont fontHeader = new XFont("Republic Heavy", 85, XFontStyle.Bold);
            XFont fontSub = new XFont("Republic Heavy", 55, XFontStyle.Bold);
            XFont fontPar = new XFont("Source Sans Pro Light", 55, XFontStyle.Regular);

            // Create a new PDF document
            PdfDocument document = new PdfDocument();

            // Create first page (8.5 x 11)
            PdfPage page = new PdfPage();
            page.Width = 2550;
            page.Height = 3300;
            page.Orientation = PageOrientation.Portrait;

            // add page to document
            document.Pages.Add(page);

            // drawing graphics
            XGraphics gfx = XGraphics.FromPdfPage(page);

            // embed fonts into PDF
            gfx.MFEH = PdfFontEmbedding.Always;

            // add Text
            gfx.DrawString("Company Name Project Proposal", fontHeader, XBrushes.Black, new XRect(285, 680, page.Width, page.Height), XStringFormats.TopLeft);
            gfx.DrawString("Schedule A", fontSub, XBrushes.Black, new XRect(285, 780, page.Width, page.Height), XStringFormats.TopLeft);
            gfx.DrawString("testing the paragraph font", fontPar, XBrushes.Black, new XRect(285, 880, page.Width, page.Height), XStringFormats.TopLeft);

            // Save the document...
            const string filename = "PageSizes_tempfile1.pdf";
            document.Save(filename);
            Process.Start(filename);
        }


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 19, 2015 7:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
Hi!

If you want to have the fonts embedded in the document, then specify that.

Code:
// Set font encoding to unicode
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

XFont font = new XFont("Times New Roman", 12, XFontStyle.Regular, options);
This works with both Unicode and PDF encoding.

Note that PostScript fonts are not supported. AFAIK only the WPF build supports OpenType fonts with PostScript outlines - maybe you have to switch to the WPF build.
See also:
http://www.pdfsharp.net/wiki/PostScript_fonts.ashx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 19, 2015 11:42 am 
Offline

Joined: Mon Oct 13, 2014 3:42 pm
Posts: 10
awesome, its working now.
Thank you Thomas.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 152 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group