Loading TTF fonts from a resources folder

Hi,


From reading through various posts I think I know the answer to this however I just want clarification.

I am trying to use the Tahoma font when generating a PDF, however I’d preferably like to be able to just provide a path to the file which is kept within a resources folder for my application e.g. /resources/Tahoma.ttf. Is this possible?

I’ve also tried installing the font into my system -> Fonts and then doing:

final Section sec1 = pdf1.getSections().add();

sec1.getTextInfo().setFontName("Tahoma");

but this causes an error: Font 'Tahoma' not found. Please make sure the customer font file is correctly set.

Any ideas? I can use the core Aspose fonts fine, so I am making do with Helvtica for now, but I could really do with being able to use Tahoma!

Thanks,


Chris

Hi Chris,


Thanks for your inquiry. We are looking into your query and will get back to you soon. Meanwhile, can you please share some details about your working environment and Aspose.Pdf API version. It would help us to test your scenario.

We are sorry for the inconvenience caused.

Best Regards,

Hi,


I am using a Mac on Mountain Lion OSX and Aspose 4.6.

Thanks,

Chris

Hi Chris,

Thanks for sharing the details.

I have tested the scenario and I am able to notice the same problem. For the sake of correction, I have logged this problem as PDFNEWJAVA-34127 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 a little time. We are sorry for this inconvenience.

This is for .NET/C#/Windows but it should be close for Java or whatnot.


//create the font from a ttf file
Aspose.Pdf.Text.Font myFont =
Aspose.Pdf.Text.FontRepository.OpenFont("C:\temp\MyFont-Regular.ttf");
myFont.IsEmbedded = true;

//new doc, add a new blank page
Document doc = new Document();
Page page = doc.Pages.Add();

//use the font in a style
TextState style = new TextState();
style.Font = myFont;
style.FontSize = 12;
style.FontStyle = FontStyles.Regular;
style.LineSpacing = 4;

TextFragment frag = new TextFragment(sb.ToString());
frag.TextState.ApplyChangesFrom(style);
frag.IsInLineParagraph = true;
page.Paragraphs.Add(frag);

//save it
doc.Save("C:\temp\fontTest.pdf");

Hi Dirk,

Thanks for sharing the code snippet. Yes you are correct. The following code can be used with Aspose.Pdf for Java to use a custom font file (even if it’s not installed over the system).

//create the font from a TTF file
com.aspose.pdf.Font myFont = new com.aspose.pdf.FontRepository().openFont("E:\\Package\\fonts\\3OF9_NEW.TTF");
myFont.setEmbedded(true);

//new doc, add a new blank page
com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
com.aspose.pdf.Page page = doc.getPages().add();

//use the font in a style
com.aspose.pdf.TextState style = new com.aspose.pdf.TextState();
style.setFont(myFont);
style.setFontSize(12);
style.setFontStyle(com.aspose.pdf.FontStyles.Regular);
style.setLineSpacing(4);

com.aspose.pdf.TextFragment frag = new com.aspose.pdf.TextFragment("This is sample string");
frag.getTextState().applyChangesFrom(style);
frag.setInLineParagraph(true);
page.getParagraphs().add(frag);

//save it
doc.save("C:\\pdftest\\CustomerFont_output.pdf");