I want to create a pdf having 2 cells in each row. I have used the following
private void printMass()
{
ClsOrder _clsOrder = new ClsOrder();
ClsControl _ClsControl = new ClsControl();
ViewState["OrderFolder"] = _ClsControl.fetchByID("OrderPath").Rows[0]["CtrlText"].ToString();
var orderlst = _clsOrder.fetchOrdertoPrint();
if (orderlst.Rows.Count > 0)
{
Document doc = new Document(iTextSharp.text.PageSize.A4, 15, 15, 50, 25);
//if (!Directory.Exists(Convert.ToString(ViewState["OrderFolder"]) + "\\" + orderNumber)) Directory.CreateDirectory(Convert.ToString(ViewState["OrderFolder"]) + "\\label_" + orderNumber);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(Convert.ToString(ViewState["OrderFolder"]) + "\\label_lst.pdf", FileMode.Create));
doc.Open();//Open Document to write
PdfPTable table = new PdfPTable(2);
//actual width of table in points
table.TotalWidth = 650;
table.WidthPercentage = 100;
//table.TotalHeight = 4000;
//fix the absolute width of the table
//table.LockedWidth = true;
int[] intTblWidth = { 325, 325 };
table.SetWidths(intTblWidth);
//relative col widths in proportions - 1/3 and 2/3
//float[] widths = new float[] { 1f, 2f };
//table.SetWidths(widths);
table.HorizontalAlignment = 0;
//leave a gap before and after the table
//table.SpacingBefore = 20f;
//table.SpacingAfter = 30f;
for (int k = 1; k <= orderlst.Rows.Count; k++)
{
var orderNumber = Convert.ToString(orderlst.Rows[k - 1]["OrderNumber"]);
var order = _clsOrder.fetchOrderByOrderNumber(Convert.ToString(orderlst.Rows[k - 1]["OrderNumber"]));
if (k % 2 == 0)
{
}
PdfPCell cell = new PdfPCell();
//to remove border of the Cell
cell.UseVariableBorders = true;
cell.BorderColorLeft = BaseColor.BLACK;
cell.BorderColorRight = BaseColor.BLACK;
cell.BorderColorTop = BaseColor.BLACK;
cell.BorderColorBottom = BaseColor.BLACK;
//cell.Width = 800;
cell.MinimumHeight = 150;
//cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
if (k % 2 == 0)
{
cell.HorizontalAlignment = 2;
cell.PaddingRight = 15f;//0=Left, 1=Centre, 2=Right
}
else
{
cell.HorizontalAlignment = 0;
cell.PaddingRight = 15f; //0=Left, 1=Centre, 2=Right
}
CreatePDF(orderNumber, order, cell, k.ToString());
table.AddCell(cell);
}
doc.Add(table);
doc.Close(); //Close document
Response.AddHeader("Content-Disposition", "attachment;filename=\"label_lst.pdf\"");
Response.TransmitFile(Convert.ToString(ViewState["OrderFolder"]) + "\\label_lst.pdf");
Response.End();
}
}
private void CreatePDF(string orderNumber, DataTable order, PdfPCell doc, string pos)
{
ClsOrder _clsOrder = new ClsOrder();
ClsCounty _clsCounty = new ClsCounty();
var position = 0;
iTextSharp.text.Font arial = FontFactory.GetFont("Arial", 14, Font.BOLD);
Phrase pharse = new Phrase("*" + orderNumber + "*", arial);
iTextSharp.text.Font small = FontFactory.GetFont("Arial", 9);
Phrase chunk = new Phrase(orderNumber, small);
var cname = _clsCounty.fetchCountyByID(Convert.ToInt32(order.Rows[0]["OrdCounty"]));
Paragraph ordCounty = new Paragraph(Convert.ToString(cname.Rows[0]["Name"]).ToUpper());
var ordDate = DateTime.Parse(order.Rows[0]["OrdDate"].ToString());
Paragraph ordOrdDate = new Paragraph(String.Format("{0:MM/dd/yy}", ordDate));
PdfPTable tab = new PdfPTable(2);
tab.WidthPercentage = 100;
//t.TotalWidth = 400;
int[] intTblWidth = { 220, 105 };
tab.SetWidths(intTblWidth);
PdfPCell cell2 = new PdfPCell(new Paragraph(ordCounty));
cell2.UseVariableBorders = true;
cell2.BorderColorLeft = BaseColor.WHITE;
cell2.BorderColorRight = BaseColor.WHITE;
cell2.BorderColorTop = BaseColor.WHITE;
cell2.BorderColorBottom = BaseColor.WHITE;
cell2.HorizontalAlignment = Element.ALIGN_LEFT;
cell2.PaddingLeft = 15f;
PdfPCell cell3 = new PdfPCell(new Paragraph(ordOrdDate));
cell3.UseVariableBorders = true;
cell3.BorderColorLeft = BaseColor.WHITE;
cell3.BorderColorRight = BaseColor.WHITE;
cell3.BorderColorTop = BaseColor.WHITE;
cell3.BorderColorBottom = BaseColor.WHITE;
cell3.HorizontalAlignment = Element.ALIGN_RIGHT;
cell3.PaddingLeft = 15f;
tab.AddCell(cell2);
tab.AddCell(cell3);
Paragraph OrdTitleCo = new Paragraph(Convert.ToString(order.Rows[0]["OrdTitleCo"]).ToUpper());
Paragraph OrdName = new Paragraph(Convert.ToString(order.Rows[0]["OrdName"]).ToUpper());
Paragraph OrdSrchType = new Paragraph(Convert.ToString(order.Rows[0]["OrdSrchType"]).ToUpper());
Paragraph OrdTitleCoOrderNumber = new Paragraph(Convert.ToString(order.Rows[0]["OrdTitleCoOrderNumber"]).ToUpper());
Paragraph OrdAddress = new Paragraph(Convert.ToString(order.Rows[0]["OrdAddress"]).ToUpper());
Paragraph OrdCity = new Paragraph(Convert.ToString(order.Rows[0]["OrdCity"]).ToUpper() + ", " + Convert.ToString(order.Rows[0]["OrdState"]).ToUpper() + " " + Convert.ToString(order.Rows[0]["OrdZipCode"]).ToUpper());
PdfPTable tabDetails = new PdfPTable(1);
tabDetails.WidthPercentage = 100;
cell3 = new PdfPCell(OrdTitleCo);
cell3.UseVariableBorders = true;
cell3.BorderColorLeft = BaseColor.WHITE;
cell3.BorderColorRight = BaseColor.WHITE;
cell3.BorderColorTop = BaseColor.WHITE;
cell3.BorderColorBottom = BaseColor.WHITE;
cell3.HorizontalAlignment = position;
cell3.PaddingLeft = 15f;
tabDetails.AddCell(cell3);
cell3 = new PdfPCell(OrdName);
cell3.UseVariableBorders = true;
cell3.BorderColorLeft = BaseColor.WHITE;
cell3.BorderColorRight = BaseColor.WHITE;
cell3.BorderColorTop = BaseColor.WHITE;
cell3.BorderColorBottom = BaseColor.WHITE;
cell3.HorizontalAlignment = position;
cell3.PaddingLeft = 15f;
tabDetails.AddCell(cell3);
cell3 = new PdfPCell(OrdSrchType);
cell3.UseVariableBorders = true;
cell3.BorderColorLeft = BaseColor.WHITE;
cell3.BorderColorRight = BaseColor.WHITE;
cell3.BorderColorTop = BaseColor.WHITE;
cell3.BorderColorBottom = BaseColor.WHITE;
cell3.HorizontalAlignment = position;
cell3.PaddingLeft = 15f;
tabDetails.AddCell(cell3);
cell3 = new PdfPCell(OrdTitleCoOrderNumber);
cell3.UseVariableBorders = true;
cell3.BorderColorLeft = BaseColor.WHITE;
cell3.BorderColorRight = BaseColor.WHITE;
cell3.BorderColorTop = BaseColor.WHITE;
cell3.BorderColorBottom = BaseColor.WHITE;
cell3.HorizontalAlignment = position;
cell3.PaddingLeft = 15f;
tabDetails.AddCell(cell3);
cell3 = new PdfPCell(OrdAddress);
cell3.UseVariableBorders = true;
cell3.BorderColorLeft = BaseColor.WHITE;
cell3.BorderColorRight = BaseColor.WHITE;
cell3.BorderColorTop = BaseColor.WHITE;
cell3.BorderColorBottom = BaseColor.WHITE;
cell3.HorizontalAlignment = position;
cell3.PaddingLeft = 15f;
tabDetails.AddCell(cell3);
cell3 = new PdfPCell(OrdCity);
cell3.UseVariableBorders = true;
cell3.BorderColorLeft = BaseColor.WHITE;
cell3.BorderColorRight = BaseColor.WHITE;
cell3.BorderColorTop = BaseColor.WHITE;
cell3.BorderColorBottom = BaseColor.WHITE;
cell3.HorizontalAlignment = position;
cell3.PaddingLeft = 15f;
tabDetails.AddCell(cell3);
PdfPTable t = new PdfPTable(2);
t.WidthPercentage = 100;
t.SetWidths(intTblWidth);
PdfPCell cell123 = new PdfPCell(new Phrase(pharse));
cell123.UseVariableBorders = true;
cell123.BorderColorLeft = BaseColor.WHITE;
cell123.BorderColorRight = BaseColor.WHITE;
cell123.BorderColorTop = BaseColor.WHITE;
cell123.BorderColorBottom = BaseColor.WHITE;
cell123.HorizontalAlignment = Element.ALIGN_LEFT;
cell123.PaddingLeft = 15f;
PdfPCell cell1 = new PdfPCell(new Phrase(chunk));
cell1.UseVariableBorders = true;
cell1.BorderColorLeft = BaseColor.WHITE;
cell1.BorderColorRight = BaseColor.WHITE;
cell1.BorderColorTop = BaseColor.WHITE;
cell1.BorderColorBottom = BaseColor.WHITE;
cell1.HorizontalAlignment = Element.ALIGN_RIGHT;
cell1.VerticalAlignment = Element.ALIGN_MIDDLE;
cell1.PaddingLeft = 15f;
t.AddCell(cell123);
t.AddCell(cell1);
doc.AddElement(t);
doc.AddElement(tab);
doc.AddElement(tabDetails);
}
The problem is the first to cells are created properly but from 3 item it is not showing. how do I change the row when 2 cells are made