asp.net - 使用 itextsharp 将印地语 unicode 字体转换为 pdf

标签 asp.net c#-4.0

我已经使用 itextsharp c# 创建了 pdf,对于使用“ARIALUNI.TTF”的 unicode 印地文字体,但是当我创建 pdf 时,印地文字体没有正确显示为输入。那么,如何在 pdf 中正确获取印地语字体。 可以使用任何其他 unicode 字体吗?或任何其他决议? 这是我的代码和 html...

 private void fillgridview()
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("HindiName") });
        dt.Rows.Add("India", "भारत");
        dt.Rows.Add("China", "चीन");
        dt.Rows.Add("Australia", "ऑस्ट्रेलिया");
        dt.Rows.Add("United States", "अमेरिका");
        dt.Rows.Add("Canada", "कनाडा");
        dt.Rows.Add("Russia", "रूस");
        dt.Rows.Add("Itli", "इटली");
        dt.Rows.Add("France", "फ्रांस");
        GridView1.DataSource = dt;
        GridView1.DataBind(); 
    }
    protected void btnExportPDF_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        GridView1.DataBind();              
        BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count);
        int[] widths = new int[GridView1.Columns.Count];
        for (int x = 0; x < GridView1.Columns.Count; x++)
        {
            widths[x] = (int)GridView1.Columns[x].ItemStyle.Width.Value;
            string cellText = Server.HtmlDecode(GridView1.HeaderRow.Cells[x].Text);
            //Set Font and Font Color
            iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
            font.Color = BaseColor.WHITE;
            iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
            //Set Header Row BackGround Color
            cell.BackgroundColor = BaseColor.BLUE;
            table.AddCell(cell);
        }
        table.SetWidths(widths);
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
            {
                for (int j = 0; j < GridView1.Columns.Count; j++)
                {
                    string cellText = Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text);
                    //Set Font and Font Color
                    iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
                    font.Color = BaseColor.BLACK;
                    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
                    //Set Color of row
                    if (i % 2 == 0)
                    {
                        //Set Row BackGround Color
                        cell.BackgroundColor = BaseColor.YELLOW;
                    }
                    table.AddCell(cell);
                }
            }
        }
        //Create the PDF Document
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        using (MemoryStream memoryStream = new MemoryStream())
        {  
        PdfWriter.GetInstance(pdfDoc, memoryStream);
        pdfDoc.Open();
        pdfDoc.Add(table);
        pdfDoc.Close();
        byte[] bytes = memoryStream.ToArray();        
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);         
        //Response.Write(pdfDoc);
        Response.BinaryWrite(bytes);
        Response.End();
        }
    }






<asp:GridView ID="GridView1" runat="server" Width="300" HeaderStyle-BackColor="Blue"
    HeaderStyle-ForeColor="White" RowStyle-BackColor="Yellow" AlternatingRowStyle-BackColor="White"
    RowStyle-ForeColor="Black" AutoGenerateColumns="false" Font-Names="Arial" Font-Size="12">
    <Columns>
        <asp:BoundField ItemStyle-Width="200px" DataField="Name" HeaderText="Name" />
        <asp:BoundField ItemStyle-Width="200px" DataField="HindiName" HeaderText="Hindi Name" />
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExportPDF" runat="server" Text="ExportToPDF" OnClick="btnExportPDF_Click" />

Output:
[<img src="~/AAA.jpg" width="300" height="100">][1]


  [1]: /image/xUpnG.jpg

最佳答案

我知道这是一个老问题,但最近我遇到了同样的情况。在互联网上找不到任何解决方案。现在我有了一个棘手的解决方案。

在这里分享以帮助任何人。

我有 win10,它预装了 Kundli NormalA Regular 字体。

要查看系统中的字体-打开C盘-Windows-字体。

我右键单击它,出现一个选项 - Compress to "HINN.rar"

所以我尝试了 HINN.TFF

BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\HINN.TTF", BaseFont.IDENTITY_H, true);

iTextSharp.text.Font Hindiheadingfonta = new iTextSharp.text.Font(bf, 16, iTextSharp.text.Font.BOLD);

    

之后打开 MS Office 并选择字体 Kundli。 在印地语字体中输入您想输入的任何内容。 复制该文本并粘贴到 VS 工作室。 MS Word 中的बिजेन्द्र 将被粘贴为 C# 中的 fctsUæ。

PdfPCell Cell15B = new PdfPCell(new Phrase("**fctsUæ**", Hindiheadingfonta));
   

完成。

关于asp.net - 使用 itextsharp 将印地语 unicode 字体转换为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40736964/

相关文章:

asp.net - asp.net 中的绑定(bind)变量

asp.net:如何在后端找到中继器 div?

asp.net - 用于开发新的Windows Azure管理门户的框架?

c#-4.0 - Windows 8 WinRT 中的锁定屏幕

c# - CLR 如何解析涉及动态类型的方法?

c# - IComparer 和 IEqualityComparer 接口(interface)中逆变的好处

asp.net - 如何从浏览器历史记录中删除页面?

c# - 如何在asp.net中使用定时器控件?

.net - 使用反射 c# 映射业务对象和实体对象

c# - 当前上下文中不存在名称 'sqlDbType'