I am using PdfPTable to generate PDF's and following is my relevant code which loops through the data from db.
The problem here is if there are 10 or more columns the PDF design gets horrible. Is there any way in iTextSharp that Columns gets auto shifted in PDF by specifying Columns i require & rest columns are shifted on next page.
// Table Head
foreach (var q in tempColumnNames)
{
PdfPCell cell = new PdfPCell(new Phrase(q, fntTableFont));
table.AddCell(cell);
}
// Table Body
for (int i = 0; i < model.Count; i++)
{
for (int j = 0; j < model[i].Count(); j++)
{
PdfPCell cell = new PdfPCell(new Phrase(model[i][j].ToString(), fntTableFont));
table.AddCell(cell);
}
}
There's nothing automatic but you can use PdfPtable.WriteSelectedRows to write only the required sections.
Related
I am trying to convert a pdf to an excel spreadsheet. I have the code to extract the text with itextsharp ITextChunkLocationStrategy.
I now have a table with one column with text (split by chunks)and another column with chunks rectangle dimensions and locations.
I now need to place the text chunks into the right location on an excel document.
below is code that i have found on googling. but i do not know any way of telling excel which cells to place the text into - based on the coordinates
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
var dty = (from row in dt.AsEnumerable()
orderby row.Field<float>("x")
where row.Field<int>("yRank") == rowNo
select row);
var tbl = dty.CopyToDataTable();
for (int y = 0; y < tbl.Rows.Count; y++)
{
ws.Cells[rowNo, y + 1] = tbl.Rows[y]["text"].ToString();
}
}
rowNo++;
I am having a formatting issue when exporting my data table to Excel. The data is exported as it should, however if you look at my image, sometimes the cell height is increased and I am not sure why. I want the data to look the same from row to row. This is the syntax I am using to export
for (var i = 0; i < tbl.Columns.Count; i++)
workSheet.Cells[1, i + 1] = tbl.Columns[i].ColumnName;
for (var i = 0; i < tbl.Rows.Count; i++)
{
for (var j = 0; j < tbl.Columns.Count; j++)
workSheet.Cells[i + 2, j + 1] = tbl.Rows[i][j];
}
And here is an image that shows my formatting issues that I want to find a way to overcome. Can someone show me what syntax I need in order to have all row height/width the same?
Issue Image
EDIT
I tried this, but it throws an error and does not format as needed
The error is
System.Exception
Excel.Range range1 = workSheet.get_Range("A2", "S2000");
range1.EntireRow.Height.Value = 15;
You are over complicating it. Just use the UsedRange property. If workSheet is your actual variable name and 15 is the actual height you want to set, the below will work:
workSheet.UsedRange.EntireRow.RowHeight = 15;
I'm using itextSharp to export a DataTable to a pdf table. I can export the data to a pdf table using the sample code i have posted below. The DataTable contains close to 21 columns.
The first column in the pdf (DataTable) might contain similar values for any number of rows. If the data values in first column for a group of rows is similar, i want to merge the first 3 columns of those rows as one cell.
I'm having trouble modifying the code below to achieve this.
public iTextSharp.text.Table GetItextTable(DataTable dtChartData, string reportType)
{
int intCols = dtChartData.Columns.Count; //Total number of columns
int intRows = dtChartData.Rows.Count; //Total number of rows
iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(intCols, intRows);
try
{
pdfTable.BorderWidth = 1;
pdfTable.Width = 100;
pdfTable.Padding = 1;
pdfTable.Spacing = 1;
/*creating table headers */
for (int i = 0; i < intCols; i++)
{
iTextSharp.text.Cell cellCols = new iTextSharp.text.Cell();
iTextSharp.text.Font ColFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07,
iTextSharp.text.Font.BOLD);
for (int l = 0; l < dtChartData.Columns.Count; l++)
{
if (dtChartData.Columns[l].ColumnName.Contains("_"))
{
dtChartData.Columns[l].ColumnName = dtChartData.Columns[l].ColumnName.Replace("_", " ");
}
}
iTextSharp.text.Chunk chunkCols = new iTextSharp.text.Chunk(dtChartData.Columns[i].ColumnName,ColFont);
cellCols.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
if ((chunkCols.ToString().ToLower() == "ReportDetails"))
{
cellCols.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
}
}
/* loop that take values from every row in datatable and insert in itextsharp table */
for (int k = 0; k < intRows; k++)
{
for (int j = 0; j < intCols; j++)
{
iTextSharp.text.Cell cellRows = new iTextSharp.text.Cell();
iTextSharp.text.Font RowFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07);
iTextSharp.text.Chunk chunkRows = new iTextSharp.text.Chunk(dtChartData.Rows[k][j].ToString(),RowFont);
cellRows.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}
}
catch (Exception ex)
{
//error handling code here removed
}
return pdfTable;
}
Have you tried to change the Colspan?
iTextSharp.text.Cell cell = new iTextSharp.text.Cell();
cell.AddElement(new Paragraph("colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
In this case, cell will span three columns.
try this. i just edited your code, not checked
here i have edited 1 line of your code and added a new line (total 2 lines of change only).
dont forget to combine headding row like this, if you need
/* loop that take values from every row in datatable and insert in itextsharp table */
for (int k = 0; k < intRows; k++)
{
for (int j = 2; j < intCols; j++)// EDIT: if all first 3 cols are same, then starts with 2
{
iTextSharp.text.Cell cellRows = new iTextSharp.text.Cell();
if(j == 2) cellRows.Colspan = 3;// ADD: it'll gives a 3 times long cell
iTextSharp.text.Font RowFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07);
iTextSharp.text.Chunk chunkRows = new iTextSharp.text.Chunk(dtChartData.Rows[k][j].ToString(),RowFont);
cellRows.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}
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);
}
}
Is it possible to update style for all cells from DataGridView without iteration over them like example below?
for (int i = 0; i < dgv.Columns.Count; i++)
for (int j = 0; j < dgv.Rows.Count; j++)
if (dgv[i, j].Style != style)
dgv[i, j].Style = style;
My question is an actual due to slow speed of slyle updating for all cells.
If you want to apply the same style to all the cells, simply use the DefaultCellStyle of the datagridview.
dataGridView.DefaultCellStyle.BackColor = Color.Green;
The answer of Killercam would be helpful when you want to apply different styles to different cells on the same rows.
You can do this on a row-by-row basis:
foreach (DataGridViewRow row in dataGridView.Rows)
Row.DefaultCellStyle.BackColor = Color.Red;
or
for (int r = 0; r < dataGridView.Rows.Count; r++)
dataGridView.Rows[r].DefaultCellStyle.BackColor = Color.Red;
where using the DefaultCellStyle you can set other properties as well.
I hope this helps.