export grid view to existing pdf templete c# - 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);
}
}

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

iTextSharp PDF table too much columns Issue

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.

Constant column for multiple rows

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.

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/

Add Table to FlowDocument In Code Behind

I have tried this.....
_doc = new FlowDocument();
Table t = new Table();
for (int i = 0; i < 7; i++)
{
t.Columns.Add(new TableColumn());
}
TableRow row = new TableRow();
row.Background = Brushes.Silver;
row.FontSize = 40;
row.FontWeight = FontWeights.Bold;
row.Cells.Add(new TableCell(new Paragraph(new Run("I span 7 columns"))));
row.Cells[0].ColumnSpan = 6;
_doc2.Blocks.Add(t);
When I go to view this document the table never shows.....although the border image and document title that I add to this document before adding this table outputs fine.
You add the Columns to the Table, but where is the code that adds the row? It just isn't connected.
Add something like:
...
var rg = new TableRowGroup();
rg.Rows.Add(row);
t.RowGroups.Add(rg);
_doc2.Blocks.Add(t);

Categories

Resources