We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Using custom font from custom folder in generator.pdf

Hi,

I have a problem with specifying non-default font folder when creating pdf using generator.
The scenario is following:
I have custom folder that contains true type fonts.
I want to create pdf from a plain tex file with one of these fonts.
Class TextInfo allows me to set FontName ... this is ok, I know it.
But the font that is not in default (system folder) is not found and used.
I can also specify the TruetypeFontFileName which partially solves this issue.

The problem is that I dont know the font file name.

The FontRepository does not provide me useful functionality for this,
becase found font it does not contain file name or path.

Sample:

Pdf pdf = new Pdf();
var section = pdf.Sections.Add();
var text = new Text(File.ReadAllText(inputFile, Encoding.UTF8));
text.TextInfo.FontName = plainTextFont;
text.TextInfo.TruetypeFontFileName = Path.Combine(fontFolder, fontFile);
section.Paragraphs.Add(text);
pdf.SetUnicode();
pdf.Save(outputFile);

I need to find convinient way to do this without knowing fontFile.

Thanks for your answer.
Ondrej Boruvka, Y Soft

Hi Ondrej,


Thanks for contacting support.

In order to use the font while generating PDF file, you need to specify the font name and as you have specified above, the TruetypeFontFileName property can be used to set the path to font file name which is not installed over system. Without specifying the font name, the API might not be certain about the font which can be used form particular folder.

In case I have not properly understood your requirement, please share some further details.

Hi,


what I wanted was the functionality that helps me to find font file name … to provide it to generator as you wrote. I have the folder with bunch of fonts and I need to find file name of particular font.

I am able to do it via .Net native classes, but it is unconfortable and it woudl be great, if Aspose.pdf could provide these kind of functionality.

Best regards,
Ondra

Hi Ondra,


Thanks for sharing the details.

<span style=“font-size:10.0pt;font-family:“Arial”,“sans-serif””>I am afraid the requested feature is currently not supported. For
the sake of correction, I have logged it in our issue tracking system as PDFNEWNET-36819 under new features list. We will
investigate this issue in details and will keep you updated on the status of a
correction. <o:p></o:p>

We apologize for your inconvenience.



Hi Ondra,


Thanks for your patience.

We have further investigated the earlier reported issue and we suggest using new generator because there is no need of properties like TruetypeFontFileName and no need to “know the font file name”. Furthermore, If you need to use special font from non-system folder, you may consider using any of the overloaded methods FontRepository.OpenFont() and specify desired font name. Also if you do not know the font file name and know the font name, you can use this font name to find necessary font with a help of FontRepository.FindFont(). Nevertheless, first you need to point to folder containing custom fonts. This can be done using following code snippet:

FolderFontSource fs = new FolderFontSource(myDir);<o:p></o:p>

FontRepository.Sources.Add(fs);

Aspose.Pdf.Text.Font font = FontRepository.FindFont(desiredFontName);

this will return desired font from non-system directory. If name of custom font conflicts with name of some font in system directory, in such cases, it's necessary to only use custom directory for FontRepository class. This can be done with a following code:

FontRepository.Sources.Clear();

FolderFontSource fs = new FolderFontSource(myDir);

FontRepository.Sources.Add(fs);

Therefore we need to use fonts from different places,system directory and custom directories, in order to avoid conflicts with the same font names, can be achieved by means of property FontRepository.Sources(FontSourceCollection object). Just be clear and add necessary folders into this collection appropriately.

The following example specified below creates a new PDF file with a text from 2 files: Sample.txt - text from this file must use font vFrutiger-Normal from custom folder, VerdanaText.txt - text from this file must use font Verdana from system folder.

[C#]

Document doc = new Document();

Page page = doc.Pages.Add();

TextFragment text = ReadText(myDir + "Sample.txt");

//Disconnect system font directory to exclude conflicts with a system font Frutiger if it exists

FontRepository.Sources.Clear();

//Connect custom font directory

FolderFontSource fs = new FolderFontSource(myDir + @"\Fonts");

FontRepository.Sources.Add(fs);

text.TextState.Font = FontRepository.FindFont("Frutiger-Normal");

text.TextState.FontSize = 14;

page.Paragraphs.Add(text);

text = ReadText(myDir + "VerdanaText.txt");

//Here we must use system font Verdana, so to void font name conflicts we connect only system directory to FontRepository.Sources

FontRepository.Sources.Clear();

SystemFontSource systemSource = new SystemFontSource();

FontRepository.Sources.Add(systemSource);

text.TextState.Font = FontRepository.FindFont("Verdana");

text.TextState.FontSize = 14;

page.Paragraphs.Add(text);

doc.Save(myDir + "out.pdf");

private TextFragment ReadText(string fileName)

{

TextFragment text;

TextReader tr = new StreamReader(fileName);

using (tr)

{

text = new TextFragment(tr.ReadToEnd());

}

return text;

}