C# 从GridView输出数据到PDF存在编码问题
我正在从页面上的gridview生成PDF(使用iTextSharp),但PDF输出存在编码问题。 例如,在页面上:C# 从GridView输出数据到PDF存在编码问题,c#,asp.net,itextsharp,C#,Asp.net,Itextsharp,我正在从页面上的gridview生成PDF(使用iTextSharp),但PDF输出存在编码问题。 例如,在页面上: Aplicação para posicionar 在PDF中显示如下: Aplicação para posicionar 我是一个直接从gridview生成此数据的用户,因为我需要用户输入(例如复选框),所以无法从数据库读取此数据。我假设某种编码/解码是正常的,但我在这里完全不知所措 创建PDF所涉及的步骤: BaseFont helvetica = Ba
Aplicação para posicionar
在PDF中显示如下:
Aplicação para posicionar
我是一个直接从gridview生成此数据的用户,因为我需要用户输入(例如复选框),所以无法从数据库读取此数据。我假设某种编码/解码是正常的,但我在这里完全不知所措
创建PDF所涉及的步骤:
BaseFont helvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
Font helvetica14 = new Font(helvetica, 14, Font.NORMAL);
Font helvetica12BOLDITALIC = new Font(helvetica, 12, Font.BOLDITALIC);
Font helvetica8BOLDITALIC = new Font(helvetica, 8, Font.BOLDITALIC);
Font helvetica6 = new Font(helvetica, 6, Font.NORMAL);
//Create PDF document
Document doc = new Document(PageSize.A4);
MemoryStream outputStream = new MemoryStream();
PdfWriter.GetInstance(doc, outputStream);
doc.Open();
//Add title
doc.Add(new Paragraph(importantlblTitleGlobal + "\n\n\n\n", helvetica14));
//Copy the Api Transaction table
if (detailsApiTransactionRowCount > 1)
{
//Create PDF table
PdfPTable tableDetailsInput = new PdfPTable(detailsApiTransactionCellCount);
//Create title table
PdfPCell cell = new PdfPCell(new Phrase("Api Transaction List", helvetica12BOLDITALIC));
cell.BackgroundColor = new BaseColor(128, 128, 128);
cell.Colspan = detailsApiTransactionCellCount;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
tableDetailsInput.AddCell(cell);
string[] headers = { "Transaction Name", "Transaction Description" };
for (iteratorCell = 0; iteratorCell < detailsApiTransactionCellCount; iteratorCell++)
{
PdfPCell newCell = new PdfPCell(new Phrase(headers[iteratorCell], helvetica8BOLDITALIC));
newCell.BackgroundColor = new BaseColor(192, 192, 192);
tableDetailsInput.AddCell(newCell);
}
//Create content table
for (iteratorRow = 0; iteratorRow < detailsApiTransactionRowCount; iteratorRow++)
{
for (iteratorCell = 0; iteratorCell < detailsApiTransactionCellCount; iteratorCell++)
{
Phrase newPhrase = new Phrase(apiTransactionListGrid.Rows[iteratorRow].Cells[iteratorCell].Text, helvetica6);
tableDetailsInput.AddCell(newPhrase);
}
}
doc.Add(tableDetailsInput);
}
//Line break
doc.Add(new Paragraph("\n\n\n"));
//Copy the INPUT/OUTPUT table
if (detailsInputOutputRowCount > 0)
{
//Create PDF table
PdfPTable tableDetailsInputOutput = new PdfPTable(detailsInputOutputCellCount);
//Create title table
PdfPCell cell = new PdfPCell(new Phrase("Input/Output Details", helvetica12BOLDITALIC));
cell.BackgroundColor = new BaseColor(128, 128, 128);
cell.Colspan = detailsInputOutputCellCount;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
tableDetailsInputOutput.AddCell(cell);
//Create headers table
string[] headers = { "Name", "Format", "Description", "Observation", "isInput", "isOutput", "SpecialType" };
for (iteratorCell = 0; iteratorCell < detailsInputOutputCellCount; iteratorCell++)
{
PdfPCell newCell = new PdfPCell(new Phrase(headers[iteratorCell], helvetica8BOLDITALIC));
newCell.BackgroundColor = new BaseColor(192, 192, 192);
tableDetailsInputOutput.AddCell(newCell);
}
for (iteratorRow = 0; iteratorRow < detailsInputOutputRowCount; iteratorRow++)
{
for (iteratorCell = 0; iteratorCell < detailsInputOutputCellCount; iteratorCell++)
{
switch (iteratorCell)
{
case 3:
{
if (apiInputOutputGrid.Rows[iteratorRow].Cells[iteratorCell].Text == " ")
tableDetailsInputOutput.AddCell(new Phrase("", helvetica6));
else
tableDetailsInputOutput.AddCell(new Phrase(apiInputOutputGrid.Rows[iteratorRow].Cells[iteratorCell].Text, helvetica6));
}
break;
case 4:
{
if (iteratorRow >= 9)
{
Phrase newPhrase = new Phrase("", helvetica6);
if (booleanIsInput[iteratorRow])
{
newPhrase = new Phrase("X", helvetica6);
tableDetailsInputOutput.AddCell(newPhrase);
}
else
tableDetailsInputOutput.AddCell(newPhrase);
}
else
{
Phrase newPhrase = new Phrase("", helvetica6);
PdfPCell newCell = new PdfPCell(newPhrase);
newCell.BackgroundColor = new BaseColor(192, 192, 192);
tableDetailsInputOutput.AddCell(newCell);
}
}
break;
case 5:
{
if (iteratorRow >= 9)
{
Phrase newPhrase = new Phrase("", helvetica6);
if (booleanIsOutput[iteratorRow])
{
newPhrase = new Phrase("X", helvetica6);
tableDetailsInputOutput.AddCell(newPhrase);
}
else
tableDetailsInputOutput.AddCell(newPhrase);
}
else
{
Phrase newPhrase = new Phrase("", helvetica6);
PdfPCell newCell = new PdfPCell(newPhrase);
newCell.BackgroundColor = new BaseColor(192, 192, 192);
tableDetailsInputOutput.AddCell(newCell);
}
}
break;
case 6:
{
tableDetailsInputOutput.AddCell(new Phrase(specialType[iteratorRow], helvetica6));
}
break;
default:
tableDetailsInputOutput.AddCell(new Phrase(apiInputOutputGrid.Rows[iteratorRow].Cells[iteratorCell].Text, helvetica6));
break;
}
}
}
doc.Add(tableDetailsInputOutput);
}
//CloseDocument
doc.Close();
//Clear the response buffer'
Response.Clear();
//Set the output type as a PDF'
Response.ContentType = "application/pdf";
//Disable caching'
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "");
//Set the filename'
string filename = "filename.pdf";
filename = filename.Replace(' ', '_');
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
//Set the length of the file so the browser can display an accurate progress bar'
Response.AddHeader("Content-length", outputStream.GetBuffer().Length.ToString());
//Write the contents of the memory stream'
Response.OutputStream.Write(outputStream.GetBuffer(), 0, outputStream.GetBuffer().Length);
//Close the response stream'
Response.End();
BaseFont-helvetica=BaseFont.CreateFont(BaseFont.helvetica,BaseFont.CP1252,false);
Font helvetica14=新字体(helvetica,14,Font.NORMAL);
Font helvetica12BOLDITALIC=新字体(helvetica,12,Font.BOLDITALIC);
Font helvetica8BOLDITALIC=新字体(helvetica,8,Font.BOLDITALIC);
Font helvetica6=新字体(helvetica,6,Font.NORMAL);
//创建PDF文档
文档文档=新文档(PageSize.A4);
MemoryStream outputStream=新的MemoryStream();
GetInstance(doc,outputStream);
doc.Open();
//添加标题
添加文件(新段落(重要标题全局+“\n\n\n\n”,helvetica14));
//复制Api事务表
如果(detailsApiTransactionRowCount>1)
{
//创建PDF表格
PDFPTableDetailsInput=新的PdfPTable(detailsApiTransactionCellCount);
//创建标题表
PdfPCell cell=新的PdfPCell(新短语(“Api事务列表”,helvetica12BOLDITALIC));
cell.BackgroundColor=新的基色(128、128、128);
cell.Colspan=detailsApiTransactionCellCount;
cell.HorizontalAlignment=1;//0=左,1=中,2=右
tableDetailsInput.AddCell(单元格);
字符串[]头={“事务名称”,“事务描述”};
for(iteratorCell=0;iteratorCell0)
{
//创建PDF表格
PDFPTableDetailsInputOutput=新PdfPTable(detailsInputOutputCellCount);
//创建标题表
PdfPCell cell=新的PdfPCell(新短语(“输入/输出详细信息”,helvetica12BOLDITALIC));
cell.BackgroundColor=新的基色(128、128、128);
cell.Colspan=detailsInputOutputCellCount;
cell.HorizontalAlignment=1;//0=左,1=中,2=右
tableDetailsInputOutput.AddCell(单元格);
//创建标题表
字符串[]头={“名称”、“格式”、“描述”、“观察”、“输入”、“输出”、“特殊类型”};
for(iteratorCell=0;iteratorCell=9)
{
短语newPhrase=新短语(“,helvetica6”);
if(布尔输入[iteratorRow])
{
新短语=新短语(“X”,helvetica6);
tableDetailsInputOutput.AddCell(新短语);
}
其他的
tableDetailsInputOutput.AddCell(新短语);
}
其他的
{
短语newPhrase=新短语(“,helvetica6”);
PdfPCell newCell=新PdfPCell(新短语);
newCell.BackgroundColor=新的基色(192,192,192);
tableDetailsInputOutput.AddCell(newCell);
}
}
打破
案例5:
Response.charset="utf-8"
apiInputOutputGrid.Rows[iteratorRow].Cells[iteratorCell].Text
HttpUtility.HtmlDecode(apiInputOutputGrid.Rows[iteratorRow].Cells[iteratorCell].Text)