1.11M Members

PDF creation ITextSharp how to rename field names

 
0
 

Pulling column names from a database. Column names aren't user friendly so was wanting to rename them on the pdf. Is there a way to do this?

  foreach (DataColumn column in dt.Columns)
                {


                    PdfPCell pCell = new PdfPCell(new Phrase(column.ColumnName, TableHeaderFont));
                    pCell.BackgroundColor = BaseColor.BLUE;
                    pCell.Padding = 6;
                    contentTable.AddCell(pCell);
                }

this is the code I'm using to pull the column names.

 
0
 

Just replace column.ColumnName with the names you want. I suggest using a dictionary to use for the conversion.

 
0
 

in the line

PdfPCell pCell = new PdfPCell(new Phrase(column.ColumnName, TableHeaderFont));

Will that not only change the column name off one? or change all columns to the same name?

I thought i might have to do something such as

foreach (DataColumn column in dt.Columns)
{   

String NewName = column.ColumnName;
String revisedName = "";
switch (NewName)
{

case "AppRef":
    revisedName ="Reference";
    break;
    default:
    break;

    etc for rest of columns
    }

 PdfPCell pCell = new PdfPCell(new Phrase(column.ColumnName, TableHeaderFont));
pCell.BackgroundColor = BaseColor.BLUE;
pCell.Padding = 6;
contentTable.AddCell(pCell);
}
 
0
 

That's kind of what I meant. Just instead of your switch, create a dictionary with the from and to values. Something like this:

var map = new Dictionary<string, string>();
map.Add("AppRef", "Reference");
// Etc.
PdfPCell pCell = new PdfPCell(new Phrase(map[column.ColumnName], TableHeaderFont));
 
0
 

O get you now, thank you, i'll try that now.

Question Answered as of 1 Year Ago by pritaeas
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article