We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,163 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PDF creation ITextSharp how to rename field names

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.

2
Contributors
4
Replies
5 Hours
Discussion Span
5 Months Ago
Last Updated
5
Views
Question
Answered
coder91
Junior Poster in Training
94 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

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);
}
coder91
Junior Poster in Training
94 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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));
pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

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

coder91
Junior Poster in Training
94 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by pritaeas

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0659 seconds using 2.66MB