Hi @Cenk ,
Perhaps the issue relates that font file (NotoSansTC-Regular.otf), might be it was corrupted, try to re-download it.
Besides, you can also store the font file in the wwwroot
folder, then access the file via the IWebHostEnvironment and the WebRootPath.
According to your code, I create a sample (install the iText7 7.2.4 version) and download the font file from here: NotoSansTC-Regular.otf.
private void PrintPDF()
{
string[] data = new string[] { "Mahesh Chand", "Mike Gold", "Delivery Information: AllSet Electronics.(武汉全易得科技有限公司)" };
var path = Path.Combine(_env.WebRootPath, "files","output.pdf");
GeneratePdf(data, path);
}
public void GeneratePdf(string[] paragraphs, string destination)
{
FileInfo file = new FileInfo(destination);
file.Delete();
var fileStream = file.Create();
fileStream.Close();
PdfDocument pdfdoc = new PdfDocument(new PdfWriter(file));
FontProgram fontProgram = FontProgramFactory.CreateFont(Path.Combine(_env.WebRootPath, "font", "NotoSansCJKsc-Regular.otf"));
PdfFont chinese = PdfFontFactory.CreateFont(fontProgram, PdfEncodings.IDENTITY_H);
using (Document document = new Document(pdfdoc))
{
foreach (var para in paragraphs)
{
Paragraph delivery = new Paragraph(para)
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14)
.SetFont(chinese);
document.Add(delivery);
}
document.Close();
}
}
Need to add the following reference:
The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion