Rendering Issue for accents characters in PDF document

Hi,

I am trying to convert a word document to a PDF document and that word document has some accents characters. If any word or any sentence has any  accent characters then the whole word or sentence is rendered as a Bold word or sentence. It should be the same as mentioned in the word document.

But I am facing this issue in Azure only. In my local machine, It is working fine. We are using Azure Kubernetes Service (AKS). The operating system of Container is Linux version -  standard D8s_v3


We are using the below code for converting Word documents to PDF.

Package Information -

Syncfusion.DocIO.Net.Core (version - 19.2.0.62)

Syncfusion.DocIORenderer.Net.Core( version - 19.2.0.62)

Syncfusion.Pdf.Net.Core( version - 19.2.0.62)


FileStream docStream = new FileStream(@"Template.docx", FileMode.Open, FileAccess.Read);

//Loads file stream into Word document

WordDocument wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic);

//Instantiation of DocIORenderer for Word to PDF conversion

DocIORenderer render = new DocIORenderer();

//Converts Word document into PDF document

PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);

//Releases all resources used by the Word document and DocIO Renderer objects

render.Dispose();

wordDocument.Dispose();

//Save the document into stream

MemoryStream stream = new MemoryStream();

pdfDocument.Save(stream);

stream.Position = 0;

//Close the documents

pdfDocument.Close(true);

//Defining the ContentType for pdf file

string contentType = "application/pdf";

//Define the file name

string fileName = " WordtoPDF.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name

return File(stream, contentType, fileName);

I have attached word documents and PDF documents also.

Please help me with this.

Thank you in Advance


Attachment: DocAndPDF_14693b12.zip

7 Replies

LB Lokesh Baskar Syncfusion Team November 26, 2021 07:53 AM UTC

Hi Sumit, 
Thank you for contacting Syncfusion support.

On further analyzing, we have found that fonts (Calibri font) used in the Word document is missed in your Azure environment. So that the reported issue occurred at your side when rendering the text. Could you please ensure that all the fonts used in the Word document are available in the environment during Word to PDF conversion. Using
SubstituteFont event, you can identify the missed font in production environment during Word to PDF conversion. Please refer here to know more information about this.

Please refer the below KB to know about what happens when the Word document used fonts not in production machine:

https://www.syncfusion.com/kb/7570/what-happens-when-the-word-document-used-fonts-for-a-text-is-not-installed-in-production

If the fonts used in the Word document is not available in your environment  means, please sets the required font or sets the alternate font (same font file *.ttf or alternate font) using the code snippets in the below link:

https://help.syncfusion.com/file-formats/docio/word-to-pdf?cs-save-lang=1&cs-lang=csharp#event-handler-to-use-alternate-font-without-installing

 
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args) 
{ 
//Sets the required font or alternate font when a specified font is not in the production environment. 
if (args.OrignalFontName == "Calibri" && args.FontStyle == FontStyle.Regular) 
args.AlternateFontStream =  new FileStream("calibri.ttf" ,FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
} 
 


Please let us know if you have any other questions.

Regards, 
Lokesh B 



SM Sumit Manna November 26, 2021 09:49 AM UTC

Hi Lokesh,


Thank you for replying.


We have checked with other fonts also like Times New Roman, Arial which are available in the Azure environment. We are facing the same issue. 

If you see the attached word document all words in the document are Calibri font only like 'Test', 'Dirección', 'N° de colegiado'. In converted PDF document only  'Dirección', 'N° de colegiado' are becoming BOLD, and other words are fine like 'Test'. If it is an unavailability font issue then it should apply to all other words also. 

While we are using assent character like  ó, , í ​that time we are facing this issue.​​


Thank you in advance.


Thanks& Regards,

Sumit



LB Lokesh Baskar Syncfusion Team November 29, 2021 07:30 AM UTC

Hi Sumit,  
Thank you for your update.

As mentioned earlier we need to substitute the Calibri font which is present in the input Word document then only we get the similar output PDF as like in the input document. Please refer to the below code snippet.
 
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)  
 
//Sets the required font or alternate font when a specified font is not in the production environment.  
if (args.OrignalFontName == "Calibri" && args.FontStyle == FontStyle.Regular)  
args.AlternateFontStream =  new FileStream("calibri.ttf" ,FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
 
  

Please refer to the below KB documentation for an example of substituting the font in the azure environment.
https://www.syncfusion.com/kb/11888/how-to-convert-word-document-to-pdf-in-azure-app-service-on-linux

Could you please try the above code snippet in the AKS environment and 
let us know whether it resolves your problem. If still facing issue means then could you please share the simple issue reproducible sample?

Please let us know if you have any other questions.

Regards, 

Lokesh B 
 
 



SM Sumit Manna December 8, 2021 07:14 AM UTC

Hi Lokesh,


Thank you for sharing this information.

We have implemented mentioned code changes in our application. But we are facing another issue after these changes in the rendered PDF file. In our document BOLD character is mentioned but those characters are not shown as BOLD. It is shown in Regular font in rendered PDF file.


Requesting you to please help us with this.


We are using the below code snippet.


public string CovertPdfStreamToBase64(WordDocument document)

        {


            document.FontSettings.SubstituteFont += FontSettings_SubstituteFont;

            MemoryStream ms = new MemoryStream();

            DocIORenderer render = new DocIORenderer();

            PdfDocument pdfDocument = render.ConvertToPDF(document);

            document.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;

            render.Dispose();

            pdfDocument.Save(ms);

            pdfDocument.Close();

            return StreamToBase64String(ms);


        }


        public string StreamToBase64String(MemoryStream ms)

        {

            var bytes = ms.ToArray();

            ms.Position = 0;

            return Convert.ToBase64String(bytes);

        }


        private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)

        {

            string filePath = string.Empty;

            FileStream fileStream = null;

            string fontName = args.OriginalFontName.ToLower();

            if (args.FontStyle == FontStyle.Regular)

            {

                switch (fontName)

                {

                    case string font when fontName.Contains("algerian"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Algerian.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("arial"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Arial.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("calibri"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Calibri.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("cambria"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Cambria.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("candara"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Candara.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("courier new"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Courier_New.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("georgia"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Georgia.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("impact"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Impact.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("roboto"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Roboto.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("segoe print"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Segoe_Print.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("segoe scipt"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Segoe_Scipt.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("segoe ui"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Segoe_UI.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("symbol"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Symbol.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("times"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Times_New_Roman.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("verdana"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Verdana.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    case string font when fontName.Contains("wingdings"):

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Wingding.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;

                    default:

                        filePath = Path.Combine(AppContext.BaseDirectory, @"Fonts/Times_New_Roman.ttf");

                        fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                        args.AlternateFontStream = fileStream;

                        break;


                }

            }

        }

I have attached a screenshot, rendered PDF file, word document, and fonts also for your reference.


Attachment: SyncfusionIssue_48e7e046.zip


LB Lokesh Baskar Syncfusion Team December 8, 2021 03:00 PM UTC

Hi Sumit,

Thank you for your update.

From the given code snippet, we have found that you have not substituted the required font in the font substitution event method. For example if the Calibri Bold font is missed, then we need to substitute the calibri bold font ttf file(calibrib.ttf) in the font substitution event method based on the font style.  Similar we have to substitute the required ttf font based on the font style. Please refer the below code snippet for an example. (Here the calibrib.ttf is the calibri bold ttf font and calibri.ttf is the calibri regular font ) 
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args) 
{ 
//Sets the required font or alternate font when a specified font is not in the production environment. 
if (args.OrignalFontName == "Calibri" && args.FontStyle == FontStyle.Regular) 
args.AlternateFontStream = new FileStream("calibri.ttf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
else if (args.OrignalFontName == "Calibri" && args.FontStyle == FontStyle.Bold) 
args.AlternateFontStream = new FileStream("calibrib.ttf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
else if (args.OrignalFontName == "Times New Roman" && args.FontStyle == FontStyle.Regular) 
args.AlternateFontStream = new FileStream("Times_New_Roman.ttf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
else if (args.OrignalFontName == "Times New Roman" && args.FontStyle == FontStyle.Bold) 
args.AlternateFontStream = new FileStream("timesbd.ttf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
else if (args.OrignalFontName == "Segoe Print" && args.FontStyle == FontStyle.Regular) 
args.AlternateFontStream = new FileStream("Segoe_Print.ttf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
else if (args.OrignalFontName == "Segoe Print" && args.FontStyle == FontStyle.Bold) 
args.AlternateFontStream = new FileStream("segoeprb.ttf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
 
} 

From your code snippet you have substituted the regular font styles only. Please refer the screenshot given below. 
 

Please refer to the bold fonts of calibri, times new roman, segoe print given below.
https://www.syncfusion.com/downloads/support/forum/170710/ze/BoldFont-757698983.zip

Please let us know if you have any other questions.

Regards,
Lokesh B 



SM Sumit Manna December 10, 2021 08:51 AM UTC

Hi Lokesh,

Thank you for suggesting these changes.

We have implemented these changes. It is working fine for Italic, Regular and Bold. But we are not able to implement the BOLD ITALIC font style together. We have not found any option for BOLD ITALIC font style. There are only Relugar, Bold, Italic, Strikeout, Underline options in Syncfusion.Drawing.FontStyle enum. There is no option for BOLD ITALIC. Pls, help us with this.


Also, Can you please suggest to us that from where we can download these all fonts? We are using the below fonts in our application. This will help us a lot.

Algerian

Arial

Calibri

Cambria

Cambria Math

Candara

CourierNew

Georgia

Impact

Roboto

SegoePrint

SegoeScipt

SegoeUI

Symbol

TimesNewRoman

Verdana

Wingding


I have attached word document and rendered pdf file for your refernce.


Thank you in Advance.


Attachment: SyncfusionItalicBold_728ab96d.zip



LB Lokesh Baskar Syncfusion Team December 10, 2021 01:44 PM UTC

Hi Sumit,

Thank you for your update.

To check whether the bold italic font style together, please refer the highlighted code snippet given below.  
 
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)  
{  
//Sets the required font or alternate font when a specified font is not in the production environment.  
if (args.OrignalFontName == "Times New Roman" && args.FontStyle == (FontStyle.Italic | FontStyle.Bold)) 
args.AlternateFontStream = new FileStream("Font/timesbi.ttf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
}  
 
You can download the font from any authorized website.

Please let us know if you have any other questions.

Regards,
Lokesh B 
 


Loader.
Up arrow icon