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

Private Fonts with MigraDoc (VB.NET)
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2649
Page 1 of 1

Author:  ToadRage [ Thu Nov 14, 2013 8:15 pm ]
Post subject:  Private Fonts with MigraDoc (VB.NET)

Hello!
I'm new to PDFSharp and MigraDoc (Just started a few days ago) and I'm having a hell of a time trying to figure out how to use private fonts with MigraDoc. I have scoured this forum and Google for the last day and cannot seem to piece together a solution for this conundrum.

Problem:
I have a Windows Forms Application written in vb.net that I am deploying via ClickOnce. This form allow the end user to print reports to PDF (via MigraDoc) that contain a barcode font called “Free 3 of 9 Extended” (The font file is included in the Application.StartupPath and is named “FRE3OF9X.TTF”) . Unfortunately ClickOnce cannot install fonts as part of its deployment process so I am looking at using the private font collection.

I have already looked at the following resources and can't figure out how to make these suggestions work within the MigraDoc objects:

viewtopic.php?f=2&t=1880

http://www.pdfsharp.net/wiki/PrivateFonts-sample.ashx

Info
MigraDocs - WPF - Version 1.32

Objective:
My objective is to use the barcode font without installing it to the pc via a private font collection. Then have that font recognized within MigraDoc.
For example: Paragraph.Format.Font.Name = "Free 3 of 9 Extended"


For the purposes of this thread I created a simple form that contains only a button called Button1. Any idea how I can get this to work?

Code:
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.DocumentObjectModel.Tables
Imports MigraDoc.Rendering
Imports PdfSharp.Pdf
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Creates the document object
        Dim document As New Document()
        document.DefaultPageSetup.PageFormat = PageFormat.A4
        document.DefaultPageSetup.PageHeight = "11in"
        document.DefaultPageSetup.PageWidth = "8.5in"

        document.Info.Title = "Barcodes"
        document.DefaultPageSetup.RightMargin = ".21975in"
        document.DefaultPageSetup.LeftMargin = ".21975in"
        document.DefaultPageSetup.TopMargin = ".5in"
        document.DefaultPageSetup.BottomMargin = ".5in"

        'Adds a section to the document
        Dim section As Section = document.AddSection()

        'Add a barcode to the document
        Dim bc As Paragraph = section.AddParagraph("*12345*")

        'Here is is the problem. How do I get this line to work with a private font collection?
        bc.Format.Font.Name = "Free 3 of 9 Extended"

        bc.Format.Font.Size = "20pt"

        'Render the pdf document
        Dim pdfRenderer As New PdfDocumentRenderer(False, PdfFontEmbedding.Always)
        pdfRenderer.Document = document
        pdfRenderer.RenderDocument()

        'Save the pdf document to their "My Documents" folder
        Dim filename As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Test.pdf"

        pdfRenderer.PdfDocument.Save(filename)
        Process.Start(filename)
    End Sub
End Class


Attachments:
File comment: Barcode font attached
FRE3OF9X.zip [1.82 KiB]
Downloaded 626 times

Author:  ToadRage [ Fri Nov 15, 2013 1:45 pm ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

Is this correctly formatted?

Code:
        Dim pfc As New XPrivateFontCollection()
        Dim uri As New Uri("file:///" + Application.StartupPath + "/FRE3OF9X.TTF")
        pfc.Add(uri, "#Free 3 of 9 Extended")


If so, how can I get it to work with format.font.name since it only accepts a string?:

Code:
bc.Format.Font.Name = "Free 3 of 9 Extended"

Author:  () => true [ Sat Nov 16, 2013 5:55 pm ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

ToadRage wrote:
Is this correctly formatted?
Do you get error messages when you run the code?

The source package of PDFsharp includes the complete source code of a working C# sample.
So you can try making a VB sample that works using code and fonts from the C# sample.
Or try to make the C# sample work with your font.
One step at a time.

Then finally try your font with your VB code. Then try it with Click Once.

The C# takes the font from a resource. This should also work with Click Once (well, I hope it does).

Author:  ToadRage [ Mon Nov 18, 2013 4:35 am ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

() => true wrote:
ToadRage wrote:
Is this correctly formatted?
Do you get error messages when you run the code?

The source package of PDFsharp includes the complete source code of a working C# sample.
So you can try making a VB sample that works using code and fonts from the C# sample.
Or try to make the C# sample work with your font.
One step at a time.

Then finally try your font with your VB code. Then try it with Click Once.

The C# takes the font from a resource. This should also work with Click Once (well, I hope it does).


The problem is that I am trying to do this with MigraDoc not PDFSharp and the Paragraph.Format.Font.Name only accepts a string value. How do you specify private fonts with MigraDoc when it only accepts a string instead of a font object like Xfont?

Author:  Thomas Hoevel [ Mon Nov 18, 2013 10:30 am ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

ToadRage wrote:
How do you specify private fonts with MigraDoc when it only accepts a string instead of a font object like Xfont?
The font has a name and you specify the font with its name.

With PDFsharp you also specify the font name to get the XFont object.

Author:  ToadRage [ Mon Nov 18, 2013 3:45 pm ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

Thank you both for your replies. I am still trying to wrap my head around how this is supposed to work when MigraDoc only accepts a font name string. The code below gives this error:

Image

Code:
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.DocumentObjectModel.Tables
Imports MigraDoc.Rendering
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' Load in a private font to use.
        Dim pfc As New XPrivateFontCollection()
        Dim uri As New Uri("file:///" + Application.StartupPath + "/FRE3OF9X.TTF")
        pfc.Add(uri, "#Free 3 of 9 Extended")


        'Creates the document object
        Dim document As New Document()
        document.DefaultPageSetup.PageFormat = PageFormat.A4
        document.DefaultPageSetup.PageHeight = "11in"
        document.DefaultPageSetup.PageWidth = "8.5in"

        document.Info.Title = "Barcodes"
        document.DefaultPageSetup.RightMargin = ".21975in"
        document.DefaultPageSetup.LeftMargin = ".21975in"
        document.DefaultPageSetup.TopMargin = ".5in"
        document.DefaultPageSetup.BottomMargin = ".5in"

        'Adds a section to the document
        Dim section As Section = document.AddSection()

        'Add a barcode to the document
        Dim bc As Paragraph = section.AddParagraph("*12345*")

        'Here is is the problem. How do I get this line to work with a private font collection?
        bc.Format.Font.Name = "Free 3 of 9 Extended"


        bc.Format.Font.Size = "20pt"

        'Render the pdf document
        Dim pdfRenderer As New PdfDocumentRenderer(False, PdfFontEmbedding.Always)
        pdfRenderer.Document = document
        pdfRenderer.RenderDocument()

        'Save the pdf document to their "My Documents" folder
        Dim filename As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Test.pdf"

        pdfRenderer.PdfDocument.Save(filename)
        Process.Start(filename)
    End Sub

End Class


How do I specify it use the XPrivateFontCollection collection font?

Author:  Thomas Hoevel [ Mon Nov 18, 2013 4:11 pm ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

With the current PDFsharp version, there is a single, global XPrivateFontCollection.

In C#, you use code like this:
Code:
XPrivateFontCollection globalFontCollection = XPrivateFontCollection.Global;


So instead of
Code:
Dim pfc As New XPrivateFontCollection()

try something like
Code:
Dim pfc As XPrivateFontCollection
pfc = XPrivateFontCollection.Global


If it still does not work, check the names of the fonts that have been added to the pfc.
And make sure you use the WPF build of MigraDoc.

Author:  ToadRage [ Mon Nov 18, 2013 5:36 pm ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

Ok... Well something different is happening now, but not sure what. When I run project after setting pfc = XPrivateFontCollection.Global I don't get an error immediately. Instead it returns to Visual Studio and noting is shown in the error/warnings window. After clicking the "Continue" button 8 times the same error eventually comes up. I am using WPF 1.32 and the font name seem to be correct. I have spent so long trying to figure this thing out and it's probably something simple...

Here is a link to the Visual Studio Solution: (I tried attaching it, but the file would not upload for some reason)
https://dl.dropboxusercontent.com/u/477 ... ection.zip

Author:  ToadRage [ Mon Nov 18, 2013 5:57 pm ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

Ahhhh I feel like a bloody fool! It was something simple! It was missing the "./" from the font name parameter pfc.Add(uri, "./#Free 3 of 9 Extended")

Here is the working code incase someone else is looking for this:

Code:
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.DocumentObjectModel.Tables
Imports MigraDoc.Rendering
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Load in a private font to use.
        Dim pfc As XPrivateFontCollection
        pfc = XPrivateFontCollection.Global

        Dim uri As New Uri("file:///" & Application.StartupPath & "\FRE3OF9X.TTF")
        pfc.Add(uri, "./#Free 3 of 9 Extended")

        'Creates the document object
        Dim document As New Document()
        document.DefaultPageSetup.PageFormat = PageFormat.A4
        document.DefaultPageSetup.PageHeight = "11in"
        document.DefaultPageSetup.PageWidth = "8.5in"

        document.Info.Title = "Barcodes"
        document.DefaultPageSetup.RightMargin = ".21975in"
        document.DefaultPageSetup.LeftMargin = ".21975in"
        document.DefaultPageSetup.TopMargin = ".5in"
        document.DefaultPageSetup.BottomMargin = ".5in"

        'Adds a section to the document
        Dim section As Section = document.AddSection()

        'Add a barcode to the document
        Dim bc As Paragraph = section.AddParagraph("*")

        'Here is is the problem. How do I get this line to work with a private font collection?
        bc.Format.Font.Name = "Free 3 of 9 Extended"


        bc.Format.Font.Size = "20pt"

        'Render the pdf document
        Dim pdfRenderer As New PdfDocumentRenderer(False, PdfFontEmbedding.Always)
        pdfRenderer.Document = document
        pdfRenderer.RenderDocument()

        'Save the pdf document to their "My Documents" folder
        Dim filename As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Test.pdf"

        pdfRenderer.PdfDocument.Save(filename)
        Process.Start(filename)
    End Sub
End Class



Thank you both for your help! I am so happy to finally see this working. I look forward to learning more about PDFSharp and MigraDoc

Author:  arronlee [ Mon May 26, 2014 8:45 am ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

Hi, Have you ever found out any solution?

Author:  Thomas Hoevel [ Mon May 26, 2014 8:52 am ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

arronlee wrote:
Hi, Have you ever found out any solution?
Did you read the last post above yours?

Author:  lucywill [ Mon May 26, 2014 10:05 am ]
Post subject:  Re: Private Fonts with MigraDoc (VB.NET)

i use this method to add a barcode to the pdf document and it supports many barcode types include both 1D and 2D barcodes.

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