0
私の体セクションにheader
とfooter
セクション、 を含むpdfを作成しています。私のdatagridview
データを含むテーブルが必要です。 pdfは3ページまたは4ページ長くなります。私はそれを作ったが、問題は、私のテーブルがページに2回表示されていることです。あなたは以下のようにキャプチャで見ることができます。 enter image description hereテーブルをitextsharpで作成するpdf
私のコードは私に皆を助けるためにしてみてください
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("আইটেম সিলেক্ট করুন");
}
}
です! ありがとう:-)