我使用以下代码填充我的PdfPTable:
public PdfPTable GetTable(List<hsp_Narudzbe_IzdavanjeFakture4_Result> proizvodi)
{
PdfPTable table = new PdfPTable(5);
table.WidthPercentage = 100F;
table.DefaultCell.UseAscender = true;
table.DefaultCell.UseDescender = true;
Font f = new Font(Font.FontFamily.HELVETICA, 13);
f.Color = BaseColor.WHITE;
PdfPCell cell = new PdfPCell(new Phrase("Stavke narudžbe: ", f));
cell.BackgroundColor = BaseColor.BLACK;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Colspan = 5;
table.AddCell(cell);
for (int i = 0; i < 2; i++)
{
table.AddCell("Redni broj");
table.AddCell("Proizvod");
table.AddCell("Cijena");
table.AddCell("Šifra proizvoda");
table.AddCell("Kolicina");
}
table.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
table.HeaderRows = 3;
table.FooterRows = 1;
int broj = 0;
table.DeleteLastRow();
foreach (hsp_Narudzbe_IzdavanjeFakture4_Result p in proizvodi)
{
broj++;
table.AddCell(broj.ToString());
table.AddCell(p.Naziv);
table.AddCell(p.Cijena);
table.AddCell(p.Sifra);
table.AddCell(p.Kolicina.ToString());
}
float[] widths = { 15.00F, 15.00F,15.00F,15.00F};
table.GetRow(1).SetWidths(widths);
return table;
}
代码的解释很少:
broj ++;表示每次添加新行时递增的行号。这样我就得到了每一行的数字。
现在我的问题是:如何操纵第一列的宽度(代表行号)。我尝试了以下代码:
float[] widths = { 15.00F, 15.00F,15.00F,15.00F};
table.GetRow(1).SetWidths(widths);
有人可以帮我解决这个问题吗?
答案 0 :(得分:5)
结合您的原始帖子和评论,我看到两个问题:
我写了一个名为ColumnWidthExample的小样本,应该会产生这样的PDF:
这是生成此PDF的Java代码:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
float[] columnWidths = {1, 5, 5};
PdfPTable table = new PdfPTable(columnWidths);
table.setWidthPercentage(100);
table.getDefaultCell().setUseAscender(true);
table.getDefaultCell().setUseDescender(true);
Font f = new Font(FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
PdfPCell cell = new PdfPCell(new Phrase("This is a header", f));
cell.setBackgroundColor(GrayColor.GRAYBLACK);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(3);
table.addCell(cell);
table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
for (int i = 0; i < 2; i++) {
table.addCell("#");
table.addCell("Key");
table.addCell("Value");
}
table.setHeaderRows(3);
table.setFooterRows(1);
table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
for (int counter = 1; counter < 101; counter++) {
table.addCell(String.valueOf(counter));
table.addCell("key " + counter);
table.addCell("value " + counter);
}
document.add(table);
document.close();
}
将此代码改编为C#应该相当简单。
通过在表的级别定义列宽来解决第一个问题。例如像这样(还有其他方法可以获得相同的结果):
float[] columnWidths = {1, 5, 5};
PdfPTable table = new PdfPTable(columnWidths);
值1,5和5不是绝对宽度。当我们将表格的宽度定义为页面边距内可用宽度的100%时,iText会将可用宽度除以11(1 + 5 + 5),第一列将测量该值的1倍,而列两个和三个将测量该值的5倍。
通过更改水平对齐可以解决第二个问题。如果使用显式PdfPCell
对象,则需要更改单元格级别的值,如以下行所示:
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
如果您将其留给iText创建PdfPCell
对象,则需要更改默认单元格级别的对齐方式:
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
请注意,向表中添加PdfPCell
对象时,不会应用默认单元属性。在这种情况下,使用PdfPCell
的属性。
答案 1 :(得分:0)
好的,所以我设法将单元格的文本集中在每一列中,现在我无法弄清楚如何使第一行具有最小的宽度......
这是代码
public PdfPTable GetTable(List<hsp_Narudzbe_IzdavanjeFakture4_Result> proizvodi)
{
PdfPTable table = new PdfPTable(5);
table.WidthPercentage = 100F;
table.DefaultCell.UseAscender = true;
table.DefaultCell.UseDescender = true;
Font f = new Font(Font.FontFamily.HELVETICA, 13);
f.Color = BaseColor.WHITE;
PdfPCell cell = new PdfPCell(new Phrase("Stavke narudžbe: ", f));
cell.BackgroundColor = BaseColor.BLACK;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Colspan = 5;
table.AddCell(cell);
for (int i = 0; i < 2; i++)
{
table.AddCell(new PdfPCell(new Phrase("Redni broj"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
table.AddCell(new PdfPCell(new Phrase("Proizvod"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
table.AddCell(new PdfPCell(new Phrase("Cijena"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
table.AddCell(new PdfPCell(new Phrase("Šifra"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
table.AddCell(new PdfPCell(new Phrase("Kolicina"))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER,
BackgroundColor = BaseColor.LIGHT_GRAY
});
}
table.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
table.HeaderRows = 3;
table.FooterRows = 1;
int broj = 0;
table.DeleteLastRow();
foreach (hsp_Narudzbe_IzdavanjeFakture4_Result p in proizvodi)
{
broj++;
table.AddCell(new PdfPCell(new Phrase(broj.ToString()))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
table.AddCell(new PdfPCell(new Phrase(p.Naziv))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
table.AddCell(new PdfPCell(new Phrase(p.Cijena))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
table.AddCell(new PdfPCell(new Phrase(p.Sifra))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
table.AddCell(new PdfPCell(new Phrase(p.Kolicina.ToString()))
{
VerticalAlignment = Element.ALIGN_MIDDLE,
HorizontalAlignment = Element.ALIGN_CENTER
});
}
return table;
}
有人可以帮我弄清楚如何将每行的宽度设置为自定义尺寸吗?
谢谢!