PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 30, 2024 2:17 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Thu Nov 14, 2013 8:15 pm 
Offline

Joined: Thu Nov 14, 2013 5:37 pm
Posts: 6
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 625 times
Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 15, 2013 1:45 pm 
Offline

Joined: Thu Nov 14, 2013 5:37 pm
Posts: 6
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"


Top
 Profile  
Reply with quote  
PostPosted: Sat Nov 16, 2013 5:55 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 343
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).

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 18, 2013 4:35 am 
Offline

Joined: Thu Nov 14, 2013 5:37 pm
Posts: 6
() => 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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 18, 2013 10:30 am 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 18, 2013 3:45 pm 
Offline

Joined: Thu Nov 14, 2013 5:37 pm
Posts: 6
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 18, 2013 4:11 pm 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 18, 2013 5:36 pm 
Offline

Joined: Thu Nov 14, 2013 5:37 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 18, 2013 5:57 pm 
Offline

Joined: Thu Nov 14, 2013 5:37 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Mon May 26, 2014 8:45 am 
Offline

Joined: Mon May 26, 2014 8:38 am
Posts: 3
Hi, Have you ever found out any solution?


Top
 Profile  
Reply with quote  
PostPosted: Mon May 26, 2014 8:52 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
arronlee wrote:
Hi, Have you ever found out any solution?
Did you read the last post above yours?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon May 26, 2014 10:05 am 
Offline

Joined: Mon May 26, 2014 10:01 am
Posts: 1
i use this method to add a barcode to the pdf document and it supports many barcode types include both 1D and 2D barcodes.


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

All times are UTC


Who is online

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