PDFsharp add private/installed font - pdfsharp

I want to apply Trade gothic font to my pdf text using PDFsharp, I have installed the font and use below line of code to apply
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
// var options = new XPdfFontOptions(PdfFontEmbedding.Always);
XFont font = new XFont("TRADE GOTHIC BOLD CONDENSED NO. 20", 20, XFontStyle.Bold, options);
But it does not work!!.
Also I wanted to know in production I'm using Windows server 2008, is there a way I can dynamically add this font in production server even it is not there?
As suggested I followed the pdfsharp forum ,
this is my sample code
XPrivateFontCollection privateFontCollection = XPrivateFontCollection.Global;
// Uri fontUri = new Uri(MappedApplicationPath + "Fonts\\trade-gothic-no-20-condensed-bold-1361518746.ttf");
Uri fontUri = new Uri("C:\\inetpub\\wwwroot\\wss\\VirtualDirectories\\80\\Fonts\\trade-gothic-no-20-condensed-bold-1361518746.ttf");
LoadPrivateFont(privateFontCollection, fontUri, "./#TradeGothicNo.20-Condensed");
I tried all possible combination of path and file name , the name as mentioned in .ttf file but still getting exception . I have a sharepoint Visual webpart, and on page load event of that webpart m writing this code..
This is load method
protected void LoadPrivateFont(PdfSharp.Drawing.XPrivateFontCollection privateFontCollection, Uri fontUri, string sFontFamilyname)
{
try
{
privateFontCollection.Add(fontUri, sFontFamilyname);
}
catch
{
}
}
I have followed this post
http://forum.pdfsharp.net/viewtopic.php?f=2&t=1880
Thanks

When using fonts with PDFsharp, make sure the font is a TrueType font (not a PostScript font).
Also make sure you write the font name correctly - as shown by the Font applet of Windows or as shown by Word.
You can use a private font collection to use fonts that are not installed on the computer. This should solve your "problem" with Windows Server 2008. Use the WPF build of PDFsharp.
The PDFsharp source package includes a full working sample that uses private fonts.
A code snippet can be seen here:
http://pdfsharp.net/wiki/PrivateFonts-sample.ashx

Related

How do I load a custom Font in Xamarin using UrhoSharp?

I am trying to follow the example posted by Adam Pedley at
Introduction to UrhoSharp in Xamarin Forms but have run into a problem. In the following code block, he is loading a custom font:
private void CreateText()
{
// Create Text Element
var text = new Text()
{
Value = "Hello World!",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
text.SetColor(Color.Cyan);
text.SetFont(font: ResourceCache.GetFont("Fonts/Anonymous Pro.ttf"), size: 30);
// Add to UI Root
UI.Root.AddChild(text);
}
The problem I have is that I don't have this font, and it isn't included anywhere in any kind of Assets folder or anything in his GitHub repository of his demo project at adamped/UrhoSharp.Demo. In fact, in spite of the allusion to a "Fonts" folder in the function call, no Fonts folder exists in his demo solution in any of the projects.
Trying to run the application results in a mono runtime error " [ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: Could not find resource...".
In addition, there doesn't seem to be any means of contacting the author to ask (which I find doubly frustrating), so I am having to ask the Stack Overflow community: Where and how do I place custom font assets in my solution (his solution) so that a custom font may be used?
I am currently able to use a custom font in a standard Label with the font file being located in the /Assets folder of both my Sample.Android and Sample.UWP projects, but no combination of path or file name I have tried in the call to GetFont(...) works.
After hours of searching, I finally came across a screen shot showing the proper setup for anyone else who may run into this issue;

Rails4 + PDFKit + Fonts + Heroku not working

I'am trying to solve a font issue on heroku using PDFkit since few days and nothing seem to work for me....
I've already did few solutions such as :
Convert LucidaHW from Squirrel and do as follow ...
#font-face {
font-family: 'lucida_handwritingitalic';
src: url(data:application/x....
or this solution with .font on that website: http://www.mobalean.com/blog/2011/08/02/pdf-generation-and-heroku
or using :
kit.stylesheets << "#{Rails.root.join("public","stylesheets", "pdf", "pdf.css.scss")}"
I also moved my pdf.css.scss into vendor folder, into public folder and nothing ...
Right now, i am a bit confused because almost all of it work in dev mode (on my localhost) but nothing on heroku.
I'am aware this is not the first ticket about that issue but nobody found a solution that worked for me.
I ran into a similar issue a few months back.
This is what I'm doing right now and it has been working great for me:
1.Serve assets in base64:
add this helper method:
def asset_data_base64(path)
asset = Rails.application.assets.find_asset(path)
throw "Could not find asset '#{path}'" if asset.nil?
base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end
and in your template, include assets like this: (the example uses haml)
= stylesheet_link_tag(asset_data_base64('pdf/pdf.css'))
= javascript_include_tag(asset_data_base64('pdf/pdf.js'))
2.custom fonts
move all your fonts to a folder in your project. (I put mine under vendor/assets/fonts) then add a new initializer file in your config/initializers
if Rails.env.production?
font_dir = File.join(Dir.home, ".fonts")
Dir.mkdir(font_dir) unless Dir.exists?(font_dir)
Dir.glob(Rails.root.join("vendor","assets","fonts", "*")).each do |font|
target = File.join(font_dir, File.basename(font))
File.symlink(font, target) unless File.exists?(target)
end
end
replace Rails.root.join("vendor","assets","fonts", "*") withe the path to the folder where you put all your font files.
then in your css, do not use font-face, use the font name directly, e.g. font-family: Gotham;
Also, do not user fallback fonts! In some versions of wkhtmltopdf, it always uses the fallback font if one is provided.
=============================================
I learned the base64 trick from here: https://github.com/mileszs/wicked_pdf/issues/257
Custom fonts solution was found here: http://www.mobalean.com/blog/2011/08/02/pdf-generation-and-heroku
Try putting the font in a .fonts folder in you app root and push it to Heroku - just refer to the font via the font family and not with a src. That's how I use custom fonts on Heroku with wkhtmltopdf and it works for me.

actionscript: embed glyphs by code

you can embed certain glyphs of a font via the Flash IDE.
is this also possible by code only? (without the IDE)
[Embed(source='Fonts/arial.swf'
,fontName='Arial' )]
public static var font:Class;
There is a difference in embedding with the Flex SDK you are using (embedAsCFF propertie).
Flex 4.x SDK
[Embed(source="../lib/CustomFont.ttf", embedAsCFF="false", fontName="CustomFont", mimeType="application/x-font")]
public var CustomFont:Class;
Flex 3.x SDK
[Embed(source="../lib/CustomFont.ttf", fontName="CustomFont", mimeType="application/x-font")]
public var CustomFont:Class;
Note1: Don't forget to set textfield.embedFonts = true
Note2: You cannot use a swf as font, it should be an .TTF or .OTF file format
reference source: http://www.bit-101.com/blog/?p=2555
For Flex3 you can also try Antialiaser: a free AIR application that makes OpenType and TrueType font visualization easy, allowing you to embed certain glyphs and customize all font setting, so you can use them later inside your Flex and AIR projects.
http://www.cycle-it.com/antialiaser/
It's possible, see Using embedded fonts (look for 'unicodeRange').

How to get users try the fonts they want online

I'm trying to get the following done:
I have a set of fonts and I want users to be able to try these fonts online, but i want the result to appear in an image so no one can use the font unless they buy it. I found an example about what I exactly want here. Can anyone tell me how to do it ??
Thanks
The src of the image is a server side page that renders an image:
http://1001freefonts.com/image.php?text=test&font=KatyBerry
So what you want to do is to set the src to an aspx-page with the input text as a querystring. From that you create a graphics object, and use the font to draw the specified text onto that
excerpt from article
private void button4_Click(object sender, System.EventArgs e)
{
Graphics grf = this.CreateGraphics();
try
{
grf.Clear(Color.White);
using (Font myFont = new Font("Arial", 14))
{
grf.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new PointF(10, 100));
}
}
finally
{
grf.Dispose();
}
}
After that, you need to do a Response.Clear() so that you don't get any other text content rendered, and set Response.ContentType = "image/jpeg"; (or whatever content type you'll be using), and then write your image to the response output buffer.
If you don't need to display user-entered text, won't using static images be the KISS solution?
There are a number of solutions to this problem :
render the font on the server - this is probably the best approach and from a copyright pov (if any) the safest. The answer from David outlines an approach.
Render the font on the client. For a busy site this would place less load on your server. There are a number of solutions in javascript, my favourite being Cufon. The browser downloads the cufon runtime as well as a font file and this is rendered by the browser. You need to be aware of font licensing when using this approach, but it does require no setup on the server, and can even be used offline.
You can also use Flash to render the fonts, without licensing issues.

pdfsharp - font embed?

I am given a file that has some other language font. They can not download the font, so they want me to embed it in the pdf.
Now, I only have PDF Reader, so I cant edit or create a pdf file. so I decided to quickly do it in C#.NET using PDFSharp library, but I just cant seem to figure out how to embed fonts using pdfSharp?!
Also, it's only 1 file that I have to process, so if you know of a way to do it manually, then that would be great too.
There are two ways to do this. For each font you want to embed like this:
var options = new XPdfFontOptions(PdfFontEmbedding.Always);
var font = new XFont("Times New Roman", 12, XFontStyle.Regular, options);
Then if you use the font, it will get embedded.
If you want all fonts use on a page to be embedded you can do it like this:
var page = new PdfPage();
var gfx = XGraphics.FromPdfPage(page);
gfx.MFEH = PdfFontEmbedding.Automatic;
The second approach will also work for any fonts used in MigraDoc code.

Resources