Custom fonts and 3rd party hosting - e.g. Adobe Typekit

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.<o:p></o:p>


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 earlier reported issue and following are our observations. Please note that Aspose.Pdf library has a class Aspose.Pdf.Text.FontRepository which helps to get desired font via 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 type of font format. At current moment only ttf format(FontTypes.TTF) is supported.

So to use font which data can be accessed via web link first build a System.IO.Stream object with the font bytes downloaded from web, then pass this stream into function FontRepository.OpenFont(Stream fontStream, FontTypes fontType) with a FontTypes.TTF as a second parameter.
Function FontRepository.OpenFont returns Aspose.Pdf.Text.Font object which can be used in different PDF document’s scenarios.

Code snipped below presents a fictional function which creates PDF document. Resultant document has a passed text in it’s body and uses a font built from passed Stream. Parameter isEmbedded controls would font be embedded into document or not.

[C#]

void CreatePdfWithFontFromStream(Stream
st, bool isEmbedded, string
text)<o:p></o:p>

{<o:p></o:p>

Font font = FontRepository.OpenFont(st,
FontTypes.TTF);<o:p></o:p>

Document doc = new
Document();<o:p></o:p>

Page page = doc.Pages.Add();<o:p></o:p>

TextFragment fragment = new
TextFragment(text);<o:p></o:p>


fragment.TextState.Font = font;<o:p></o:p>


fragment.TextState.Font.IsEmbedded = isEmbedded;<o:p></o:p>


fragment.TextState.FontSize = 14;<o:p></o:p>


page.Paragraphs.Add(fragment);<o:p></o:p>

doc.Save(“FontStreamExample.pdf”);<o:p></o:p>

}