How does Excel file with uninstalled fonts is converted to PDF/Image?

23 Nov 20217 minutes to read

When the fonts used in particular Excel document are not installed in the machine, the desired characters will be missing in the PDF/Image conversion. However, XlsIO comes up with a font substitution method through SubstituteFontEventHandler event. This will enable user to specify alternate font name to render the characters in the specified alternate font. Otherwise, Microsoft Sans Serif is used as the default one.

NOTE

Due to this font substitution, there might be a slight difference with the rendered text in the generated PDF/Image files during Excel to PDF/Image conversion.

The following code snippet shows how to use font substitution in Excel to PDF conversion using XlsIO.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");

  //Initializes the SubstituteFont event to perform font substitution during Excel to PDF conversion
  application.SubstituteFont += new SubstituteFontEventHandler(SubstituteFont);

  ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
  PdfDocument pdf = converter.Convert();

  Stream stream = File.Create("Output.pdf");
  pdf.Save(stream);
}

private static void SubstituteFont(object sender, SubstituteFontEventArgs args)
{
  //Sets the alternate font when a specified font is not installed in the production environment
  if (args.OriginalFontName == "Wingdings Regular")
	args.AlternateFontName = "Bauhaus 93";
  else
	args.AlternateFontName = "Times New Roman";
}
Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")

  'Initializes the SubstituteFont event to perform font substitution during Excel to PDF conversion
  AddHandler application.SubstituteFont, AddressOf SubstituteFont

  Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
  Dim pdf As PdfDocument = converter.Convert()

  Dim stream As Stream = File.Create("Output.pdf")
  pdf.Save(stream)
End Using

Private Shared Sub SubstituteFont(ByVal sender As Object, ByVal args As SubstituteFontEventArgs)
  'Sets the alternate font when a specified font is not installed in the production environment.
  If args.OriginalFontName = "Wingdings Regular" Then
	args.AlternateFontName = "Bauhaus 93"
  Else
	args.AlternateFontName = "Times New Roman"
  End If
End Sub
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    FileStream fileStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(fileStream);

    //Initializes the SubstituteFont event to perform font substitution during Excel to PDF conversion
    application.SubstituteFont += new SubstituteFontEventHandler(SubstituteFont);

    XlsIORenderer renderer = new XlsIORenderer();
    PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

    FileStream stream = new FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
    pdfDocument.Save(stream);
}

private static void SubstituteFont(object sender, SubstituteFontEventArgs args)
{
    //Sets the alternate font when a specified font is not installed in the production environment
    if (args.OriginalFontName == "Wingdings Regular")
        args.AlternateFontName = "Bauhaus 93";
    else
        args.AlternateFontName = "Times New Roman";
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    FileStream fileStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(fileStream);

    //Initializes the SubstituteFont event to perform font substitution during Excel to PDF conversion
    application.SubstituteFont += new SubstituteFontEventHandler(SubstituteFont);

    XlsIORenderer renderer = new XlsIORenderer();
    PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

    FileStream stream = new FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
    pdfDocument.Save(stream);
}

private static void SubstituteFont(object sender, SubstituteFontEventArgs args)
{
    //Sets the alternate font when a specified font is not installed in the production environment
    if (args.OriginalFontName == "Wingdings Regular")
        args.AlternateFontName = "Bauhaus 93";
    else
        args.AlternateFontName = "Times New Roman";
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    Assembly assembly = typeof(App).GetTypeInfo().Assembly;
    Stream fileStream = assembly.GetManifestResourceStream("App.Sample.xlsx");
    IWorkbook workbook = application.Workbooks.Open(fileStream);

    //Initializes the SubstituteFont event to perform font substitution during Excel to PDF conversion
    application.SubstituteFont += new SubstituteFontEventHandler(SubstituteFont);

    XlsIORenderer renderer = new XlsIORenderer();
    PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

    MemoryStream stream = new MemoryStream();
    pdfDocument.Save(stream);

    stream.Position = 0;

    //Save the stream as a file in the device and invoke it for viewing
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf", "application/pdf", stream);
}

private static void SubstituteFont(object sender, SubstituteFontEventArgs args)
{
    //Sets the alternate font when a specified font is not installed in the production environment
    if (args.OriginalFontName == "Wingdings Regular")
        args.AlternateFontName = "Bauhaus 93";
    else
        args.AlternateFontName = "Times New Roman";
}

See Also