Adding an image next to a table in iTextSharp - c#

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

Related

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

iTextSharp NET Remove spacing between cells

I'm trying to add three cells in one row, which have colored background with the same color. The problem is that I see space (or vertical border) between these cells, but I don't need this space.
As you can see on the image, there is some space. Maybe this is the wrong way to do what I need. I need this green text in the center of page and white text on the right side at the same row and with the same background color.
This is my code:
PdfPCell emptyCell = new PdfPCell();
emptyCell.BackgroundColor = BaseColor.DARK_GRAY;
emptyCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
emptyCell.VerticalAlignment = Element.ALIGN_CENTER;
emptyCell.MinimumHeight = 30f;
emptyCell.Colspan = 3;
emptyCell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(emptyCell);
iTextSharp.text.Font clubFont = new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL, new BaseColor(0.541f, 0.765f, 0f));
PdfPCell cell = new PdfPCell();
Paragraph clubName = new Paragraph(clubString, clubFont) {Alignment = Element.ALIGN_CENTER};
cell.AddElement(clubName);
cell.BackgroundColor = BaseColor.DARK_GRAY;
cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
cell.VerticalAlignment = Element.ALIGN_CENTER;
cell.MinimumHeight = 30f;
cell.Colspan = _currentTable.NumberOfColumns - 6;
cell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(cell);
iTextSharp.text.Font dispFont = new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL, BaseColor.WHITE);
PdfPCell dispCell = new PdfPCell();
Paragraph dispersion = new Paragraph(dispersionString, dispFont);
dispersion.Alignment = Element.ALIGN_CENTER;
dispCell.AddElement(dispersion);
dispCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
dispCell.VerticalAlignment = Element.ALIGN_CENTER;
dispCell.MinimumHeight = 30f;
dispCell.Colspan = 3;
dispCell.BackgroundColor = BaseColor.DARK_GRAY;
dispCell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(dispCell);

making table in itextsharp pdf

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.

Increase Height of last cell

I created a table using the iTextSharp.text.pdf library.
Table I created:
Code I used:
var signTable = new PdfPTable(2);
signTable.WidthPercentage = 65f;
signTable.HorizontalAlignment = Element.ALIGN_RIGHT;
signTable.SetWidths(new[] { 25, 40 });
//SignName
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.SignName", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
signTable.AddCell(cell);
//creates empty cell
cell = new PdfPCell();
signTable.AddCell(cell);
//SignDate
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.SignDate", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
signTable.AddCell(cell);
//creates empty cell
cell = new PdfPCell();
signTable.AddCell(cell);
//Signature
cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.Signature", lang.Id), font));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
signTable.AddCell(cell);
//creates empty cell
cell = new PdfPCell();
signTable.AddCell(cell);
//add table to pdf document.
doc.Add(signTable);
Question
How can I increase the height of the last added empty cell?(cell in right bottom corner)
I tried using following code, but it didn't change anything:
signTable.Rows[2].SetExtraHeight(5, 80f);
You can use
cell.MinimumHeight = 80f;
or
cell.FixedHeight = 80f;

Code isn't drawing a horizontal line in my PDF

I'm trying to add a horizontal line on top to divide the header text from the actual values in my pdf file:
Here's my code:
public class StudentList
{
public void PrintStudentList(int gradeParaleloID)
{
StudentRepository repo = new StudentRepository();
var students = repo.FindAllStudents()
.Where(s => s.IDGradeParalelo == gradeParaleloID);
try
{
Document document = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\Alumnos.pdf", FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
cb.SetLineWidth(2.0f); // Make a bit thicker than 1.0 default
cb.SetGrayStroke(0.95f); // 1 = black, 0 = white
cb.MoveTo(20, 30);
cb.LineTo(400, 30);
cb.Stroke();
PdfPTable table = new PdfPTable(3);
float[] widths = new float[] { 0.6f, 0.75f, 2f };
table.SetWidths(widths);
PdfPCell numeroCell = new PdfPCell(new Phrase("Nro."));
numeroCell.Border = 0;
numeroCell.HorizontalAlignment = 0;
table.AddCell(numeroCell);
PdfPCell codigoCell = new PdfPCell(new Phrase("RUDE"));
codigoCell.Border = 0;
codigoCell.HorizontalAlignment = 0;
table.AddCell(codigoCell);
PdfPCell nombreCell = new PdfPCell(new Phrase("Apellidos y Nombres"));
nombreCell.Border = 0;
nombreCell.HorizontalAlignment = 0;
table.AddCell(nombreCell);
int c = 1;
foreach (var student in students)
{
PdfPCell cell = new PdfPCell(new Phrase(c.ToString()));
cell.Border = 0;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
cell = new PdfPCell(new Phrase(student.Rude.ToString()));
cell.Border = 0;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
cell = new PdfPCell(new Phrase(student.LastNameFather + " " + student.LastNameMother + " " + student.Name));
cell.Border = 0;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
c++;
}
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
document.Add(table);
document.Close();
}
catch (DocumentException de)
{
Debug.WriteLine(de.Message);
}
catch (IOException ioe)
{
Debug.WriteLine(ioe.Message);
}
}
}
I don't understand why the cb.Stroke() isn't working. Any suggestions?
Drawing with iTextSharp's PdfContentByte class can be a little confusing. The height is actually relative to the bottom, not the top. So the top of the page is not 0f, but instead is actually document.Top, which on your page size of PageSize.LETTER is 726f. So if you want to draw your line 30 units from the top, try:
cb.MoveTo(20, document.Top - 30f);
cb.LineTo(400, document.Top - 30f);
Just curious -- why do it the hard way instead of using table borders? (I noticed you set your borders to 0) Trying to space out lines by hand is the biggest pain. If you ever move content in your PDF around, you'll have to make sure your measurements still work.
Try this:
numeroCell.Border = 0;
numeroCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
numeroCell.BorderWidthBottom = 1f;
codigoCell.Border = 0;
codigoCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
codigoCell.BorderWidthBottom = 1f;
nombreCell.Border = 0;
nombreCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black);
nombreCell.BorderWidthBottom = 1f;
That should give you a nice solid black line under your header row, no measurements needed:

Categories