these two functions take care of converting a datagridview into a datatable using c # when the conversion is carried out the file is printed on the pdf, but the name of the columns is embossed once the printing is done in pdf, how can I solve this? I have to make the column name appear
C# Code:
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
dt.Columns.Add();
}
}
object[] cellValues = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
cellValues[i] = row.Cells[i].Value;
}
dt.Rows.Add(cellValues);
}
return dt;
}
public void createPDF(DataTable dataTable, string destinationPath)
{
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationPath, FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(dataTable.Columns.Count);
table.WidthPercentage = 100;
//Set columns names in the pdf file
for (int k = 0; k < dataTable.Columns.Count; k++)
{
PdfPCell cell = new PdfPCell(new Phrase(dataTable.Columns[k].ColumnName));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
cell.BackgroundColor = new iTextSharp.text.BaseColor(51, 102, 102);
table.AddCell(cell);
}
//Add values of DataTable in pdf file
for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
PdfPCell cell = new PdfPCell(new Phrase(dataTable.Rows[i][j].ToString()));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
}
}
document.Add(table);
document.Close();
}
Try adding this in the foreach loop that adds columns:
dt.Columns.Add(column.Name, typeof(string));
Or alternatively:
dt.Columns.Add(column.HeaderText, typeof(string));
So it would look like this:
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
dt.Columns.Add(column.Name, typeof(string));
}
}
I haven't tested this code but you have to explicitly add names to the Columns, tell me if it works...
Related
For Example in this Image , I have four Dates of record , may be in next PDF will have 10 dates of record then how to set maximum five number of Dates Record show in a First-Table and next five dates show in Second-Table just after First-Table (Second Table Auto Generate) in iTextSharp
for (int c = 4; c < dt.Columns.Count; c++)
{
cell = new PdfPCell(new Phrase(dt.Columns[c].ToString(), fntMainHeading));
cell.Padding = 2;
cell.BorderColor = new Color(217, 217, 217);
table.AddCell(cell);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
if (ServiceCode == 0 || ServiceCode != Convert.ToInt32(dt.Rows[i]["ServiceCode"]))
{
ServiceCode = Convert.ToInt32(dt.Rows[i]["ServiceCode"]);
cell = new PdfPCell(new Phrase(Convert.ToString(dt.Rows[i]["ServiceName"]), fntHeading));
cell.Padding = 2;
cell.Colspan = col;
cell.BorderColor = new Color(217, 217, 217);
table.AddCell(cell);
}
for (int j = 4; j < dt.Columns.Count; j++)
{
cell = new PdfPCell(new Phrase(Convert.ToString(dt.Rows[i][j]), fntNormalText));
cell.Padding = 2;
cell.BorderColor = new Color(217, 217, 217);
table.AddCell(cell);
}
}
table.SpacingBefore = 10;
table.SpacingAfter = 10;
doc.Add(table);
}
I am trying to create a word table from xml file but the rows are not in their proper order,ie the first element occurs as last row, 2nd as 1st row and so on. Where am i going wrong?
Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count, ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
for (int r = 1; r <= ds.Tables[0].Rows.Count; r++)
{
for (int c = 1; c <= ds.Tables[0].Columns.Count; c++)
{
oTable.Cell(r, c).Range.Text = ds.Tables[0].Rows[r-1].ItemArray[c-1].ToString();
}
}
Here is the sample code to create simple word file with Table. It will give you a loop hint how to proceed with it.
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section s = doc.AddSection();
Table table = s.AddTable(true);
//Create Header and Data
String[] header = { "Item", "Description", "Qty", "Unit Price", "Price" };
String[][] data = {
new String[]{ "Spire.Doc for .NET",".NET Word Component","1","$799.00","$799.00"},
new String[]{"Spire.XLS for .NET",".NET Excel Component","2","$799.00","$1,598.00"},
new String[]{"Spire.Office for .NET",".NET Office Component","1","$1,899.00","$1,899.00"},
new String[]{"Spire.PDF for .NET",".NET PDFComponent","2","$599.00","$1,198.00"},
};
//Add Cells
table.ResetCells(data.Length + 1, header.Length);
//Header Row
TableRow fRow = table.Rows[0];
fRow.IsHeader = true;
//Row Height
fRow.Height = 23;
//Header Format
fRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < header.Length; i++)
{
//Cell Alignment
Paragraph p = fRow.Cells[i].AddParagraph();
fRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Data Format
TextRange TR = p.AppendText(header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 14;
TR.CharacterFormat.TextColor = Color.Teal;
TR.CharacterFormat.Bold = true;
}
//Data Row
for (int r = 0; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r + 1];
//Row Height
dataRow.Height = 20;
//C Represents Column.
for (int c = 0; c < data[r].Length; c++)
{
//Cell Alignment
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Fill Data in Rows
Paragraph p2 = dataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(data[r][c]);
//Format Cells
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 12;
TR2.CharacterFormat.TextColor = Color.Brown;
}
}
//Save and Launch
doc.SaveToFile("WordTable.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("WordTable.docx");
Console.ReadLine();
}
I am facing issue while generating pdf from Windows form using iTextSharp dll. I am populating more than 70 records at a time in a dataset and I am binding same to new table(pdfptable) by looping. But it says Object reference not set to instance of object. when I reduced number of rows to 68 it works fine. After analysis I found that table is not able to split along the multiple pages.
I am using following piece of code. Please help
float[] widths1 = new float[] { 1f, 2f,1f,1f,1f,1f,1f};
PdfPTable grd = new PdfPTable(GrdAnoSecList.Columns.Count);
grd.SetWidths(widths1);
for (int i = 0; i < GrdAnoSecList.Columns.Count; i++)
{
PdfPCell ph;
if (Convert.ToString(GrdAnoSecList.Columns[i].HeaderText) == "Job Name")
{
ph = new PdfPCell(new Phrase(GrdAnoSecList.Columns[i].HeaderText, grdfontHdr));
ph.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
ph.BorderWidthTop = 0;
ph.BorderWidthRight = 0;
ph.HorizontalAlignment = 1;
}
else
{
ph= new PdfPCell(new Phrase(GrdAnoSecList.Columns[i].HeaderText, grdfontHdr));
ph.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
ph.BorderWidthTop = 0;
ph.BorderWidthRight = 0;
ph.HorizontalAlignment = 1;
//ph.Width = 100f;
}
grd.AddCell(ph);
}
int ct = 0;
foreach (DataGridViewRow row in GrdAnoSecList.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
ct++;
PdfPCell ph2;
if (Convert.ToString(cell.Value) == "Total")
{
ph2 = new PdfPCell(new Phrase(cell.Value.ToString(), grdfontHdr));
}
else
{
ph2 = new PdfPCell(new Phrase(cell.Value.ToString(), myfont));
}
if (cell.ValueType == typeof(decimal) || cell.ValueType == typeof(Int32))
{
ph2.HorizontalAlignment = 2;
}
else if (cell.ColumnIndex == 1 && Convert.ToString(cell.Value) != "Total")
{
ph2.HorizontalAlignment = 0;
}
else
{
ph2.HorizontalAlignment = 1;
}
grd.AddCell(ph2);
}
}
PdfPCell cellAmountText1 = new PdfPCell(new PdfPTable(grd));
cellAmountText1.Colspan = 6;
table.AddCell(cellAmountText1);
I was adding a table to pdf. I have 3 rows and 3 columns. I want the first column to appear only once as a single cell for all the rows. How can I do that?My code is as follows. My output should come like Deloitte in the column of company as shown in the image:
PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
float[] widths = new float[] { 4f, 4f, 4f };
table.SetWidths(widths);
table.WidthPercentage = 100;
int iCols = 0;
string colname = "";
PdfPCell cells = new PdfPCell(new Phrase("Products"));
cells.Colspan = dt.Columns.Count;
foreach (DataColumn c in dt.Columns)
{
table.AddCell(new Phrase(c.ColumnName, fontbold));
}
foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
table.AddCell(new Phrase(r[0].ToString(), font5));
table.AddCell(new Phrase(r[1].ToString(), font5));
table.AddCell(new Phrase(r[2].ToString(), font5));
}
} document.Add(table);
The MyFirstTable example from my book does exactly what you need. Ported to C#, it looks like this:
PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
// we add the four remaining cells with addCell()
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
You can look at the resulting PDF here. In your case you'd need
cell.Rowspan = 6;
For the cell with value Deloitte.
i am trying to build a pdf, in which i have to add a table without border and i am doing like this, but is there any better way to do this?
my code is like this:
PdfPTable row1 = new PdfPTable(4);
row1.TotalWidth = 350f;
row1.LockedWidth = true;
int[] intTblWidth1 = { 20,50,20,40 };
row1.SetWidths(intTblWidth1);
row1.SpacingBefore = 20f;
row1.HorizontalAlignment = Element.ALIGN_LEFT;
PdfPCell cel = new PdfPCell(new Phrase("Ordered By: ", bodyFont));
cel.Colspan = 1;
cel.Border = 0;
cel.HorizontalAlignment = 0;
row1.AddCell(cel);
PdfPCell cel1 = new PdfPCell(new Phrase(_requester, titleFont));
cel1.Border = 0;
cel1.HorizontalAlignment = 0;
cel1.VerticalAlignment = 0;
row1.AddCell(cel1);
PdfPCell cel2 = new PdfPCell(new Phrase("Order #: ", bodyFont));
cel2.Colspan = 1;
cel2.Border = 0;
cel2.HorizontalAlignment = 0;
row1.AddCell(cel2);
PdfPCell cel3 = new PdfPCell(new Phrase(_orderNumber, titleFont));
cel3.Colspan = 1;
cel3.Border = 0;
cel3.HorizontalAlignment = 0;
row1.AddCell(cel3);
doc.Add(row1);
i am using new table to create new row.
if i do like this:-
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
doc.Add(table);
i am not able to hide the border of table, and i don't wanna any border line in the table.
i have to generate a dynamic pdf. any suggestion will be appreciated, i will mark your answer if it work for me. thank you in advance ;) happy coding.
Try doing this:
table.DefaultCell.Border = Rectangle.NO_BORDER;
or you should try this for each cell
cellxxx.Border = Rectangle.NO_BORDER;
The Border Elements of the PdfPTable are defined by the PdfPCell which are added to the table. Each Cell will have its own style/formatting. Here is the API: http://api.itextpdf.com/