I need to create 2 column mailing labels in itextsharp PDF. I already have created the logic for 1 column labels and it is working fine. As, I am unable to understand how to do it for labels with 2 column. Pl consider the below scenario,
I need to do the logic like below format
Code which I have
public Stream CreatePDF(Label _label)
{
FontFactory.RegisterDirectories();
Rectangle pageSize;
switch (_label.PageSize)
{
case Enums.PageSize.A4:
pageSize = iTextSharp.text.PageSize.A4;
break;
default:
pageSize = iTextSharp.text.PageSize.A4;
break;
}
var doc = new Document(pageSize,
_label.PageMarginLeft,
_label.PageMarginRight,
_label.PageMarginTop,
_label.PageMarginBottom);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(doc, output);
writer.CloseStream = false;
doc.Open();
var numOfCols = _label.LabelsPerRow + (_label.LabelsPerRow - 1);
var tbl = new PdfPTable(numOfCols);
var colWidths = new List<float>();
for (int i = 1; i <= numOfCols; i++)
{
if (i % 2 > 0)
{
colWidths.Add(_label.Width);
}
else
{
colWidths.Add(_label.HorizontalGapWidth);
}
}
var w = iTextSharp.text.PageSize.A4.Width - (doc.LeftMargin + doc.RightMargin);
var h = iTextSharp.text.PageSize.A4.Height - (doc.TopMargin + doc.BottomMargin);
var size = new iTextSharp.text.Rectangle(w, h);
tbl.SetWidthPercentage(colWidths.ToArray(), size);
var val = System.IO.File.ReadLines("C:\\Users\\lenovo\\Desktop\\test stock\\testing3.txt").ToArray();
int cnt = 0;
for (int iRow = 0; iRow < ((val.Count() / _label.LabelsPerRow) + 1); iRow++)
{
var rowCells = new List<PdfPCell>();
for (int iCol = 1; iCol <= numOfCols; iCol++)
{
if (val.Count() > cnt)
{
if (iCol % 2 > 0)
{
var cellContent = new Phrase();
if (val[cnt] != "")
{
var fontHeader = FontFactory.GetFont("Verdana", BaseFont.CP1250, true, 12, 0);
cellContent.Add(new Chunk("Default Header\n\n", fontHeader));
Code39BarcodeDraw barcode39 = BarcodeDrawFactory.Code39WithoutChecksum;
System.Drawing.Image img = barcode39.Draw(val[cnt], 25);
var pdfImg = iTextSharp.text.Image.GetInstance(ReadImage(img));
var width = pdfImg.PlainWidth;
if (width > colWidths.ToArray()[0])
pdfImg.ScaleAbsoluteWidth(colWidths.ToArray()[0] - 5);
cellContent.Add(new Chunk(pdfImg, 0, 0, true));
var font = FontFactory.GetFont("Verdana", BaseFont.CP1250, true, 12, 0);
cellContent.Add(new Chunk("\n\n" + val[cnt], font));
}
cnt += 1;
var cell = new PdfPCell(cellContent);
cell.FixedHeight = _label.Height;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Border = IncludeLabelBorders ? Rectangle.BOX : Rectangle.NO_BORDER;
rowCells.Add(cell);
}
else
{
var gapCell = new PdfPCell();
gapCell.FixedHeight = _label.Height;
gapCell.Border = Rectangle.NO_BORDER;
rowCells.Add(gapCell);
}
}
else
{
var gapCell = new PdfPCell();
gapCell.FixedHeight = _label.Height;
gapCell.Border = Rectangle.NO_BORDER;
rowCells.Add(gapCell);
}
}
tbl.Rows.Add(new PdfPRow(rowCells.ToArray()));
if ((iRow + 1) < _label.LabelRowsPerPage && _label.VerticalGapHeight > 0)
{
tbl.Rows.Add(CreateGapRow(numOfCols));
}
}
doc.Add(tbl);
doc.Close();
output.Position = 0;
return output;
}