I was trying to create a pdf document which should show document in table format in C#.
So at first I added iTextSharp library to my application.
Then three namespaces:
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html;
Then added code:
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc,newFileStream("/hello.pdf",FileMode.Create));
doc.Open();
Then tried to add table to document using Table class(also tried HtmlTable), but it was not working, because when I tried to add that table to document
doc.Add(table1);
it was showing error:
“The best overloaded method match for iTextSharp.text.Document.Add(iTextSharp.text.Ielement) has some invalid arguments.”
Because the Table is a class of System.Web.UI.WebControls and HtmlTable is a class of
System.Web.UI.HtmlControls.
Then I used PdfPTable and it worked. Here you can create table without using row element by assigning the number of columns while creating table like:
PdfPTable table1 = newPdfPTable(2);
Here no. of columns is 2.
You can add a child table into a parent table like:
PdfPCell cell2A = newPdfPCell(table2);
Here is the code sample:
I was trying to create a pdf document which should show document in table format in C#.
So at first I added iTextSharp library to my application.
Then three namespaces:
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html;
Then added code:
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc,newFileStream("/hello.pdf",FileMode.Create));
doc.Open();
Then tried to add table to document using Table class(also tried HtmlTable), but it was not working, because when I tried to add that table to document
doc.Add(table1);
it was showing error:
“The best overloaded method match for iTextSharp.text.Document.Add(iTextSharp.text.Ielement) has some invalid arguments.”
Because the Table is a class of System.Web.UI.WebControls and HtmlTable is a class of System.Web.UI.HtmlControls.
Then I used PdfPTable and it worked. Here you can create table without using row element by assigning the number of columns while creating table like:
PdfPTable table1 = newPdfPTable(2);
Here no. of columns is 2.
You can add a child table into a parent table like:
PdfPCell cell2A = newPdfPCell(table2);
Here is the code sample:
Document doc = newDocument(PageSize.A4);
PdfWriter.GetInstance(doc,newFileStream("/hello.pdf",FileMode.Create));
doc.Open();
PdfPTable table1 = newPdfPTable(2);
table1.WidthPercentage = 90;
PdfPCell cell11= newPdfPCell();
cell11.AddElement(newParagraph("Receipt ID : "+receiptNo));
cell11.AddElement(newParagraph("Date : " + date));
cell11.AddElement(newParagraph("Photo Status : " + status));
cell11.VerticalAlignment = Element.ALIGN_LEFT;
PdfPCell cell12=newPdfPCell();
cell12.AddElement(newParagraph("Transaction ID : "+tranid));
cell12.AddElement(newParagraph("Expected Date Of Delivery : " + expDate));
cell12.VerticalAlignment = Element.ALIGN_RIGHT;
table1.AddCell(cell11);
table1.AddCell(cell12);
PdfPTable table2 = newPdfPTable(3);
//One row added
PdfPCell cell21 = newPdfPCell();
cell21.AddElement(newParagraph("Photo Type"));
PdfPCell cell22 = newPdfPCell();
cell22.AddElement(newParagraph("No. of Copies"));
PdfPCell cell23 = newPdfPCell();
cell23.AddElement(newParagraph("Amount"));
table2.AddCell(cell21);
table2.AddCell(cell22);
table2.AddCell(cell23);
//New Row Added
PdfPCell cell31 = newPdfPCell();
cell31.AddElement(newParagraph(type));
cell31.FixedHeight = 300.0f;
PdfPCell cell32 = newPdfPCell();
cell32.AddElement(newParagraph(noOfCopy));
cell32.FixedHeight = 300.0f;
PdfPCell cell33 = newPdfPCell();
cell33.AddElement(newParagraph("20.00 * "+noOfCopy+" = "+(20*Convert.ToInt32(noOfCopy))+".00"));
cell33.FixedHeight = 300.0f;
table2.AddCell(cell31);
table2.AddCell(cell32);
table2.AddCell(cell33);
PdfPCell cell2A = newPdfPCell(table2);
cell2A.Colspan = 2;
table1.AddCell(cell2A);
PdfPCell cell41 = newPdfPCell();
cell41.AddElement(newParagraph("Name : " + custName));
cell41.AddElement(newParagraph("Advance : " + advance));
cell11.VerticalAlignment = Element.ALIGN_LEFT;
PdfPCell cell42 = newPdfPCell();
cell42.AddElement(newParagraph("Customer ID : " + custId));
cell42.AddElement(newParagraph("Balance : " + balance));
cell42.VerticalAlignment = Element.ALIGN_RIGHT;
table1.AddCell(cell41);
table1.AddCell(cell42);
doc.Add(table1);
doc.Close();
Response.Redirect("/hello.pdf");