【问题标题】:IText7 Column width issueIText7列宽问题
【发布时间】:2021-06-10 20:43:08
【问题描述】:

我一直在尝试使用 iText7不同的列大小 在表格中创建 PDF 文档,在我的代码中,我已经为每列设置了宽度.但我无法得到想要的结果。

版本 - itext7-core:7.1.15

这里是部分代码:

private void ManipulatePdf(string pdfPath)
{
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfPath));
    Document doc = new Document(pdfDoc);
    doc.SetMargins(20, 20, 20, 20);
    Table table2 = new Table(UnitValue.CreatePercentArray(new float[] {10,20,40,10,10,20,10 })).UseAllAvailableWidth();

    
    table2.SetWidth(UnitValue.CreatePercentValue(100));
    
    table2.SetFixedLayout();

    table2.AddCell(getCellm1(300));
    table2.AddCell(getCellm2(240));
    table2.AddCell(getCellm3(100));
    table2.AddCell(getCellm4(100));
    table2.AddCell(getCellm5(100));
    table2.AddCell(getCellm6(300));
    table2.AddCell(getCellm6(300));
    doc.Add(table2);
    doc.Close();
}

private Cell getCellm7(int cm)
{
    Cell cell = new Cell(1, 7);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm6(int cm)
{
    Cell cell = new Cell(1, 6);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm5(int cm)
{
    Cell cell = new Cell(1, 5);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm4(int cm)
{
    Cell cell = new Cell(1, 4);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm3(int cm)
{
    Cell cell = new Cell(1, 3);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm2(int cm)
{
    Cell cell = new Cell(1, 2);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

private Cell getCellm1(int cm)
{
    Cell cell = new Cell(1, 1);

    Paragraph p = new Paragraph(
    String.Format("%smm", 10 * cm)).SetFontSize(8);
    p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
    cell.Add(p);
    return cell;
}

以上代码的输出

从上面的代码中得到不同的列大小,我想要的结果如下:

这就是我希望列的样子:

如果有人使用 iText 来做这样的事情,任何建议都将不胜感激。如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: c# pdf itext itext7


    【解决方案1】:

    您的百分比值 (10,20,40,10,10,20,10) 加起来是 120,而不是 100。调用 .UseAllAvailableWidth() 等同于 table2.SetWidth(UnitValue.CreatePercentValue(100)); - 无需同时执行这两个操作。

    您还可以将数字传递给Cell 构造函数(例如new Cell(1, 4))。这些数字实际上并不表示单元格的位置,而是表示单元格的行跨度和列跨度。这才是意外布局的真正原因。除非您希望单元格占据多行或多列,否则您不必向单元格构造函数传递任何内容。

    修复上述错误会得到以下结果:

    参考代码:

    private void ManipulatePdf(string pdfPath) {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfPath));
        Document doc = new Document(pdfDoc);
        doc.SetMargins(20, 20, 20, 20);
        Table table2 = new Table(UnitValue.CreatePercentArray(new float[] {8, 15, 38, 8, 8, 15, 8}))
            .UseAllAvailableWidth();
    
        table2.SetFixedLayout();
    
        table2.AddCell(CreateCell("S.No"));
        table2.AddCell(CreateCell("Item Code"));
        table2.AddCell(CreateCell("Description"));
        table2.AddCell(CreateCell("Qty"));
        table2.AddCell(CreateCell("Unit"));
        table2.AddCell(CreateCell("Unit Price"));
        table2.AddCell(CreateCell("P.Unit"));
    
        doc.Add(table2);
        doc.Close();
    }
    
    private Cell CreateCell(String text) {
        Cell cell = new Cell();
        Paragraph p = new Paragraph(text).SetFontSize(8);
        p.SetTextAlignment(TextAlignment.CENTER);
        cell.Add(p);
        return cell;
    }
    

    【讨论】:

    • 让我检查一下.. 感谢您的回复
    猜你喜欢
    • 1970-01-01
    • 2021-01-21
    • 2020-08-25
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多