从GridView输出数据到PDF有编码问题

时间:2011-08-12 15:54:44

标签: c# asp.net itextsharp

我正在从页面上的网格视图生成PDF(使用iTextSharp),但PDF输出存在编码问题。 例如,在页面上:

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 == "&nbsp;")
                                    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();

任何提示?

编辑:添加新行。 页面是UTF-8,当我定义basefont时,我在iTextSharp的常量中找不到UTF。 EDIT2:我刚刚在Adobe Reader中检查了PDF文件的属性,它说它的编码是Ansi。

感谢。

3 个答案:

答案 0 :(得分:2)

尝试从网格中解码文本,即改为

apiInputOutputGrid.Rows[iteratorRow].Cells[iteratorCell].Text

使用(来自System.Web):

HttpUtility.HtmlDecode(apiInputOutputGrid.Rows[iteratorRow].Cells[iteratorCell].Text)

答案 1 :(得分:0)

如何将此功能添加到您的功能中:

Response.charset="utf-8"

...或者不管是什么字符集应该是,如果不是utf-8 - 也许拉丁语1?

否则,此SO帖子看起来可能与您的相关:Html to pdf some characters are missing (itextsharp)

答案 2 :(得分:0)

它是pdf组件中的编码问题,解析pdf,还是从网格返回的数据,这可能不是新信息:)检查网格结果并查看每个字段等。是否有任何一个编码?有些东西是html编码 - 所以需要弄清楚它是你的网格代码还是pdf代码。