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
|
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);
}
|
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 ó, N°, í that time we are facing this issue.
Thank you in advance.
Thanks& Regards,
Sumit
|
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);
}
|
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.
|
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);
} |
|
|
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
|
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);
} |