per*_*456 1 c# asp.net height itextsharp width
我使用以下代码填充我的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;
}
Run Code Online (Sandbox Code Playgroud)
代码的一点解释:
broj ++; 表示每次添加新行时递增的行号.这样我就得到了每一行的数字.
现在我的问题是:我如何操纵第一列的宽度(代表行号).我试过以下代码:
float[] widths = { 15.00F, 15.00F,15.00F,15.00F};
table.GetRow(1).SetWidths(widths);
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题吗?
结合您的原始帖子和评论,我看到两个问题:
我写了一个名为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();
}
Run Code Online (Sandbox Code Playgroud)
将此代码调整为C#应该相当简单.
第一个问题是通过在表级定义列宽来解决的.例如像这样(有其他方法可以得到相同的结果):
float[] columnWidths = {1, 5, 5};
PdfPTable table = new PdfPTable(columnWidths);
Run Code Online (Sandbox Code Playgroud)
值1,5和5不是绝对宽度.当我们将表格的宽度定义为页面边距内可用宽度的100%时,iText会将可用宽度除以11(1 + 5 + 5),第一列将测量该值的1倍,而列两个和三个将测量该值的5倍.
您可以通过更改水平对齐来解决第二个问题.如果使用显式PdfPCell
对象,则需要更改单元级别的值,如以下行所示:
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Run Code Online (Sandbox Code Playgroud)
如果将其保留到iText以创建PdfPCell
对象,则需要更改默认单元格级别的对齐:
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
Run Code Online (Sandbox Code Playgroud)
请注意,PdfPCell
向表中添加对象时,不会应用默认单元属性.在这种情况下,使用它的属性PdfPCell
.