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

Free Support Forum - aspose.com

Print out a PDF table in C# - Changing font doesn't work for nordic characters

Hi, I’m trying to print out a table with Aspose and I can change the fonts - however, nordic characters are not working.

I assume this is some kind of encoding issue?

However, I am unable to find how to set the encoding to Unicode as the documentation doesn’t imply any of these and the old implementation has been changed.

Here’s my printout and code as well:

internal BaseParagraph Build(PageInfo pageInfo, int repeatedRows)
    {
        using FileStream fontStream = File.OpenRead(@"../Relex.Documents.Aspose/Resources/Fonts/TimesNewRoman.ttf");
        var table = new Table
        {
            RepeatingRowsCount = repeatedRows,
            DefaultCellTextState =
            {
                Font = FontRepository.OpenFont(fontStream, FontTypes.TTF)
            }
        };

        return BuildTable(table, pageInfo);
    }

Screenshot 2021-05-27 at 14.22.24.png (28.6 KB)

@niklasrelex

Can you please share the complete sample code snippet which is generating the PDF output as shared in the image? We will test the scenario in our environment and address it accordingly.

Not really, because the whole service involved is huge. But I can try to share most of the code:

For the initialization/setting up the page:

public Task<MemoryStream> ReadDocument()
    {
        var output = new MemoryStream();
        using (var document = new Document())
        using (Page page = document.Pages.Add())
        {
            SetupPage(page);
            AddTitle(page);
            AddTable(page);
            AddPageNumbers(document);
            document.Save(output);
        }
        return Task.FromResult(output);
    }

    private static void SetupPage(Page page)
    {
        page.PageInfo.Margin = new MarginInfo(20, 20, 20, 20);
        page.PageInfo.IsLandscape = true;
    }

    private void AddTable(Page page)
    {
        var table = new ExpressionTable(_request, _resolver, _tableStyle).Build(page.PageInfo, 1);
        page.Paragraphs.Add(table);
    }

    private void AddTitle(Page page)
    {
        var title = new TextFragment(_request.Title);
        _tableStyle.StyleTableCardTitle(title);
        page.Paragraphs.Add(title);
    }

And then what the AddTable does:

internal BaseParagraph Build(PageInfo pageInfo, int repeatedRows)
        {
            using FileStream fontStream = File.OpenRead(@"../Relex.Documents.Aspose/Resources/Fonts/TimesNewRoman.ttf");
            var table = new Table
            {
                RepeatingRowsCount = repeatedRows,
                DefaultCellTextState =
                {
                    Font = FontRepository.OpenFont(fontStream, FontTypes.TTF)
                }
            };

            return BuildTable(table, pageInfo);
        }

        private BaseParagraph BuildTable(Table table, PageInfo pageInfo)
        {
            List<DataColumn> dataColumns = CreateDataColumns(table).ToList();
            PopulateTable(table, dataColumns);

            table.ColumnWidths = ColumnWidths.Calc(pageInfo, dataColumns);
            dataColumns.ForEach(col => col.ApplyStyles());
            return table;
        }

Populate table etc are just generic things that insert the data. ApplyStyles doesn’t do anything for now.

private void PopulateTable(Table table, List<DataColumn> dataColumns)
    {
        foreach (var requestDataRow in _dataRows)
        {
            var dataRow = table.Rows.Add();
            foreach (var dataColumn in dataColumns)
            {
                String data = _resolver.Resolve(dataColumn, requestDataRow);
                var cell = dataRow.Cells.Add(data);
                dataColumn.AddDataCell(cell, data);
            }
        }
    }
    private IEnumerable<DataColumn> CreateDataColumns(Table table)
    {
        var headers = table.Rows.Add();
        foreach (var requestExpressionColumn in _expressionColumns)
        {
            var cell = headers.Cells.Add(requestExpressionColumn.FriendlyName);
            yield return new DataColumn(cell, requestExpressionColumn, _tableStyle);
        }
    }

I’m guessing this might be an OS related encoding issue, as this is not consistent with every machine.

@niklasrelex

We have tested the scenario in our environment while using Aspose.PDF for .NET 21.5 and did not notice any issue in the output PDF. The text was rendered correctly inside the table with specified font. We performed test in an environment i.e. Windows 10 EN x64. Could you please share the environment details in which you are experiencing this issue so that we can further proceed to assist you accordingly.

PS: Have you tried using a font other than TimesNewRoman.ttf?