Font substitution when adding a Table in PDF


#1

Hi,

We are using Lato font to add text containing Chinese chars in the header of the PDF. Since this font doesn’t have support for this chars, Aspose uses MS Gothic instead.

Is there any way to configure Aspose to use Arial Unicode MS instead?

Best regards!


#2

@gwert

Thank you for contacting support.

You may Set Default Font Name while Saving PDF so that it may be replaced if some font is not present or not supported. However, in case you still face any problem then please share SSCCE code and respective fonts as ZIP files so that we may investigate further to help you out.


#3

Hi,

I gave this suggestion a try and it does not suit the need we have.
The core issue we have is not that a missing font BUT missing support for Chinese characters in LatoLatin-Regular font we use. We just want Aspose to default the font that it uses as a replacement for Lato to be Arial Unicode MS and not MS Gothic.

Here is the SSCCE code FontSubstitution.zip (316.3 KB) and here is the Lato font Lato-Regular.zip (325.7 KB). I cannot upload Arial Unicode MS here but here is the download link https://www.wfonts.com/font/arial-unicode-ms.

Best regards!


#4

@gwert

Thank you for sharing requested data.

We have enabled warnings for font substitution with FontSubstitution event and FontSubstitutionHandler delegate. Would you please try modified code in your environment and then share your kind feedback with us. Program.zip

static void OnFontSubstitution(Aspose.Pdf.Text.Font oldFont, Aspose.Pdf.Text.Font newFont)
{
    System.Console.WriteLine(string.Format("Font '{0}' was substituted with another font '{1}'",
    oldFont.FontName, newFont.FontName));
}

document.FontSubstitution += new Document.FontSubstitutionHandler(OnFontSubstitution);

Please let us know if this helps you achieve your requirements or we will investigate further accordingly.


#5

Hi,

We gave it a try and the only font substitution that happened was:

Font ‘Helvetica’ was substituted with another font ‘Arial’

The code did not try any font substitution for LatoLatin.

The problem with the table header is that the 08 包括打印出来的与 LandscapePortrait text in there is rendered on my local using MS Gothic font (please check the generated Output_19.5.pdf).
All we want is to configure Aspose to render with Arial Unicode MS all chars not supported by LatoLatin Regular. But we do want all text that has only supported chars (like latin ones) to be rendered using LatoLatin.

Could it be that this is not a font substitution scenario?

Best regards.


#6

@gwert

Thank you for your kind feedback.

We have logged an investigation ticket with ID PDFNET-46449 in our issue management system. We will further investigate the scenario and will share our findings with you.


#7

Thank you!

In the mean time, are there any leads I could pursue?

Best regards!


#8

@gwert

We would be able to share further feedback with you once the ticket is investigated in detail. Kindly spare us some time.


#9

Hi,

I see that the status of the issue is Resolved.
Can you please let me know in what version is this going to be released?

Thank you!


#10

@gwert

Thank you for getting back to us.

We are pleased to inform you that PDFNET-46449 has been marked as resolved for Aspose.PDF for .NET 19.8 which will tentatively be released around first week of August 2019.

Once it is released, you will be able to specify ReplacementFont property in textsegment.TextEditOptions. The specified font will be used (if accessible) in the case where specified TextState.Font can not display segment text.

segment.TextEditOptions = new TextEditOptions(TextEditOptions.NoCharacterAction.UseCustomReplacementFont)
{
    ReplacementFont = FontRepository.FindFont("ArialUnicodeMS")
};

We have used the following code for testing:

//open
Document document = new Document(inFile);

//update
for (int i = 1; i <= document.Pages.Count; i++)
{
    Page page = document.Pages[i];

    //page margins need to be updated as well, otherwise the header will be truncated
    page.PageInfo.Margin.Right = 30;
    page.PageInfo.Margin.Left = 30;

    //prepare header of PDF page
    HeaderFooter pageHeader = new HeaderFooter
    {
        Margin = new MarginInfo(30, 0, 30, 20)
    };

    double headerWidth = page.Rect.Width - 60;

    #region Build table in header

    //create table in header
    Table table = new Table
    {
        Left = 0,
        Top = 0,
        ColumnWidths = headerWidth.ToString(System.Globalization.CultureInfo.InvariantCulture),
        DefaultCellBorder =
            new BorderInfo(
                BorderSide.All,
                0.1F,
                Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#d3d3d3"))),
        Margin = new MarginInfo
        {
            Top = 0f,
            Left = 0f,
            Bottom = 0f,
            Right = 0f
        }
    };

    Table rowTable = new Table
    {
        Left = 0,
        Top = 0,
        DefaultCellBorder =
            new BorderInfo(
                BorderSide.Left,
                0.1F,
                Color.FromRgb(System.Drawing.ColorTranslator.FromHtml("#d3d3d3"))),
        DefaultCellPadding = new MarginInfo
        {
            Top = 2f,
            Left = 2f,
            Bottom = 2f,
            Right = 2f
        }
    };

    Row row = rowTable.Rows.Add();

    TextSegment segment = new TextSegment("08 包括打印出来的与 LandscapePortrait")
    {
        TextState = new TextState
        {
            FontStyle = FontStyles.Regular,
            FontSize = 12,
            Font = FontRepository.FindFont("Times New Roman")
        }
    };

    double cellWidth = segment.Rectangle.Width + 5;

    rowTable.ColumnWidths = $"{(int)(headerWidth - cellWidth)} {(int)cellWidth}";

    Cell cell1 = row.Cells.Add();
    cell1.Alignment = HorizontalAlignment.Center;

    TextState textState = new TextState
    {
        FontSize = 12,
        HorizontalAlignment = cell1.Alignment,
        Font = FontRepository.FindFont("Times New Roman")
    };

    TextFragment textFragment1 = new TextFragment();
    textFragment1.Segments.Add(new TextSegment("Cell 1 data in Latin chars")
    {
        TextState = textState
    });
    cell1.Paragraphs.Add(textFragment1);

    Cell cell2 = row.Cells.Add();
    cell2.Alignment = HorizontalAlignment.Center;

    TextFragment textFragment2 = new TextFragment();
    segment = new TextSegment("08 包括打印出来的与 LandscapePortrait")
    {
        TextState = textState
    };
    segment.TextEditOptions = new TextEditOptions(TextEditOptions.NoCharacterAction.UseCustomReplacementFont)
    {
        ReplacementFont = FontRepository.FindFont("ArialUnicodeMS")
    };
    textFragment2.Segments.Add(segment);
    cell2.Paragraphs.Add(textFragment2);

    table.Rows.Add().Cells.Add().Paragraphs.Add(rowTable);

    #endregion

    pageHeader.Paragraphs.Add(table);

    page.Header = pageHeader;

    page.FreeMemory();
}

document.Save(outFile);

We have also attached generated PDF document for your kind reference 46449_fixed.pdf. We will notify you as soon as upcoming version will be released.