<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" Width="300" HeaderStyle-BackColor="#D44A24"
HeaderStyle-ForeColor="White" AutoGenerateColumns="false" Font-Names="Helvatica" Font-Size="12">
<Columns>
<asp:BoundField ItemStyle-Width="200px" DataField="StudentName" HeaderText="Student Name" />
<asp:BoundField ItemStyle-Width="200px" DataField="StudentNameHindi" HeaderText="Student Name In Hindi" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExportPDF" runat="server" Text="Export Gridview To PDF" OnClick="btnExportGridviewPDF_Click" />
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("StudentName"), new DataColumn("StudentNameHindi") });
dt.Rows.Add("Sneha", "स्नेहा");
dt.Rows.Add("Rishabh", "रिषभ");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnExportGridviewPDF_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);
PdfPTable table = new 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
Font font = new Font(bf, 10, Font.NORMAL);
font.Color = new Color(GridView1.HeaderStyle.ForeColor);
PdfPCell cell = new PdfPCell(new Phrase(12, cellText, font));
// Set Header Row BackGround Color
cell.BackgroundColor = new Color(GridView1.HeaderStyle.BackColor);
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
Font font = new Font(bf, 10, Font.NORMAL);
font.Color = new Color(GridView1.RowStyle.ForeColor);
PdfPCell cell = new PdfPCell(new Phrase(12, cellText, font));
table.AddCell(cell);
}
}
// Create the PDF Document
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ExportGridView.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
}
马拉地语(marathi)之类的单词स्नेहा不能正确显示,在gridview上它可以正确显示,而转换为pdf则不起作用。请检查我的代码并给我解决方案,在此先感谢marathi加入स्नेहा之类的单词显示不正确,在gridview上它显示正确,而转换为pdf则不起作用。请检查我的代码并给我解决方案,谢谢。