如何在utf8中将DataGridView转换为pdf
本文关键字:转换 pdf DataGridView 中将 utf8 | 更新日期: 2023-09-27 18:28:50
我在c#中使用iTextSharp.dll
将dataGridView
转换为pdf
。它不会将pdf
作为utf8
,因此我有两个问题:
utf8
问题,它也从左到右列,我如何使它们从右到左
这是代码:
private void pictureBox10_Click(object sender, EventArgs e)
{
FolderBrowserDialog fdialog = new FolderBrowserDialog();
DialogResult result = fdialog.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
string file = fdialog.SelectedPath;
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(dataGridViewX2.ColumnCount);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 100;
pdfTable.HorizontalAlignment = Element.ALIGN_RIGHT;
pdfTable.DefaultCell.BorderWidth = 1;
//Adding Header row
foreach (DataGridViewColumn column in dataGridViewX2.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
//cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
pdfTable.AddCell(cell);
}
//Adding DataRow
foreach (DataGridViewRow row in dataGridViewX2.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
pdfTable.AddCell(cell.Value.ToString());
}
}
//Exporting to PDF
string folderPath = file + "''PDFs''";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
stream.Close();
}
}
}
尝试使用ITextSharp
:将GridView
导出为PDF的教程
protected void btnExportPDF_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"'fonts'Arial.ttf", BaseFont.IDENTITY_H, true);
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 = new Color(GridView1.HeaderStyle.ForeColor);
iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
//Set Header Row BackGround Color
cell.BackgroundColor = new Color(GridView1.HeaderStyle.BackColor);
//Important for Arabic, Persian or Urdu Text
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
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 = new Color(GridView1.RowStyle.ForeColor);
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 = new Color(GridView1.RowStyle.BackColor);
}
//Important for Arabic, Persian or Urdu Text
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
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=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
注意:
本文的代码示例提供的iTextSharp DLL具有已修改为允许使用网格视图样式、颜色和格式。因此,如果您使用iTextSharp的任何其他副本网格视图样式、颜色和格式不会在导出的PDF中呈现。
编辑
作为第二种解决方案,在这个答案中,有一个链接可能会帮助你解决问题。
此GridView导出库支持RTL
编辑2
这里提到的第三种解决方案可能也会帮助您
在处理Unicode字符和iTextSharp时你需要处理的事情。你已经做了第一个这就是得到一个支持你的字符的字体。第二件事是你想在iTextSharp上注册字体,这样
//Path to our font string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); //Register the font with iTextSharp iTextSharp.text.FontFactory.Register(arialuniTff);
现在我们有了字体,我们需要创建一个
StyleSheet
对象告诉iTextSharp何时以及如何使用它。//Create a new stylesheet iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet(); //Set the default body font to our registered font's internal name ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
您还需要做的一个非HTML部分是设置一个特殊的
encoding
参数。此编码特定于iTextSharp,并且在你的情况下,你希望它是Identity-H
。如果你不设置这个则默认为CCD_ 12(CCD_。//Set the default encoding to support Unicode characters ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
最后,我们需要将样式表传递给
ParseToList
方法://Parse our HTML using the stylesheet created above List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST);
把所有这些放在一起,从打开到关闭,你会得到:
doc.Open(); //Sample HTML StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(@"<p>This is a test: <strong>α,β</strong></p>"); //Path to our font string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); //Register the font with iTextSharp iTextSharp.text.FontFactory.Register(arialuniTff); //Create a new stylesheet iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet(); //Set the default body font to our registered font's internal name ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS"); //Set the default encoding to support Unicode characters ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H); //Parse our HTML using the stylesheet created above List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST); //Loop through each element, don't bother wrapping in P tags foreach (var element in list) { doc.Add(element); } doc.Close();
编辑
在注释中,您将显示指定覆盖字体的HTML。iTextSharp不会在系统中抓取字体及其HTML解析器不使用字体回退技术。HTML/CSS中指定的任何字体必须手动注册。
string lucidaTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "l_10646.ttf"); iTextSharp.text.FontFactory.Register(lucidaTff);