making table in itextsharp pdf - c#
i am creating a pdf which contain a header and footer section,
in my body section, i want a table which contain my datagridview data. pdf may longer then 3 or 4 page. i have made it, but the problem is that, my table is showing 2 times in a page. you can see in capture which is given bellow.enter image description here
my code is
public partial class Footer : PdfPageEventHelper
{
iTextSharp.text.Image image;
public override void OnEndPage(PdfWriter writer, Document doc1)
{
Document doc = new Document(PageSize.A4, 30f, 30f, 42f, 30f);
doc.Open();
iTextSharp.text.Image footer = iTextSharp.text.Image.GetInstance("dslmfooter.png");
footer.ScaleAbsoluteHeight(40f);
footer.ScaleAbsoluteWidth(520f);
doc.Add(footer);
footer.Alignment = Element.ALIGN_CENTER;
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = 300;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(footer);
cell.Border = 0;
cell.PaddingLeft = 10;
footerTbl.AddCell(cell);
footerTbl.WriteSelectedRows(0, -1, 25, 50, writer.DirectContent);
}
}
public partial class Header : PdfPageEventHelper
{
iTextSharp.text.Image image;
public override void OnStartPage(PdfWriter writer, Document doc1)
{
var color_table = new BaseColor(0, 0, 128);
BaseFont Kalpurush = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font f_12_normal = new iTextSharp.text.Font(Kalpurush, 11, iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font f_12_color = new iTextSharp.text.Font(Kalpurush, 12, iTextSharp.text.Font.NORMAL, color_table);
Document doc = new Document(PageSize.A4, 30f, 30f, 42f, 30f);
doc.Open();
iTextSharp.text.Image Header = iTextSharp.text.Image.GetInstance("dslmheader.png");
Header.ScaleAbsoluteHeight(170f);
Header.ScaleAbsoluteWidth(550f);
doc.Add(Header);
Header.Alignment = Element.ALIGN_CENTER;
PdfPTable footerTb2 = new PdfPTable(1);
footerTb2.TotalWidth = 300;
footerTb2.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell1 = new PdfPCell(Header);
cell1.Border = 0;
cell1.PaddingLeft = 10;
footerTb2.AddCell(cell1);
footerTb2.WriteSelectedRows(0, -1, 10, 830, writer.DirectContent);
}
}
private void print_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(#"server=" + _str1[0] + ";username=" + _str1[1] + ";password=" + _str1[2] + ";database=" + _str1[3]);
MySqlCommand cmd;
DataGridViewRow row = new DataGridViewRow();
MySqlDataReader reader;
string str = "";
string order = "";
///checking////
int dq = 0;
for (int i = 0; i < dgv_party_rod.Rows.Count; i++)
{
row = dgv_party_rod.Rows[i];
if (Convert.ToBoolean(row.Cells[0].Value) == true)
{
dq++;
str = row.Cells["item"].Value.ToString();
order = Convert.ToString(row.Cells["order_no"].Value);
}
}
if (dq > 0)
{
/////////////////itextsharp///////////////////
Document doc = new Document(PageSize.A4, 30f, 30f, 42f, 30f);
// BaseFont sm = GetFont("Kalpurush");
var FontColour = new BaseColor(0, 74, 121);
var colour = new BaseColor(0, 122, 204);
var colour1 = new BaseColor(232, 17, 35);
var color_table = new BaseColor(0, 0, 128);
var color_green = new BaseColor(29, 163, 98);
BaseFont Kalpurush = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font f_15_bold = new iTextSharp.text.Font(Kalpurush, 15, iTextSharp.text.Font.BOLD);
iTextSharp.text.Font f_15_normal = new iTextSharp.text.Font(Kalpurush, 15, iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font f_12_normal = new iTextSharp.text.Font(Kalpurush, 11, iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font f_12_color = new iTextSharp.text.Font(Kalpurush, 12, iTextSharp.text.Font.NORMAL, color_table);
iTextSharp.text.Font f_16_normal = new iTextSharp.text.Font(Kalpurush, 16, iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font f_16_bold = new iTextSharp.text.Font(Kalpurush, 16, iTextSharp.text.Font.BOLD, colour1);
iTextSharp.text.Font f_16_bold_green = new iTextSharp.text.Font(Kalpurush, 16, iTextSharp.text.Font.BOLD, color_green);
Random rnd = new Random();
int pdfname = rnd.Next(1, 10000);
FileStream os = new FileStream(#"D:\dlsmpdf\print" + pdfname.ToString() + ".rod", FileMode.Create);
using (os)
{
PdfWriter writer = PdfWriter.GetInstance(doc, os);
//open the stream
doc.Open();
doc.NewPage();
doc.SetMargins(20f, 40f, 200f, 100f);
// doc.setMargin
// Document doc = new Document(PageSize.A4, 30f, 30f, 42f, 30f);
//var output = new FileStream(#"D:\dlsmpdf\print", FileMode.Create);
writer.PageEvent = new Header();
writer.PageEvent = new Footer();
// Open the Document for writing
doc.Open();
// PdfContentByte tab = writer.DirectContent;
// PdfPTable tabe = new PdfPTable(4);
// tabe.TotalWidth = 400f;
// //fix the absolute width of the table
// tabe.LockedWidth = true;
// //relative col widths in proportions - 1/3 and 2/3
// float[] widths = new float[] { 100f, 100f, 100f, 100f };
// tabe.SetWidths(widths);
// tabe.HorizontalAlignment = 0;
// //leave a gap before and after the table
// tabe.SpacingBefore = 20f;
// tabe.SpacingAfter = 30f;
//PdfPCell:
// cmd = new MySqlCommand("SELECT * FROM party_tin WHERE item = '" + str + "' AND order_no = '" + order + "'", con);
// reader = cmd.ExecuteReader();
// reader.Read();
// PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
// cell.Colspan = 3;
// cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
// tabe.AddCell(cell);
// cell = new PdfPCell(new Phrase("Order No:", f_12_normal));
// tabe.AddCell(cell);
// string order1 = reader.GetString("order_no");
// cell = new PdfPCell(new Phrase(order1, f_12_color));
// tabe.AddCell("Col 2 Row 1");
// tabe.AddCell("Col 3 Row 1");
// tabe.AddCell("Col 1 Row 2");
// tabe.AddCell("Col 2 Row 2");
// tabe.AddCell("Col 3 Row 2");
// tabe.WriteSelectedRows(0, -1, 20, 450, tab);
///////////1st table////////////
PdfContentByte ab = writer.DirectContent;
PdfPTable able = new PdfPTable(4);
float[] width = new float[] { 90f, 180f, 80f, 150f };
able.TotalWidth = 500f;
con.Open();
string Query = "SELECT * FROM party_rod WHERE item = '" + str + "' AND order_no = '" + order + "'";
MySqlCommand cmdt = new MySqlCommand(Query, con);
MySqlDataReader myReader;
myReader = cmdt.ExecuteReader();
myReader.Read();
PdfPCell ell = new PdfPCell(new Phrase("Order NO:", f_12_normal));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
string order_no = myReader.GetString("order_no");
ell = new PdfPCell(new Phrase(order_no));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
ell = new PdfPCell(new Phrase("Date:", f_12_normal));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
string date = String.Format("{0:dd/MM/yyyy}", myReader.GetString("order_date"));
ell = new PdfPCell(new Phrase(date));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
ell = new PdfPCell(new Phrase("Name:", f_12_normal));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
string name = myReader.GetString("name");
ell = new PdfPCell(new Phrase(name));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
// 2nd row
ell = new PdfPCell(new Phrase("Address:", f_12_normal));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
string address = myReader.GetString("address");
ell = new PdfPCell(new Phrase(address));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
ell = new PdfPCell(new Phrase("Company:", f_12_normal));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
string company = myReader.GetString("mill_name");
ell = new PdfPCell(new Phrase(company));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
//3rd row
ell = new PdfPCell(new Phrase("Phone No:", f_12_normal));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
string phone = myReader.GetString("phone");
ell = new PdfPCell(new Phrase(phone));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
ell = new PdfPCell(new Phrase("Mill Name:", f_12_normal));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
string mill_name = myReader.GetString("mill_name");
ell = new PdfPCell(new Phrase(mill_name));
ell.HorizontalAlignment = 0;
ell.Border = 0;
able.AddCell(ell);
able.WriteSelectedRows(0, -1, 50, 650, ab);
//reader.Close();
////////////2nd table///////////////
PdfContentByte tab2 = writer.DirectContent;
PdfPTable table2 = new PdfPTable(new float[] { 30f, 30f, 30f, 30f, 30f, 30f, 40f, 40f, 40f, 50f, 50f, 50f });
table2.TotalWidth = 540f;
//header
PdfPCell cell2 = new PdfPCell(new Phrase("Size", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Ton", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Tk of Ton", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("KG", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Tk of KG", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Gram", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Tk of Gram", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("House Labour Charge", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Mill Labour Charge", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Product Price", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Ex due", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Cash", f_12_normal));
cell2.HorizontalAlignment = 1;
cell2.BackgroundColor = new iTextSharp.text.BaseColor(210, 211, 213);
table2.AddCell(cell2);
////rows/////
// con.Open();
double total = 0.0;
double total_tn = 0.0;
double total_tk_of_tn = 0.0;
double total_kg = 0.0;
double total_gram = 0.0;
double total_house = 0.0;
double total_ex_due = 0.0;
double total_cash = 0.0;
double total_due = 0.0;
double total_amount = 0.0;
con.Close();
//double total_amnt = 0.0;
DataGridViewRow rw = new DataGridViewRow();
for (int i = 0; i < dgv_party_rod.Rows.Count; i++)
{
rw = dgv_party_rod.Rows[i];
if (Convert.ToBoolean(row.Cells[0].Value) == true)
{
con.Open();
str = row.Cells["item"].Value.ToString();
order = Convert.ToString(row.Cells["order_no"].Value);
string Query1 = "select * from party_rod WHERE item = '" + str + "' AND order_no = '" + order + "'";
MySqlCommand cmd1 = new MySqlCommand(Query1, con);
MySqlDataReader myReader1;
myReader1 = cmd1.ExecuteReader();
myReader1.Read();
string size = myReader1.GetString("size");
cell2 = new PdfPCell(new Phrase(size));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string acc = myReader1.GetString("ton");
cell2 = new PdfPCell(new Phrase(acc));
cell2.HorizontalAlignment = 1;
// cell2.Border = 0;
table2.AddCell(cell2);
string bank_name = myReader1.GetString("tk_of_ton");
cell2 = new PdfPCell(new Phrase(bank_name));
cell2.HorizontalAlignment = 1;
// cell2.Border = 0;
table2.AddCell(cell2);
string dscrption = myReader1.GetString("kg");
cell2 = new PdfPCell(new Phrase(dscrption));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string taka = myReader1.GetString("tk_of_kg");
cell2 = new PdfPCell(new Phrase(taka));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string f = myReader1.GetString("gram");
cell2 = new PdfPCell(new Phrase(f));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string g = myReader1.GetString("tk_of_gram");
cell2 = new PdfPCell(new Phrase(g));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string hlc = myReader1.GetString("house_labour_charge");
cell2 = new PdfPCell(new Phrase(hlc));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string mlc = myReader1.GetString("mill_labour_charge");
cell2 = new PdfPCell(new Phrase(mlc));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string pp = myReader1.GetString("total_amount");
cell2 = new PdfPCell(new Phrase(pp));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string ed = myReader1.GetString("ex_due");
cell2 = new PdfPCell(new Phrase(ed));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
string cash = myReader1.GetString("cash");
cell2 = new PdfPCell(new Phrase(cash));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
con.Close();
}
}
///total row///
cell2 = new PdfPCell(new Phrase("Total:", f_12_normal));
cell2.HorizontalAlignment = 1;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(total_tn.ToString(), f_12_normal));
cell2.HorizontalAlignment = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(total_tk_of_tn.ToString(), f_12_normal));
cell2.HorizontalAlignment = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(total_kg.ToString(), f_12_normal));
cell2.HorizontalAlignment = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(total_gram.ToString(), f_12_normal));
cell2.HorizontalAlignment = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(total_house.ToString(), f_12_normal));
cell2.HorizontalAlignment = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(total.ToString(), f_12_normal));
cell2.HorizontalAlignment = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(total_ex_due.ToString(), f_12_normal));
cell2.HorizontalAlignment = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(total_cash.ToString(), f_12_normal));
cell2.HorizontalAlignment = 2;
table2.AddCell(cell2);
/////total amount///
total_due += Convert.ToDouble((total + total_house + total_ex_due) - total_cash);
total_amount += Convert.ToDouble((total + total_house + total_ex_due));
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Total Amount:", f_16_normal));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
cell2.Colspan = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(""));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
cell2.Border = 0;
cell2.Colspan = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
/////total amount///
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Total Cash:", f_16_bold_green));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
cell2.Colspan = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(""));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
cell2.Border = 0;
cell2.Colspan = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
/////total amount///
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase("Total Due:", f_16_bold));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
cell2.Colspan = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(""));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
cell2.Border = 0;
cell2.Colspan = 2;
table2.AddCell(cell2);
cell2 = new PdfPCell(new Phrase(" "));
cell2.HorizontalAlignment = 2;
cell2.Border = 0;
table2.AddCell(cell2);
table2.WriteSelectedRows(0, -1, 30, 550, tab2);
doc.Add(table2);
doc.Close();
System.Diagnostics.Process.Start(#"D:\dlsmpdf\print" + pdfname.ToString() + ".rod");
}
}
else
{
MessageBox.Show("আইটেম সিলেক্ট করুন");
}
}
please try to help me everyone!
thank you :-)
This is some iText7 code to generate a table that spans multiple pages.
You should be able to combine it with code to insert headers and footers and have a working example for your usecase:
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SplitRowAtEndOfPage().manipulatePdf(DEST);
}
#Override
protected void manipulatePdf(String dest) throws Exception {
Table table = new Table(1);
table.setWidth(550);
// the number of iterations has been changed in order to provide the same as in itext5 example
for (int i = 0; i < 6; i++) {
Cell cell;
if (i == 5) {
cell = new Cell().add("Three\nLines\nHere");
} else {
cell = new Cell().add(Integer.toString(i));
}
table.addCell(cell);
}
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(612, 237));
doc.add(table);
doc.close();
}
Have a look at http://developers.itextpdf.com/examples/tables/clone-splitting-tables for more examples.
Related
i have scheduled C# exe in task scheduler,when trigerring manually it send mail and pdf but from task scheduler it fails to create and attached pdf
i have scheduled C# exe in task scheduler,when trigerring manually it send mail and pdf attached but from task scheduler it fails to create and attached pdf it is only sending mail below is my code snippets for pdf public static void ExportToPdF_Verify_report(DataTable myDataTable, DataTable dt2, string filename) { DataTable dt = myDataTable; Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 35, 10); Font font13 = FontFactory.GetFont("ARIAL", 13); Font font18 = FontFactory.GetFont("ARIAL", 10, Font.BOLD); Font fonttext = FontFactory.GetFont("ARIAL", 10, Font.NORMAL); try { PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(filename, FileMode.Create)); pdfDoc.Open(); if (dt.Rows.Count > 0) { PdfPTable PdfTable = new PdfPTable(1); PdfTable.TotalWidth = 200f; PdfTable.LockedWidth = true; PdfPCell PdfPCell = new PdfPCell(new Phrase(new Chunk("", font18))); //PdfPCell PdfPCell = new PdfPCell(new Phrase(new Chunk("Manual Discrepancy Report", font18))); PdfPCell.Border = Rectangle.NO_BORDER; PdfTable.AddCell(PdfPCell); //DrawLine(writer, 25f, pdfDoc.Top - 30f, pdfDoc.PageSize.Width - 25f, pdfDoc.Top - 30f, new BaseColor(222,0,0)); //DrawLine(writer, 25f, pdfDoc.Top - 30f, pdfDoc.PageSize.Width - 25f, pdfDoc.Top - 30f, new BaseColor(0,0,0)); pdfDoc.Add(PdfTable); PdfTable = new PdfPTable(4); /* new PdfPTable(dt.Columns.Count); */ PdfTable.SpacingBefore = 20f; PdfTable.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY; PdfPCell cell_heading = new PdfPCell(new Phrase(new Chunk("Coupon Verification Report", font18))); cell_heading.BackgroundColor = BaseColor.LIGHT_GRAY; cell_heading.MinimumHeight = 16; cell_heading.Colspan = 4; cell_heading.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right PdfTable.AddCell(cell_heading); PdfTable.DefaultCell.BackgroundColor = BaseColor.WHITE; //for (int columns = 0; columns <= dt.Columns.Count - 1; columns++) //{ // PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[columns].ColumnName, font18))); // PdfTable.AddCell(PdfPCell); //} PdfTable.HorizontalAlignment = 1; int valid_amnt = 0; int excep_amnt = 0; for (int rows = 0; rows <= dt2.Rows.Count - 1; rows++) { valid_amnt = valid_amnt + int.Parse(dt2.Rows[rows][6].ToString()); excep_amnt = excep_amnt + int.Parse(dt2.Rows[rows][8].ToString()); } PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Batch Id", font18)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][0].ToString(), fonttext)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Sap Code", font18)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][3].ToString(), fonttext)))); //pdfDoc.Add(PdfTable); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Distributor Name", font18)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][2].ToString(), fonttext)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("State", font18)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][5].ToString(), fonttext)))); //pdfDoc.Add(PdfTable); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Batch Date", font18)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][1].ToString(), fonttext)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Valid Amount", font18)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(valid_amnt.ToString(), fonttext)))); //pdfDoc.Add(PdfTable); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Scan Date", font18)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(dt.Rows[0][4].ToString(), fonttext)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk("Exception Amount", font18)))); PdfTable.AddCell(new PdfPCell(new Phrase(new Chunk(excep_amnt.ToString(), fonttext)))); pdfDoc.Add(PdfTable); PdfTable = new PdfPTable(9); PdfTable.SpacingBefore = 20f; PdfTable.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY; PdfPCell cell_heading2 = new PdfPCell(new Phrase(new Chunk("Detailed Summary", font18))); cell_heading2.BackgroundColor = BaseColor.LIGHT_GRAY; cell_heading2.MinimumHeight = 15; cell_heading2.Colspan = 9; cell_heading2.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right PdfTable.AddCell(cell_heading2); //for (int columns = 0; columns <= dt2.Columns.Count - 1; columns++) string[] arr_det = new string[] { "Coupon Type", "Claimed Coupon Count", "Claimed Coupon Amount", "Coupon Receipt Count", "Valid Coupon Count", "Valid Coupon Amount", "Exceptions Coupon Count", "Exceptions Coupon Amount", "Short/Excess Coupon Count" }; for (int columns = 0; columns < arr_det.Length; columns++) { PdfPCell = new PdfPCell(new Phrase(new Chunk(arr_det[columns], font18))); PdfTable.AddCell(PdfPCell); } for (int rows = 0; rows <= dt2.Rows.Count - 1; rows++) { //for (int column = 0; column <9; column++) //{ PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][0].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][2].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][3].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][4].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][5].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][6].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); // int sums_exception = int.Parse(dt2.Rows[rows][8].ToString())+ int.Parse(dt2.Rows[rows][9].ToString())+ int.Parse(dt2.Rows[rows][10].ToString())+ int.Parse(dt2.Rows[rows][11].ToString()) + int.Parse(dt2.Rows[rows][12].ToString()) + int.Parse(dt2.Rows[rows][13].ToString()); PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][7].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][8].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); PdfPCell = new PdfPCell(new Phrase(new Chunk(dt2.Rows[rows][9].ToString(), fonttext))); PdfTable.AddCell(PdfPCell); //} } PdfPCell = new PdfPCell(new Phrase(new Chunk("Total", font18))); PdfTable.AddCell(PdfPCell); string sum_colmns = sum_datatable_column(dt2, 2).ToString(); PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18))); PdfTable.AddCell(PdfPCell); sum_colmns = sum_datatable_column(dt2, 3).ToString(); PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18))); PdfTable.AddCell(PdfPCell); sum_colmns = sum_datatable_column(dt2, 4).ToString(); PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18))); PdfTable.AddCell(PdfPCell); sum_colmns = sum_datatable_column(dt2, 5).ToString(); PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18))); PdfTable.AddCell(PdfPCell); sum_colmns = sum_datatable_column(dt2, 6).ToString(); PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18))); PdfTable.AddCell(PdfPCell); sum_colmns = sum_datatable_column(dt2, 7).ToString(); PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18))); PdfTable.AddCell(PdfPCell); sum_colmns = sum_datatable_column(dt2, 8).ToString(); PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18))); PdfTable.AddCell(PdfPCell); sum_colmns = sum_datatable_column(dt2, 9).ToString(); PdfPCell = new PdfPCell(new Phrase(new Chunk(sum_colmns, font18))); PdfTable.AddCell(PdfPCell); pdfDoc.Add(PdfTable); } pdfDoc.Close(); i tried to run change run user when user not logged on in action changes added path for arguments and optional checked about itextsharper changed configure for window server 2012 R2 please anyone help me on this , any more info required let me know
itextsharp Rowspan Table
I'm trying to create a rowscape table with itextsharp but it's impossible. I want this table: But the one it's generated is this one: I'm using Rowscpae = 2 for the first 2 cells (and trying to put rowscape only in one): table = new PdfPTable(new float[] { 40f, 10f, 50f }) { WidthPercentage = 100f }; table.TotalWidth = 555f; cell = new PdfPCell(new Phrase("¿Fuma o ha fumado alguna vez?", fuente_cabecera)); cell.Rowspan = 2; table.AddCell(cell); Chunk c1 = new Chunk(" SÍ ",fuente_cabecera); Chunk c2 = new Chunk(checkBox + "\n", f); Chunk c3 = new Chunk("NO ", fuente_cabecera); Chunk c4 = new Chunk(uncheckBox, f); Paragraph p1 = new Paragraph(); p1.Add(c1); p1.Add(c2); p1.Add(c3); p1.Add(c4); cell = new PdfPCell(p1); cell.VerticalAlignment = Element.ALIGN_MIDDLE; cell.Rowspan = 2; table.AddCell(cell); cell = new PdfPCell(new Phrase("Indique cantidad diaria", fuente_cabecera)); table.AddCell(cell); cell = new PdfPCell(new Phrase("sssss", fuente_cabecera)); table.AddCell(cell);
Why the border is not coming for pdf pages created by itextsharp?
This is the code I use to create a pdf file. This works good, and the pdf file is in more than one page. I do have the table in the pdf file, the table is starting from page 1 and goes to last page (i.e, page 3). So the border of the page is closing on the page 2. It would be good if I have the table border on page 2. Can any one please help me how can I have have border on all the pages? Thanks much in advance. public void Masterpdffile(DataTable dtpdfvalues, InvoiceSchema _InvoiceSchema, Companyschema _Companyschema) { string Filename = string.Empty; string FilePath = string.Empty; try { MemoryStream PDFData = new MemoryStream(); Document doc = new Document(PageSize.A4, 25, 25, 40, 40); string ApplicationPath = Application.StartupPath + "\\" + "Logo.png"; PdfWriter writer = PdfWriter.GetInstance(doc, PDFData); var titleFont = FontFactory.GetFont("Times New Roman", 16, iTextSharp.text.Font.BOLD); var titleFont1 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL); var boldTableFont = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL); var boldTableFont1 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL); var boldTableFont2 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL); var Total = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.BOLD); var boldTableFont3 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL); var bodyFont = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL); var bodyFont1 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL); var italicFont = FontFactory.GetFont("Times New Roman", 12, iTextSharp.text.Font.NORMAL); var Font =FontFactory.GetFont("Times New Roman", 12,BaseColor.WHITE); var border =(PdfPCell.BOTTOM_BORDER); iTextSharp.text.Rectangle pageSize = writer.PageSize; Filename = _InvoiceSchema.InvoiceNumber+"-"+"Masterpackingslip"+"-" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".pdf"; FilePath = Constant.Folderpath + "\\" + Filename; PdfWriter.GetInstance(doc, new System.IO.FileStream(FilePath, FileMode.Create)); doc.Open(); #region Image and Title float[] phWidth = new float[] { 50f, 30f, 20f }; PdfPTable Pheader = new PdfPTable(3); Pheader.WidthPercentage = 100f; Pheader.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; string Companyaddress = string.Empty; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address1)) Companyaddress += _Companyschema.Qbcompanyaddress.Address1 + Environment.NewLine; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address2)) Companyaddress += _Companyschema.Qbcompanyaddress.Address2 + Environment.NewLine; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address3)) Companyaddress += _Companyschema.Qbcompanyaddress.Address3 + Environment.NewLine; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address4)) Companyaddress += _Companyschema.Qbcompanyaddress.Address4 + Environment.NewLine; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address5)) Companyaddress += _Companyschema.Qbcompanyaddress.Address5 + Environment.NewLine; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.City)) Companyaddress += _Companyschema.Qbcompanyaddress.City + ","; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.State)) Companyaddress += _Companyschema.Qbcompanyaddress.State + " "; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.PostalCode)) Companyaddress += _Companyschema.Qbcompanyaddress.PostalCode + Environment.NewLine; if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Country)) Companyaddress += _Companyschema.Qbcompanyaddress.Country + Environment.NewLine; PdfPCell pcellsContect = new PdfPCell(new Phrase(_Companyschema.CompanyName+ Environment.NewLine + Environment.NewLine + Companyaddress, bodyFont1)); pcellsContect.PaddingLeft = 50; pcellsContect.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsContect.Border = iTextSharp.text.Rectangle.NO_BORDER; pcellsContect.UseAscender = true; Pheader.AddCell(pcellsContect); PdfPCell pspac = new PdfPCell(); pspac.Border = iTextSharp.text.Rectangle.NO_BORDER; pspac.VerticalAlignment = 1; PdfPCell pInvoicez = new PdfPCell(); pInvoicez.Border = iTextSharp.text.Rectangle.NO_BORDER; pInvoicez.VerticalAlignment = 1; PdfPCell pcellsforspace = new PdfPCell(new Phrase(" Master Packing Slip", titleFont)); pcellsforspace.HorizontalAlignment = Element.ALIGN_LEFT; pcellsforspace.VerticalAlignment = Element.ALIGN_BOTTOM; pcellsforspace.Border = iTextSharp.text.Rectangle.NO_BORDER; pcellsforspace.UseDescender = true; Paragraph p1 = new Paragraph("Packing Slip", titleFont); p1.Alignment = Element.ALIGN_RIGHT; p1.Font = boldTableFont; pInvoicez.AddElement(p1); Pheader.AddCell(pspac); Pheader.AddCell(pcellsforspace); doc.Add(Pheader); #endregion #region Top float[] outwodth = new float[] { 50f, 20f, 30f }; PdfPTable Outer = new PdfPTable(3); Outer.WidthPercentage = 100; Outer.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; Outer.SetWidths(outwodth); float[] wid = new float[] { 50f, 50f }; PdfPTable pdf = new PdfPTable(2); pdf.SetWidths(wid); pdf.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; pdf.HorizontalAlignment = Element.ALIGN_LEFT; pdf.WidthPercentage = 90.0f; PdfPCell pcel = new PdfPCell(); float[] widtSp = new float[] { 10f }; PdfPTable pdfSpace = new PdfPTable(1); pdfSpace.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; pdfSpace.HorizontalAlignment = Element.ALIGN_RIGHT; pdfSpace.WidthPercentage = 5.0f; pdfSpace.SetWidths(widtSp); float[] widt = new float[] { 10f, 10f }; PdfPTable ptable = new PdfPTable(2); ptable.HorizontalAlignment = Element.ALIGN_RIGHT; ptable.WidthPercentage = 5.0f; ptable.SetWidths(widt); PdfPCell pcellsDate = new PdfPCell(new Phrase("Date", boldTableFont1)); pcellsDate.HorizontalAlignment = Element.ALIGN_CENTER; pcellsDate.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsDate.UseAscender = true; pcellsDate.FixedHeight = 20f; ptable.AddCell(pcellsDate); PdfPCell pcellsInvoice = new PdfPCell(new Phrase("Invoice #", boldTableFont1)); pcellsInvoice.HorizontalAlignment = Element.ALIGN_CENTER; pcellsInvoice.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsInvoice.UseAscender = true; pcellsInvoice.FixedHeight = 20f; ptable.AddCell(pcellsInvoice); PdfPCell pcellsDateValue = new PdfPCell(new Phrase(_InvoiceSchema.InvoiceDate.ToShortDateString(), bodyFont)); pcellsDateValue.HorizontalAlignment = Element.ALIGN_CENTER; pcellsDateValue.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsDateValue.PaddingLeft=10; pcellsDateValue.UseAscender = true; pcellsDateValue.FixedHeight = 20f; ptable.AddCell(pcellsDateValue); PdfPCell pcellsInvValue = new PdfPCell(new Phrase(_InvoiceSchema.InvoiceNumber, bodyFont)); pcellsInvValue.HorizontalAlignment = Element.ALIGN_CENTER; pcellsInvValue.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsInvValue.UseAscender = true; pcellsInvValue.FixedHeight = 20f; ptable.AddCell(pcellsInvValue); Outer.AddCell(pdf); Outer.AddCell(pdfSpace); Outer.AddCell(ptable); Outer.SpacingAfter = 10f; doc.Add(Outer); #endregion #region Bill Mid table float[] widthBP = new float[] { 40f, 20f, 40f }; PdfPTable tableBP = new PdfPTable(3); tableBP.WidthPercentage = 100; tableBP.SetWidths(widthBP); tableBP.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; float[] widp = new float[] { 20f }; PdfPTable pdfpSp = new PdfPTable(1); pdfpSp.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; pdfpSp.HorizontalAlignment = Element.ALIGN_RIGHT; pdfpSp.WidthPercentage = 5.0f; pdfpSp.SetWidths(widp); string billaddress = string.Empty; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address1)) billaddress += _InvoiceSchema.BillAddress.Address1 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address2)) billaddress += _InvoiceSchema.BillAddress.Address2 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address3)) billaddress += _InvoiceSchema.BillAddress.Address3 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address4)) billaddress += _InvoiceSchema.BillAddress.Address4 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address5)) billaddress += _InvoiceSchema.BillAddress.Address5 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.City)) billaddress += _InvoiceSchema.BillAddress.City + ","; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.State)) billaddress += _InvoiceSchema.BillAddress.State + " "; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.PostalCode)) billaddress += _InvoiceSchema.BillAddress.PostalCode + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Country)) billaddress += _InvoiceSchema.BillAddress.Country + Environment.NewLine; float[] width = new float[] { 5f }; PdfPTable table = new PdfPTable(1); table.HorizontalAlignment = Element.ALIGN_LEFT; table.WidthPercentage = 5.0f; table.SetWidths(width); PdfPCell cell = new PdfPCell(); cell.FixedHeight = 15f; PdfPCell pcellsBillTO = new PdfPCell(new Phrase(" Bill To", boldTableFont1)); pcellsBillTO.Colspan = 2; pcellsBillTO.HorizontalAlignment = Element.ALIGN_LEFT; pcellsBillTO.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsBillTO.UseAscender = true; pcellsBillTO.FixedHeight = 22f; table.AddCell(pcellsBillTO); PdfPCell pcellsBillTOvalue = new PdfPCell(new Phrase(billaddress, bodyFont)); pcellsBillTOvalue.HorizontalAlignment = Element.ALIGN_LEFT; pcellsBillTOvalue.VerticalAlignment = Element.ALIGN_TOP; pcellsBillTOvalue.UseAscender = true; pcellsBillTOvalue.FixedHeight = 50f; //madhu table.AddCell(pcellsBillTOvalue); float[] customerwidth = new float[] { 10f, 10f }; PdfPTable ptablecustomer = new PdfPTable(2); ptablecustomer.HorizontalAlignment = Element.ALIGN_LEFT; ptablecustomer.DefaultCell.Border = iTextSharp.text.Rectangle.BOX; ptablecustomer.WidthPercentage = 15f; ptablecustomer.SetWidths(customerwidth); PdfPCell pcellscustomervalue = new PdfPCell(new Phrase("Customer S.O", bodyFont)); pcellscustomervalue.HorizontalAlignment = Element.ALIGN_CENTER; pcellscustomervalue.VerticalAlignment = Element.ALIGN_MIDDLE; pcellscustomervalue.UseAscender = true; pcellscustomervalue.FixedHeight = 20f; ptablecustomer.AddCell(pcellscustomervalue); PdfPCell pcellsship = new PdfPCell(new Phrase("Ship", bodyFont)); pcellsship.HorizontalAlignment = Element.ALIGN_CENTER; pcellsship.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsship.UseAscender = true; pcellsship.FixedHeight = 20f; ptablecustomer.AddCell(pcellsship); PdfPCell pcellcustomername = new PdfPCell(new Phrase(_InvoiceSchema.PONumber, bodyFont)); pcellcustomername.HorizontalAlignment = Element.ALIGN_CENTER; pcellcustomername.VerticalAlignment = Element.ALIGN_MIDDLE; pcellcustomername.UseAscender = true; pcellcustomername.FixedHeight = 20f; ptablecustomer.AddCell(pcellcustomername); PdfPCell pcellsonumber = new PdfPCell(new Phrase(_InvoiceSchema.InvoiceDate.ToShortDateString(), bodyFont)); pcellsonumber.HorizontalAlignment = Element.ALIGN_CENTER; pcellsonumber.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsonumber.UseAscender = true; pcellsonumber.FixedHeight = 20f; ptablecustomer.AddCell(pcellsonumber); tableBP.AddCell(table); tableBP.AddCell(pdfpSp); string ShipAddress = string.Empty; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address1)) ShipAddress += _InvoiceSchema.ShipAddress.Address1 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address2)) ShipAddress += _InvoiceSchema.ShipAddress.Address2 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address3)) ShipAddress += _InvoiceSchema.ShipAddress.Address3 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address4)) ShipAddress += _InvoiceSchema.ShipAddress.Address4 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address5)) ShipAddress += _InvoiceSchema.ShipAddress.Address5 + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.City)) ShipAddress += _InvoiceSchema.ShipAddress.City + ","; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.State)) ShipAddress += _InvoiceSchema.ShipAddress.State + " "; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.PostalCode)) ShipAddress += _InvoiceSchema.ShipAddress.PostalCode + Environment.NewLine; if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Country)) ShipAddress += _InvoiceSchema.ShipAddress.Country + Environment.NewLine; float[] widthP = new float[] { 5f }; PdfPTable tableP = new PdfPTable(1); tableP.WidthPercentage = 30f; tableP.HorizontalAlignment = Element.ALIGN_RIGHT; tableP.SetWidths(widthP); PdfPCell pcellsProperty = new PdfPCell(new Phrase(" Ship To", boldTableFont1)); pcellsProperty.HorizontalAlignment = Element.ALIGN_LEFT; pcellsProperty.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsProperty.UseAscender = true; pcellsProperty.FixedHeight = 22f; tableP.AddCell(pcellsProperty); PdfPCell pcellsPropertyvalue = new PdfPCell(new Phrase(ShipAddress, bodyFont)); pcellsPropertyvalue.HorizontalAlignment = Element.ALIGN_LEFT; pcellsPropertyvalue.VerticalAlignment = Element.ALIGN_TOP; pcellsPropertyvalue.UseAscender = true; pcellsPropertyvalue.FixedHeight = 50f; tableP.AddCell(pcellsPropertyvalue); tableBP.AddCell(tableP); float[] widpf = new float[] { 20f }; PdfPTable pdgpSp = new PdfPTable(1); pdgpSp.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; pdgpSp.HorizontalAlignment = Element.ALIGN_RIGHT; pdgpSp.WidthPercentage = 5.0f; pdgpSp.SetWidths(widpf); float[] wigdp = new float[] { 20f }; PdfPTable pdfpghSp = new PdfPTable(1); pdfpghSp.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; pdfpghSp.HorizontalAlignment = Element.ALIGN_RIGHT; pdfpghSp.WidthPercentage = 5.0f; pdfpghSp.SetWidths(wigdp); tableBP.AddCell(ptablecustomer); tableBP.AddCell(pdgpSp); tableBP.AddCell(pdfpghSp); tableBP.SpacingAfter = 12f; doc.Add(tableBP); #endregion float[] widthM = new float[] { 2f, 2.3F, 8.3f }; PdfPTable tableM = new PdfPTable(3); tableM.SetWidths(widthM); //tableM.DefaultCell.FixedHeight = 20f; tableM.DefaultCell.Border = 0; tableM.WidthPercentage = 100.0f; //tableM.h PdfPCell clHeader = new PdfPCell(); clHeader.FixedHeight = 20f; PdfPCell pcellsitemname = new PdfPCell(new Phrase("Qty", boldTableFont1)); pcellsitemname.HorizontalAlignment = Element.ALIGN_CENTER; pcellsitemname.VerticalAlignment = Element.ALIGN_RIGHT; pcellsitemname.UseAscender = true; pcellsitemname.FixedHeight = 20f; tableM.AddCell(pcellsitemname); PdfPCell pcellsDescription = new PdfPCell(new Phrase("Item Code", boldTableFont1)); pcellsDescription.HorizontalAlignment = Element.ALIGN_CENTER; pcellsDescription.VerticalAlignment = Element.ALIGN_MIDDLE; // pcellsProperty.Border = iTextSharp.text.Rectangle.NO_BORDER; pcellsDescription.UseAscender = true; pcellsDescription.FixedHeight = 20f; tableM.AddCell(pcellsDescription); PdfPCell pcellsQty = new PdfPCell(new Phrase("Description", boldTableFont1)); pcellsQty.HorizontalAlignment = Element.ALIGN_CENTER; pcellsQty.VerticalAlignment = Element.ALIGN_MIDDLE; pcellsQty.UseAscender = true; pcellsQty.FixedHeight = 20f; tableM.AddCell(pcellsQty); PdfPCell amtCell = new PdfPCell(); int i = 0; foreach (DataRow dr in dtpdfvalues.Rows) { if (!string.IsNullOrEmpty(Convert.ToString(dr["Parent"]))) { i++; } } int k = 0; foreach (DataRow dr in dtpdfvalues.Rows) { int count = Convert.ToInt16(dtpdfvalues.Rows.Count); if (!string.IsNullOrEmpty(Convert.ToString(dr["Parent"]))) { k++; PdfPCell descCell = new PdfPCell(); PdfPCell qtyCell = new PdfPCell(); PdfPCell rateCell = new PdfPCell(); descCell.FixedHeight = 78; qtyCell.FixedHeight = 78; rateCell.FixedHeight = 78; amtCell.FixedHeight = 78; if (i != k) { descCell.MinimumHeight = 335 / i; qtyCell.MinimumHeight = 335 / i; rateCell.MinimumHeight = 335 / i; amtCell.MinimumHeight = 335 / i; descCell.FixedHeight = 78; qtyCell.FixedHeight = 78; rateCell.FixedHeight = 78; amtCell.FixedHeight = 78; } else { descCell.MinimumHeight = 335; qtyCell.MinimumHeight = 335; rateCell.MinimumHeight = 335; amtCell.MinimumHeight = 335; descCell.FixedHeight = 78; qtyCell.FixedHeight = 78; rateCell.FixedHeight = 78; amtCell.FixedHeight = 78; } rateCell.Border = 0; amtCell.Border = 0; rateCell.Border = 0; descCell.Border = 0; qtyCell.Border = 0; rateCell.BorderWidthLeft = 0.1f; amtCell.BorderWidthLeft = 0.1f; descCell.BorderWidthLeft = 0.1f; qtyCell.BorderWidthLeft = 0.1f; qtyCell.BorderWidthRight = 0.1f;
Adding an image next to a table in iTextSharp
I want to add an image next to a table without a newline. I have following code: iTextSharp.text.Font font = new iTextSharp.text.Font(FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL)); iTextSharp.text.Font fontbold = new iTextSharp.text.Font(FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD)); PdfPTable tkunde = new PdfPTable(1); tkunde.WidthPercentage = 30; tkunde.HorizontalAlignment = Element.ALIGN_LEFT; PdfPCell labkunNavn = new PdfPCell(new Phrase(navnn, fontbold)); labkunNavn.PaddingTop = 2f; labkunNavn.HorizontalAlignment = Element.ALIGN_LEFT; labkunNavn.Border = 0; tkunde.AddCell(labkunNavn); PdfPCell labkunAdresse = new PdfPCell(new Phrase(adresse, font)); labkunAdresse.PaddingTop = 2f; labkunAdresse.HorizontalAlignment = Element.ALIGN_LEFT; labkunAdresse.Border = 0; tkunde.AddCell(labkunAdresse); PdfPCell labkunPost = new PdfPCell(new Phrase(postnr + " " + område, font)); labkunPost.PaddingTop = 2f; labkunPost.HorizontalAlignment = Element.ALIGN_LEFT; labkunPost.Border = 0; tkunde.AddCell(labkunPost); doc.Add(tkunde); iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(#"C:\Users\Osman\Desktop\download.jpg"); png.WidthPercentage = 10; png.ScalePercent(50F); png.Alignment = Element.ALIGN_RIGHT; doc.Add(png); But it adds the image below the table as shown in the figure on following link: http://postimg.org/image/z9rr0dk5p/4289a76a/ I want the layout to be like this instead: http://postimg.org/image/3yae8w6q3/42ddcd80/ How do I achieve this ?. Thanks in advance
Generating PDF from a DataGridView in Winforms
I'm attempting to create a PDF from a DataGridView populated from a database. I have just started trying to learn how to use iTextSharp to accomplish this. The result of my code is a PDF that will not open. I get an error saying "File cannot be opened" Here is my code to generate the PDF void SendToPDF(string heading, string filename) { try { Document doc = new Document(PageSize.A4.Rotate(), 30, 30, 20, 20); string myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (!Directory.Exists(myDocs + #"\Production Reports")) Directory.CreateDirectory(myDocs + #"\Production Reports"); PdfWriter.GetInstance(doc, new FileStream(myDocs + #"\Production Reports\" + filename + ".pdf", FileMode.Append, FileAccess.Write)); iTextSharp.text.Font titleFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 14.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK); iTextSharp.text.Font tableFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); iTextSharp.text.Font headerfont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK); PdfPTable table = new PdfPTable(GridView.Columns.Count); //table.TotalWidth = GridView.Width; //There are ALWAYS 10 columns float[] widths = new float[] { GridView.Columns[0].Width, GridView.Columns[1].Width, GridView.Columns[2].Width, GridView.Columns[3].Width, GridView.Columns[4].Width, GridView.Columns[5].Width, GridView.Columns[6].Width, GridView.Columns[7].Width, GridView.Columns[8].Width, GridView.Columns[9].Width }; table.SetWidths(widths); table.HorizontalAlignment = 1; // 0 - left, 1 - center, 2 - right; table.SpacingBefore = 2.0F; PdfPCell cell = null; doc.Open(); Phrase p = new Phrase(new Chunk(heading, titleFont)); doc.Add(p); foreach (DataGridViewColumn c in GridView.Columns) { cell = new PdfPCell(new Phrase(new Chunk(c.HeaderText, headerfont))); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; cell.VerticalAlignment = PdfPCell.ALIGN_CENTER; table.AddCell(cell); } if (GridView.Rows.Count > 0) { for (int i = 0; i < GridView.Rows.Count - 1; i++) { for (int j = 0; j < GridView.Columns.Count - 1; j++) { cell = new PdfPCell(new Phrase(GridView.Rows[i].Cells[j].Value.ToString(), tableFont)); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; cell.VerticalAlignment = PdfPCell.ALIGN_CENTER; table.AddCell(cell); } } } doc.Add(table); doc.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, "Error Generating PDF", MessageBoxButtons.OK, MessageBoxIcon.Error); } } I'm guessing my problem has to do with setting column widths, but I'm not sure. One time, and only one time..I saw an error when I tried to open the PDF that said "illegal floating point division by 0" or something along those lines. Any help is greatly appreciated.
It may sound obvious, but your program isn't running and locking the pdf file to its process thus preventing adobe pdf reader from reading it is it?
private void jbtnPdf_Click(object sender, EventArgs e) { try { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "All Files | *.* "; if (saveFileDialog.ShowDialog() == DialogResult.OK) { string path = saveFileDialog.FileName; Document pdfdoc = new Document(PageSize.A4); // Setting the page size for the PDF PdfWriter writer = PdfWriter.GetInstance(pdfdoc, new FileStream(path + ".pdf", FileMode.Create)); //Using the PDF Writer class to generate the PDF writer.PageEvent = new PDFFooter(); // Opening the PDF to write the data from the textbox PdfPTable table = new PdfPTable(jdgvChild.Columns.Count); //table.TotalWidth = GridView.Width; float[] widths = new float[] { jdgvChild.Columns[0].Width, jdgvChild.Columns[1].Width, jdgvChild.Columns[2].Width, jdgvChild.Columns[3].Width, jdgvChild.Columns[4].Width, jdgvChild.Columns[5].Width, jdgvChild.Columns[6].Width, jdgvChild.Columns[7].Width}; table.SetWidths(widths); table.HorizontalAlignment = 1; // 0 - left, 1 - center, 2 - right; table.SpacingBefore = 2.0F; PdfPCell cell = null; pdfdoc.Open(); //doc.Open(); // Phrase p = new Phrase(new Chunk(heading, titleFont)); // doc.Add(p); foreach (GridViewDataColumn c in jdgvChild.Columns) { cell = new PdfPCell(new Phrase(new Chunk(c.HeaderText))); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; cell.VerticalAlignment = PdfPCell.ALIGN_CENTER; table.AddCell(cell); } if (jdgvChild.Rows.Count > 0) { for (int i = 0; i < jdgvChild.Rows.Count; i++) { PdfPCell[] objcell = new PdfPCell[jdgvChild.Columns.Count]; for (int j = 0; j < jdgvChild.Columns.Count-1; j++) { cell = new PdfPCell(new Phrase(jdgvChild.Rows[i].Cells[j].Value.ToString())); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; cell.VerticalAlignment = PdfPCell.ALIGN_CENTER; // table.AddCell(cell); //lstCells.Add(cell); objcell[j] = cell; } PdfPRow newrow = new PdfPRow(objcell); table.Rows.Add(newrow); } } pdfdoc.Add(table); MessageBox.Show("Pdf Generation Successfully."); pdfdoc.Close(); } } catch (Exception ex) { MessageBox.Show("Error in pdf Generation."); } }