I was wondering if it is possible to use fonts that are hosted by a 3rd party as part of Aspose PDF generation?
In our case we’re using an Adobe Typekit font that is hosted by Adobe: i.e. we can’t just download a .ttf file and install it on our server. This is just their business model - we pay for use, but they hold on to the font and we link to it on our site - probably to avoid piracy/misuse(?).
Has anyone else had any experience of using 3rd party custom fonts with AsposePDF and come up with a workaround at all? We’re using Aspose as part of a C# .NET solution, by the way.
Any suggests most appreciated, thanks for your time!
Will
Hi Will,
Thanks for using our products.
Currently Aspose.Pdf for .NET supports the feature to reference/use font installed over system or present in particular directory over the system where PDF file is being generated. However currently it does not support the feature to reference/use the font hosted by third party. For the sake of implementation, I have logged this issue as PDFNEWNET-35683 in our issue tracking system.
We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.
Hi Will,
Thanks for your patience.
We have further investigated the earlier reported issue and following are our observations. Please note that the Aspose.Pdf library has a class Aspose.Pdf.Text.FontRepository
which helps to get the desired font via a set of member functions.
There is a function FontRepository.OpenFont(Stream fontStream, FontTypes fontType)
which can be used for our case. It takes 2 parameters - stream where font bytes must be and the type of font format. Currently, only the ttf format (FontTypes.TTF)
is supported.
So to use a font which data can be accessed via a web link, first, build a System.IO.Stream
object with the font bytes downloaded from the web, then pass this stream into the function FontRepository.OpenFont(Stream fontStream, FontTypes fontType)
with FontTypes.TTF
as the second parameter.
The function FontRepository.OpenFont
returns an Aspose.Pdf.Text.Font
object which can be used in different PDF document scenarios.
Code snippet below presents a fictional function which creates a PDF document. The resulting document has the passed text in its body and uses a font built from the passed Stream. The parameter isEmbedded
controls whether the font would be embedded into the document or not.
void CreatePdfWithFontFromStream(Stream st, bool isEmbedded, string text)
{
Font font = FontRepository.OpenFont(st, FontTypes.TTF);
Document doc = new Document();
Page page = doc.Pages.Add();
TextFragment fragment = new TextFragment(text);
fragment.TextState.Font = font;
fragment.TextState.Font.IsEmbedded = isEmbedded;
fragment.TextState.FontSize = 14;
page.Paragraphs.Add(fragment);
doc.Save("FontStreamExample.pdf");
}