Setting embedded font on table

(licensed Aspose.PDF 19.1.0)
I’m trying to create a table in my pdf document using a font that already embedded in the document. Doing so gives an “Index was outside the bounds of the array” error somewhere inside TextState. Can you please help explain what’s going on here?

Example code below. I’m attaching the PDF.

using (var stream = File.OpenRead(@"c:\temp\articles_euro.pdf"))
{
	var instance = new Aspose.Pdf.Document(stream);
	var fonts = instance.FontUtilities.GetAllFonts();

	var table = new Table() { DefaultCellTextState = new TextState() { Font = fonts[0] } };
	for (int row_count = 1; row_count < 5; row_count++)
	{
		Aspose.Pdf.Row row = table.Rows.Add();
		row.Cells.Add("Column (" + row_count + ", 1)");
		row.Cells.Add("Column (" + row_count + ", 2)");
		row.Cells.Add("Column (" + row_count + ", 3)");
	}

	instance.Pages[1].Paragraphs.Add(table);

    instance.Save(@"c:\temp\test.pdf", SaveFormat.Pdf);
}

articles_euro.pdf (61.6 KB)

@MartinLj

Thank you for contacting support.

Please note that GetAllFonts method returns an array of Font type which includes information like the Name of font and other properties. Whereas, the TextState object requires the font file either from installed font directory or from custom location. Therefore, you may try to replace below line of code for loading that font, provided it is already installed in your environment.

var table = new Table() { DefaultCellTextState = new TextState() { Font = FontRepository.FindFont(fonts[0].FontName) } };

In case some font is not installed, then you may try to load it:

TextState state = new TextState();
state.Font = FontRepository.OpenFont(dataDir + "StagSans-Semibold.ttf");
table.DefaultCellTextState = state;

StagSans-Semibold.zip

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Ok, thanks. So this means that it’s impossible to use a font for my table if it’s only embedded in the pdf? It has to be available on the machine (either in a folder or installed)?

Seems counter-intuitive - the font is after all embedded, packaged with the pdf. Why can’t I then use it in the pdf?

I found you can do OpenFont with a stream too, so for now I’m including font-files in my app (.dll) as embedded resources, which works fine.

@MartinLj

Thank you for your kind feedback.

We are glad to know that things are working fine in your environment. Moreover, we have recorded your comments and a ticket with ID PDFNET-45983 has been logged in our issue management system for further investigations. We will let you know as soon as some significant updates will be available.