Constant column for multiple rows - c#

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.

Related

Conversion from DataGridView to DataTable

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...

Auto Create Table On Next Line in ITextSharp

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);
}

How to merge rows with same value in PDF using iTextSharp, in ASP.NET and C#?

I have the next problem:
I need merge the rows with the same value for this table:
Table I need
This table is in a GridView, and I realize the method that merge that rows
private void AgruparGridView(GridViewRowCollection rows, int indiceInicial, int totalCeldasAgrupar)
{
//Si no hay celdas a agrupar no realiza ninguna acción
if (totalCeldasAgrupar == 0)
return;
int i, count = 1;
ArrayList lst = new ArrayList();
// Los elementos del gridview son llenados en la lista.
lst.Add(rows[0]);
var ctrl = rows[0].Cells[indiceInicial];
//Recorrer los registros que se tengan para agrupar
for (i = 1; i < rows.Count; i++)
{
TableCell nextCell = rows[i].Cells[indiceInicial];
if (ctrl.Text == nextCell.Text)
{
count++;
nextCell.Visible = false;
lst.Add(rows[i]);
}
else
{
if (count > 1)
{
ctrl.RowSpan = count;
AgruparGridView(new GridViewRowCollection(lst), indiceInicial + 1, totalCeldasAgrupar - 1);
}
count = 1;
lst.Clear();
ctrl = rows[i].Cells[indiceInicial];
lst.Add(rows[i]);
}
}
if (count > 1)
{
ctrl.RowSpan = count;
AgruparGridView(new GridViewRowCollection(lst), indiceInicial + 1, totalCeldasAgrupar - 1);
}
count = 1;
lst.Clear();
}
I need realize the same result on a PDF Report with iTextSharp, but I haven't idea for this process.
The fragment of the code that builds this section is this:
public PdfPTable Tablas(int TipoSeccion, int TipoDoc, int ClaveEntidad, int ClaveSubSis, string descripcion)
{
PDFEvents oPDFEvents = new PDFEvents();
string sError = string.Empty;
int columns = 1;
int i = 0;
DataSet _DatosTabla = null;
PdfPTable Table;
PdfPCell CeldaNoBorde = new PdfPCell(CeldasSinBorde("", 1, 1, 4, 255, 255, 255));
#region PerfilesDOCENTE
#region Tipo3
if (TipoSeccion == 3)
{
columns = 3;
Table = new PdfPTable(columns);
Table.WidthPercentage = 80;
float[] widhts = new float[] { 15, 50 };
//Header Superior
if (this.TipoSeccionOriginal == 3 || this.TipoSeccionOriginal==0)
{
Table.AddCell(Celdas("ASIGNATURAS POR EXAMEN DISCIPLINAR PARA FUNCIONES DOCENTES", 2, 3, 8, 238, 233, 233));
Table.AddCell(Celdas("Área disciplinar", 3, 1, 10, 238, 233, 233));
Table.AddCell(Celdas("Asignatura", 3, 1, 10, 238, 233, 233));
Table.AddCell(Celdas("Evaluación Disciplinar", 3, 1, 10, 238, 233, 233));
_DatosTabla = DameDatosTablaPerfilesTelebach(ClaveEntidad, ClaveSubSis);
}
else
{
Table.AddCell(Celdas("ASIGNATURAS A CONCURSO PARA FUNCIONES TÉCNICO DOCENTES", 1, 2, 8, 238, 233, 233));
Table.AddCell(Celdas("Evaluación", 2, 1, 10, 238, 233, 233));
Table.AddCell(Celdas("Asignaturas", 2, 1, 10, 238, 233, 233));
_DatosTabla = DameDatosTablaPerfiles(TipoDoc, ClaveEntidad, ClaveSubSis, true);
}
if (_DatosTabla != null)
{
while (i < _DatosTabla.Tables[0].Rows.Count)
{
Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][0].ToString(), 10));
Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][1].ToString(), 10));
Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][2].ToString(), 10));
i = i + 1;
}
}
return Table;
}
My result is this
My actual result
Sorry for my bad english!!
Thank you for you attention!
You need to set Rowspan = n on the appropriate PdfPCell and omit adding the next n-1 cells in that column. Here's an example that first builds a regular 3x3 table. Cells are marked row,column. Then a table is built with Rowspan = 3 on the first cell. The fourth (2,1) and seventh (3,1) cell are not added.
Document doc = new Document();
PdfWriter writer =
PdfWriter.GetInstance(doc, new FileStream("tables.pdf", FileMode.Create));
doc.Open();
doc.Add(new Paragraph("Table without rowspan:"));
PdfPTable table = new PdfPTable(3);
table.SpacingBefore = 10;
table.AddCell(new PdfPCell(new Phrase("1,1")));
table.AddCell(new PdfPCell(new Phrase("1,2")));
table.AddCell(new PdfPCell(new Phrase("1,3")));
table.AddCell(new PdfPCell(new Phrase("2,1")));
table.AddCell(new PdfPCell(new Phrase("2,2")));
table.AddCell(new PdfPCell(new Phrase("2,3")));
table.AddCell(new PdfPCell(new Phrase("3,1")));
table.AddCell(new PdfPCell(new Phrase("3,2")));
table.AddCell(new PdfPCell(new Phrase("3,3")));
doc.Add(table);
doc.Add(new Paragraph("Table with rowspan 3 on first cell:"));
PdfPTable tableWithRowspan = new PdfPTable(3);
tableWithRowspan.SpacingBefore = 10;
PdfPCell cellWithRowspan = new PdfPCell(new Phrase("1,1"));
// The first cell spans 3 rows
cellWithRowspan.Rowspan = 3;
tableWithRowspan.AddCell(cellWithRowspan);
tableWithRowspan.AddCell(new PdfPCell(new Phrase("1,2")));
tableWithRowspan.AddCell(new PdfPCell(new Phrase("1,3")));
// Cell 2,1 does not exist
tableWithRowspan.AddCell(new PdfPCell(new Phrase("2,2")));
tableWithRowspan.AddCell(new PdfPCell(new Phrase("2,3")));
// Cell 3,1 does not exist
tableWithRowspan.AddCell(new PdfPCell(new Phrase("3,2")));
tableWithRowspan.AddCell(new PdfPCell(new Phrase("3,3")));
doc.Add(tableWithRowspan);
doc.Close();
The result:

export grid view to existing pdf templete c#

I am reading data from database in grid view and try to bind the same gridview in a previously created pdf templete, but i am not sure how to do that.
Is there a possible way to do that in c#.
iTextSharp is a good library for create pdf file in c# and asp.net. it's fully optional and have a lot of document
Using itextsharp you can create a pdf document
You can loop through the gridview and populate a table in the pdf file.
int[] clmwidths111 = { 30, 20 };
PdfPTable tbl14 = new PdfPTable(2);
tbl14.SetWidths(clmwidths111);
tbl14.WidthPercentage = 70;
tbl14.HorizontalAlignment = Element.ALIGN_CENTER;
tbl14.SpacingBefore = 25;
tbl14.SpacingAfter = 10;
tbl14.DefaultCell.Border = 1;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
cell = new PdfPCell(new Phrase(((Literal)row.FindControl("Totalprem1")).Text, bodyFont2));
cell.HorizontalAlignment = 0;
cell.Colspan = 1;
cell.Border = 0;
tbl14.AddCell(cell);
cell = new PdfPCell(new Phrase(((Literal)row.FindControl("Totalcom1")).Text, bodyFont2));
cell.HorizontalAlignment = 2;
cell.Colspan = 1;
cell.Border = 0;
tbl14.AddCell(cell);
}
}

how to hide the border of table in itext shrp pdf in asp.net MVC vs12?

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/

Categories

Resources