Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:19:31+00:00 2026-05-24T17:19:31+00:00

I’m generating a PDF (using iTextSharp) from a gridview that is present on the

  • 0

I’m generating a PDF (using iTextSharp) from a gridview that is present on the page, but the PDF output has encoding issues.
For example this on the page:

Aplicação para posicionar

Appears like this on the PDF:

Aplicação para posicionar

I’m a generating this directly from the gridview since I need user input (checkboxes for example) so I can’t read this data from a database. I assume some sort of encode/decode is in order, but I’m quite at a loss here.

Steps involved in creating the 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();

Any hints?

EDIT: add a new line.
The page is UTF-8, when I define the basefont I can’t find UTF in the constants of iTextSharp.
EDIT2: Also I just checked the properties of the PDF file in Adobe Reader and it says that it’s encoding is Ansi.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:19:32+00:00Added an answer on May 24, 2026 at 5:19 pm

    Try decoding the Text from the grid, i.e. instead

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

    use (from System.Web):

    HttpUtility.HtmlDecode(apiInputOutputGrid.Rows[iteratorRow].Cells[iteratorCell].Text)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.