How to create multi page report using c# [closed] - c#

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

Related

How to save created PDF file into dynamically file

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.

while creating pdf new page get first page data using itextsharp c#

I am trying to create a pdf in the attached format by using grouping using c#. The first page is fine but next page still first-page first column name exists. while creating a pdf new page get the first page data
I used
DeleteBodyRows()
for removing table rows but still the same :
foreach (DataRow row in dataTable.Rows)
{
//write in new row
var datatable = dataTable;
var departments = from r in datatable.Rows.OfType<DataRow>()
group r by r["emp_reader_id"] into g
select new { emp_reader_id = g.Key, Data = g };
foreach (var department in departments)
{
foreach (var roww in department.Data)
{
iii++;
if (ii == 0)
{
PdfPCell cell1333 = new PdfPCell(new iTextSharp.text.Phrase("Name " + ":" + roww.ItemArray[1].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell1333.Colspan = 8;
cell1333.Border = 0;
cell1333.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
table.AddCell(cell1333);
PdfPCell cell1 = new PdfPCell(new iTextSharp.text.Phrase("E.Code " + ":" + roww.ItemArray[2].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell1.Colspan = 8;
cell1.Border = 0;
cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
table.AddCell(cell1);
PdfPCell cell144 = new PdfPCell(new iTextSharp.text.Phrase("Department " + ":" + roww.ItemArray[4].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell144.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
cell144.Colspan = 8;
cell144.Border = 0;
cell144.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(cell144);
PdfPCell cell155 = new PdfPCell(new iTextSharp.text.Phrase("Designation " + ":" + roww.ItemArray[5].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell155.Colspan = 8;
cell155.Border = 0;
cell155.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
cell155.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(cell155);
PdfPCell cell166 = new PdfPCell(new iTextSharp.text.Phrase("Name Of Facility " + ":" + roww.ItemArray[6].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell166.Colspan = 8;
cell166.Border = 0;
cell166.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
cell166.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(cell166);
DataTable dtp1 = new DataTable();
dtp1.Columns.Add("Date", typeof(string));
dtp1.Columns.Add("F1", typeof(string));
dtp1.Columns.Add("F2", typeof(string));
dtp1.Columns.Add("F3", typeof(string));
dtp1.Columns.Add("F4", typeof(string));
dtp1.Columns.Add("Hours", typeof(string));
dtp1.Columns.Add("Remarks", typeof(string));
for (int i = 0; i < 7; i++)
{
PdfPCell cell = new PdfPCell(new iTextSharp.text.Phrase(dtp.Columns[i].ColumnName, new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.ITALIC, iTextSharp.text.Color.WHITE)));
cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#2980b9"));
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
table.AddCell(cell);
}
}
ii++;
DateTime date = DateTime.Parse(roww.ItemArray[3].ToString(), System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
string strDate = String.Format("{0:dd/MM/yyyy}", date);
PdfPCell cell19 = new PdfPCell(new iTextSharp.text.Phrase(strDate, new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK)));
cell19.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
table.AddCell(cell19);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
}
ii = 0;
pdfDoc.Add(table);
table.DeleteBodyRows();
pdfDoc.NewPage();
}
break;
}
All new page contains the first page's first row continuously. Above is the code I tried.
Any help ?
Thanks!
add below line next to table.DeleteBodyRows()
To remove first row
table.DeleteRow(0);

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

Repeat Heading to table in pdf

I am using the Itextsharp PDF tool to generate PDF using asp.net and C# , In that in one PDFPtable the last row data is repeating on next page means part of a table forwarded to next page. so want to show header for that Table on next page.
This is my code.
Document doc = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 20, 10);
string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf" + Guid.NewGuid().ToString("N") + ".pdf";
try
{
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
FileStream fs = new FileStream(pdfFilePath, FileMode.Create);
//
PdfWriter wri = PdfWriter.GetInstance(doc, fs);
//Header Section
string strImagePath = Server.MapPath("Images");
string strReportName = oReportDTO.strReportName + " " + oReportDTO.oDateRange.FromDate.ToString(DateFormat);
Chunk headerchunk = new Chunk(strReportName, new Font(1, 8.0f));
HeaderFooter oHeader = new HeaderFooter(new Phrase(headerchunk), false);
oHeader.Border = Rectangle.NO_BORDER;
oHeader.Alignment = 1;
doc.Header = oHeader;
//Footer Section
string name ="Logged in as : " +currentLoggedInUser.UserName + new string(' ',70)+ "Page Number : " + doc.PageNumber;
Chunk Footerchunk1 = new Chunk(name, new Font(1, 5.0f));
HeaderFooter oFooter1 = new HeaderFooter(new Phrase(Footerchunk1), true);
oFooter1.Border = Rectangle.NO_BORDER;
oFooter1.Alignment = 1;
doc.Footer = oFooter1;
iTextSharp.text.Image imgFooter = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromFile(strImagePath + "/TransRisk Logo168x97.png"), System.Drawing.Imaging.ImageFormat.Jpeg);
imgFooter.ScaleAbsolute(80, 50);
Chunk footerchunk = new Chunk(imgFooter, 260.0f, 0.0f);
HeaderFooter oFooter = new HeaderFooter(new Phrase(name),new Phrase(footerchunk));
oFooter.Border =Rectangle.NO_BORDER;
doc.Footer = oFooter;
doc.Open();//Open Document to write
Font font8 = FontFactory.GetFont("ARIAL", 7);
HtmlTable dt = customMatrixReport;
if (dt.Rows.Count > 0)
{
//Craete instance of the pdf table and set the number of column in that table
int startColumnPosition = 1;
int endColumnPosition = 13;//End Column number in Pdf Page
int NoOfReports = Convert.ToInt32(Math.Ceiling((decimal)(dt.Rows[0].Cells.Count - 1) / endColumnPosition));//Count How many Pages to show
int pageRowCount = 0;
List<PdfPCell> lstHeaderCells = new List<PdfPCell>();
PdfPTable oPdfTable = null;
PdfPCell oPdfPCell = null;
for (int report = 1; report <= NoOfReports; ++report)
{
doc.Add(oHeader);
//ColumnText.ShowTextAligned(
int noOfColumns = -1;
if (endColumnPosition > dt.Rows[0].Cells.Count - 1) { endColumnPosition = dt.Rows[0].Cells.Count - 1; noOfColumns = (endColumnPosition - startColumnPosition) + 2; oPdfTable = new PdfPTable(noOfColumns); }
else
{
oPdfTable = new PdfPTable(14);
//Widths Count
noOfColumns = 14;
}
oPdfTable.TotalWidth = 650f;
List<float> lstwidths = new List<float>();
lstwidths.Add(100f);
for (int i = 2; i <= noOfColumns; ++i)
{
lstwidths.Add(80f);
}
oPdfTable.SetTotalWidth(lstwidths.ToArray());
pageRowCount = 0;
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
//PageRowCount
pageRowCount = pageRowCount + 1;
//Description celll
if (rows == 0 )
{
//Background color for table header
oPdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows].Cells[0].InnerText, new Font(1, 8.0f, 1, Color.WHITE))));
oPdfPCell.BackgroundColor = new Color(118, 147, 199);
oPdfTable.AddCell(oPdfPCell);
}
else
{
//background color for Table cells
oPdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows].Cells[0].InnerText,new Font(1, 8.0f))));
oPdfPCell.BackgroundColor = new Color(232, 237, 255);
oPdfTable.AddCell(oPdfPCell);
}
//for header cel
if (rows == 0)
{
lstHeaderCells.Add(oPdfPCell);
}
for (int column = startColumnPosition; column <= endColumnPosition; column++)
{
if (rows == 0)
{
oPdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows].Cells[column].InnerText, new Font(1, 8.0f, 1, Color.WHITE))));
oPdfPCell.BackgroundColor = new Color(118, 147, 199);
oPdfTable.AddCell(oPdfPCell);
}
else
{
oPdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows].Cells[column].InnerText, new Font(1, 8.0f))));
oPdfPCell.BackgroundColor = new Color(232, 237, 255);
oPdfPCell.Column.Alignment = 2;
oPdfTable.AddCell(oPdfPCell);
}
if (rows == 0)
{
lstHeaderCells.Add(oPdfPCell);
}
}
if (pageRowCount >= 40 && rows != (dt.Rows.Count - 1))
{
pageRowCount = 0; doc.Add(oPdfTable); doc.NewPage(); doc.Add(oHeader); oPdfTable = new PdfPTable(noOfColumns); oPdfTable.SetTotalWidth(lstwidths.ToArray()); foreach (PdfPCell oHeaderCell in lstHeaderCells) { oPdfTable.AddCell(oHeaderCell); }
}
}
startColumnPosition = endColumnPosition + 1;
endColumnPosition = endColumnPosition + 13;
oPdfTable.SpacingBefore = 10;
oPdfTable.SpacingAfter = 10;
doc.Add(oPdfTable);
//doc.NewPage();
}
}
else
doc.Add(oHeader);
}
finally
{
doc.Close();
}
help me..
Change this line-
if (endColumnPosition > dt.Rows[0].Cells.Count - 1) { endColumnPosition = dt.Rows[0].Cells.Count - 1; noOfColumns = (endColumnPosition - startColumnPosition) + 2; oPdfTable = new PdfPTable(noOfColumns); }
To this-
if (endColumnPosition > dt.Rows[0].Cells.Count - 1) { endColumnPosition = dt.Rows[0].Cells.Count - 1; noOfColumns = (endColumnPosition - startColumnPosition) + 2; oPdfTable = new PdfPTable(noOfColumns); oPdfTable.HeaderRows = 1;}
After this line-
oPdfTable = new PdfPTable(14);
Add this-
oPdfTable.HeaderRows = 1;
Change this line-
pageRowCount = 0; doc.Add(oPdfTable); doc.NewPage(); doc.Add(oHeader); oPdfTable = new PdfPTable(noOfColumns); oPdfTable.SetTotalWidth(lstwidths.ToArray()); foreach (PdfPCell oHeaderCell in lstHeaderCells) { oPdfTable.AddCell(oHeaderCell); }
To this-
pageRowCount = 0; doc.Add(oPdfTable); doc.NewPage(); doc.Add(oHeader); oPdfTable = new PdfPTable(noOfColumns); oPdfTable.HeaderRows = 1; oPdfTable.SetTotalWidth(lstwidths.ToArray()); foreach (PdfPCell oHeaderCell in lstHeaderCells) { oPdfTable.AddCell(oHeaderCell); }

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