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);
}
Related
In my case, I need to let the user shoose the folder and the file name.
I know that I have to make some changes to "PdfWriterGetInstance".
Then how can I change my code to make it as I want:
Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("Stock.pdf", FileMode.Create));
doc.Open();
Paragraph paragraphe = new Paragraph("Raport of Stock\n\n");
paragraphe.Alignment = Element.ALIGN_CENTER;
doc.Add(paragraphe);
DateTime now = new DateTime();
Paragraph paragraphe3 = new Paragraph(" " + now.ToString()+"\n\n\n");
paragraphe.Alignment = Element.ALIGN_LEFT;
doc.Add(paragraphe3);
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
//add the header
for (int j=0; j < dataGridView1.Columns.Count; j++)
{
table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText));
}
//Flag the first row as a header
table.HeaderRows = 1;
for(int i = 0; i< dataGridView1.Rows.Count; i++)
{
for(int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (dataGridView1[k, i].Value != null)
{
table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString()));
}
}
}
doc.Add(table);
Paragraph paragraphe1 = new Paragraph(" " + "Total items: " +quaq1.ToString());
paragraphe.Alignment = Element.ALIGN_LEFT;
doc.Add(paragraphe1);
Paragraph paragraphe2 = new Paragraph(" " + "Total Price: " + tot1.ToString());
paragraphe.Alignment = Element.ALIGN_LEFT;
doc.Add(paragraphe2);
doc.Close();
Rq: I'm using iTextSharp Assembly.
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...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Using ItextSharp C# how to create a multipage report with necessary text formatting Header and footer?
Is this possible using HTML template with css?
You can use iTextSharp.dll for creating PDFs through your c# program. You can create lists, bullet points, tables and much more with the help of this. I have used it in many of enterprise level applications for creating reports in PDF.
For Example, to create a following document
You'll need this code:
Document doc = new Document(iTextSharp.text.PageSize.A4_LANDSCAPE, 25, 25, 43, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("C:\\test\\Updated_INVOICE#" + textBox45.Text + ".pdf", FileMode.Create));
doc.Open();
var myFont11 = FontFactory.GetFont("NewJune", 18, BaseColor.BLACK);
var p1 = new Paragraph("AK TRAVEL GLOBAL LTD.\n", myFont11);
doc.Add(p1);
var AddFont = FontFactory.GetFont("NewJune", 8, BaseColor.BLACK);
var p23 = new Paragraph("91 HIGH ST\nDUDLEY DY1 1QP\nPhone 01384 255777 Fax 08714 310690", AddFont);
p23.Alignment = 23;
doc.Add(p23);
var myFont = FontFactory.GetFont("NewJune", 12, BaseColor.BLACK);
var myFont1 = FontFactory.GetFont("NewJune", 30, BaseColor.GRAY);
var p2 = new Paragraph("INVOICE\n\n", myFont1);
p2.Alignment = 2;
doc.Add(p2);
var p3 = new Paragraph("INVOICE # " + textBox45.Text.ToString() + "\n", myFont);
p3.Alignment = 2;
doc.Add(p3);
var p4 = new Paragraph("DATE: " + DateTime.Now.ToString(), myFont);
p4.Alignment = 2;
doc.Add(p4);
var myFont12 = FontFactory.GetFont("NewJune", 26, BaseColor.BLACK);
var pg1 = new Paragraph("BILL NUMBER - " + textBox45.Text, myFont12);
pg1.Alignment = 1;
doc.Add(pg1);
PdfPTable table1 = new PdfPTable(2);
table1.SpacingBefore = 50;
var myFont111 = FontFactory.GetFont("NewJune", 25, BaseColor.BLACK);
table1.AddCell(new PdfPCell(new Phrase("TO:", myFont111)));
table1.AddCell(new PdfPCell(new Phrase("SHIP TO:", myFont111)));
table1.AddCell(getCell("", PdfPCell.ALIGN_LEFT));
table1.AddCell(getCell("", PdfPCell.ALIGN_RIGHT));
table1.AddCell(getCell(textBox53.Text, PdfPCell.ALIGN_LEFT));
table1.AddCell(getCell(textBox50.Text, PdfPCell.ALIGN_RIGHT));
table1.AddCell(getCell(textBox52.Text, PdfPCell.ALIGN_LEFT));
table1.AddCell(getCell(textBox49.Text, PdfPCell.ALIGN_RIGHT));
table1.AddCell(getCell(textBox51.Text, PdfPCell.ALIGN_LEFT));
table1.AddCell(getCell(textBox48.Text, PdfPCell.ALIGN_RIGHT));
table1.AddCell(getCell("", PdfPCell.ALIGN_LEFT));
table1.AddCell(getCell(textBox47.Text, PdfPCell.ALIGN_RIGHT));
doc.Add(table1);
PdfPTable table = new PdfPTable(6);
table.SpacingBefore = 20;
table.DefaultCell.BorderWidth = 2;
table.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(new Phrase("ITEM"));
cell.HorizontalAlignment = 1;
table.AddCell(cell);
PdfPCell cell1 = new PdfPCell(new Phrase("DESCRIPTION"));
cell1.Colspan = 3;
cell1.HorizontalAlignment = 1;
table.AddCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("UNIT PRICE"));
cell2.HorizontalAlignment = 1;
table.AddCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("TOTAL"));
cell3.HorizontalAlignment = 1;
table.AddCell(cell3);
//New Row
PdfPCell cell4 = new PdfPCell(new Phrase(textBox41.Text.ToString()));
cell4.HorizontalAlignment = 1;
//cell4.Rowspan = 5;
table.AddCell(cell4);
PdfPCell cell5 = new PdfPCell(new Phrase(textBox43.Text.ToString() + "KG\n" + textBox40.Text.ToString()));
cell5.HorizontalAlignment = 0;
cell5.Colspan = 3;
//cell5.Rowspan = 5;
table.AddCell(cell5);
PdfPCell cell6 = new PdfPCell(new Phrase(textBox44.Text.ToString()));
cell6.HorizontalAlignment = 1;
//cell6.Rowspan = 5;
table.AddCell(cell6);
PdfPCell cell7 = new PdfPCell(new Phrase("£" + textBox42.Text.ToString()));
cell7.HorizontalAlignment = 1;
//cell7.Rowspan = 5;
table.AddCell(cell7);
//New Row
PdfPCell cell34 = new PdfPCell(new Phrase(textBox67.Text.ToString()));
cell34.HorizontalAlignment = 1;
//cell4.Rowspan = 5;
table.AddCell(cell34);
PdfPCell cell35 = new PdfPCell(new Phrase(textBox69.Text.ToString() + "KG\n" + textBox66.Text.ToString()));
cell35.HorizontalAlignment = 0;
cell35.Colspan = 3;
//cell5.Rowspan = 5;
table.AddCell(cell35);
PdfPCell cell36 = new PdfPCell(new Phrase(textBox70.Text.ToString()));
cell36.HorizontalAlignment = 1;
//cell6.Rowspan = 5;
table.AddCell(cell36);
PdfPCell cell37 = new PdfPCell(new Phrase("£" + textBox68.Text.ToString()));
cell37.HorizontalAlignment = 1;
//cell7.Rowspan = 5;
table.AddCell(cell37);
//new row
PdfPCell cell8 = new PdfPCell(new Phrase("SUBTOTAL"));
cell8.HorizontalAlignment = 2;
cell8.Colspan = 5;
table.AddCell(cell8);
PdfPCell cell9 = new PdfPCell(new Phrase("£" + textBox37.Text.ToString()));
cell9.HorizontalAlignment = 1;
table.AddCell(cell9);
PdfPCell cell10 = new PdfPCell(new Phrase("BAG"));
cell10.HorizontalAlignment = 2;
cell10.Colspan = 5;
table.AddCell(cell10);
PdfPCell cell11 = new PdfPCell(new Phrase("£" + (float.Parse(textBox36.Text.ToString()) * 3).ToString()));
cell11.HorizontalAlignment = 1;
table.AddCell(cell11);
PdfPCell cell12 = new PdfPCell(new Phrase("SHIPPING & HANDLING"));
cell12.HorizontalAlignment = 2;
cell12.Colspan = 5;
table.AddCell(cell12);
PdfPCell cell13 = new PdfPCell(new Phrase("£" + textBox39.Text.ToString()));
cell13.HorizontalAlignment = 1;
table.AddCell(cell13);
PdfPCell cell14 = new PdfPCell(new Phrase("TOTAL DUE"));
cell14.HorizontalAlignment = 2;
cell14.Colspan = 5;
table.AddCell(cell14);
PdfPCell cell15 = new PdfPCell(new Phrase("£" + textBox38.Text.ToString()));
cell15.HorizontalAlignment = 1;
table.AddCell(cell15);
doc.Add(table);
var mFont = FontFactory.GetFont("NewJune", 18, BaseColor.BLACK);
var pr = new Paragraph("\n\nTerms & Conditions\n", mFont);
doc.Add(pr);
RomanList romanList = new RomanList(true, 20);
romanList.IndentationLeft = 30f;
var lFont = FontFactory.GetFont("NewJune", 8, BaseColor.BLACK);
char[] diff = { '#' };
string cat = string.Empty;
foreach (InvoiceModel invc in Invoices_lst)
{
if (invc.InvoiceNo == textBox45.Text)
cat = invc.cat;
}
if (cat == "air")
{
q = air_inv;
}
else
{
q = sea_inv;
}
string[] words = q.Split(diff);
for (int i = 0; i < words.Length; i++)
{
romanList.Add(new ListItem(words[i], lFont));
}
doc.Add(romanList);
doc.Close();
You can find details about how you can use it HERE
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 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/