c# - Gujarati Text Not Rendering Properly in PDF Using iText7 but Copies Correctly - Stack Overflow

I am using iText7 to add Gujarati text to a PDF, but I am facing an issue where the text does not rende

I am using iText7 to add Gujarati text to a PDF, but I am facing an issue where the text does not render correctly. However, when I copy the text from the generated PDF and paste it elsewhere, it appears correctly. Additionally, English text is rendered without any issues—this problem occurs only with Gujarati (complex) words.

private void AddTextToPdf()
{
    string inputPdfPath = @"C:\Users\Desktop\Eventi\dummy.pdf";
    string outputPdfPath = @"C:\Users\Desktop\Eventi\output.pdf";
    string nameToAdd = "સંસ્કૃતિ અને વારસો";

    float xPos = 10;
    float yPos = 600;
    PdfReader reader = new PdfReader(inputPdfPath);
    PdfWriter writer = new PdfWriter(outputPdfPath);
    PdfDocument pdfDoc = new PdfDocument(reader, writer);
    var page = pdfDoc.GetPage(1);

    PdfCanvas canvas = new PdfCanvas(page);
    string fontPath = @"C:\Users\Desktop\Eventi\BalooBhai2-Bold.ttf";
    byte[] fontData = File.ReadAllBytes(fontPath);
    PdfFont font = PdfFontFactory.CreateFont(fontPath, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);

    canvas.SetFontAndSize(font, 12);
    canvas.BeginText();
    canvas.MoveText(xPos, yPos);
    canvas.ShowText(nameToAdd);
    canvas.EndText();
    pdfDoc.Close();
}

Issue:

  • The Gujarati text does not render correctly in the PDF. It appears distorted or broken.
  • However, when I copy the text from the PDF and paste it into another document, it appears correctly.
  • English text renders fine, but the issue occurs only with Gujarati complex words.

What I’ve Tried

  • Used different Gujarati fonts (Shruti, NotoSansGujarati, BalooBhai2), but the issue persists.
  • Tried different encoding formats (PdfEncodings.IDENTITY_H, PdfEncodings.UTF8, PdfEncodings.WINANSI), but none of them worked.
  • Verified that the font supports Gujarati characters.
  • Checked if iText properly embeds the font in the PDF.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744292191a4567086.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信