【发布时间】:2015-11-12 03:44:31
【问题描述】:
我需要在 itextsharp PDF 中创建 2 列邮件标签。我已经为 1 列标签创建了逻辑,并且工作正常。因为,我无法理解如何为带有 2 列的标签执行此操作。请考虑以下情况,
我需要做如下格式的逻辑
我有的代码
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;
}
【问题讨论】:
-
你从哪里得到的代码?这很尴尬。你为什么使用
PdfPRow。没有必要使用那个类,是吗? -
我从这里得到了代码github.com/wheelibin/SharpPDFLabel#readme。我从中更改了一些代码。
-
那不是官方文档,不是吗?我只能重复我之前说过的话:你的代码中没有理由需要
PdfPRow。 -
本周我重写了 SharpPDFLabel 代码,因为我需要它更加灵活(并且可以工作)。 github.com/finalcut/SharpPDFLabel 如果您愿意,我添加了指定每个标签内容的功能(或者继续创建一张相同的标签)。
标签: c# pdf itextsharp