如何设置每行具有不同列数的 table?

How do I set a table with different amount of columns on each row?

我刚刚了解 iText7,但我似乎无法修复 table 的布局。 我画了我想要的东西只是为了给你看(请不要讨厌我的绘画技巧:P)。

仅供参考:

不知何故,我的第一行一直在设置下面所有单元格的宽度。

我尝试使用普通单元格和标头单元格,但我一直得到相同的结果。

    private void TestPDF()
    {
        var CustomSize= new Rectangle(241, 142);
        var writer = new PdfWriter("Test " + DateTime.Now.ToString("dd-MM-yyy") + ".pdf");
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf, new PageSize(CustomSize));
        document.SetMargins(0, 0, 0, 0);

        var tableinformatie = new Table(2);
        tableinformatie.SetWidth(241);
        tableinformatie.SetHeight(142);
        tableinformatie.SetPadding(0);
        tableinformatie.AddHeaderCell("test").UseAllAvailableWidth();
        tableinformatie.AddCell("test").SetWidth(241);
        tableinformatie.AddCell("test").SetWidth(100);

        document.Add(tableinformatie);
        document.Close();
    }

到目前为止,这是我的代码,我尝试使用固定布局。设置每个单元格的宽度和高度,但它不起作用。

我一定是忽略了什么。

提前谢谢你。

编辑:

我尝试了多个 table 的想法,而不是用 1 个 table 来固定它。这对我有用:)!

private void StickerInMM()
{
        var Sticker = new Rectangle(241, 142);
        var writer = new PdfWriter("Test " + DateTime.Now.ToString("dd-MM-yyy") + ".pdf");
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf, new PageSize(Sticker));
        document.SetMargins(0, 0, 0, 0);


        ImageData data = ImageDataFactory.Create("c:/Users/Public/Cyaan.jpg");
        Image img = new Image(data);
        img.SetHeight(15);

        ImageData data2 = ImageDataFactory.Create("c:/Users/Public/Tekst.jpg");
        Image img2 = new Image(data2);
        img2.ScaleToFit(100, 100);

        var EANFONT = PdfFontFactory.CreateFont("c:/Users/Public/ean13.ttf", PdfEncodings.IDENTITY_H, true);
        var cellean = new Paragraph(richTextBox2.Text)
            .SetFontSize(38)
            .SetFont(EANFONT);

        var table = new Table(new float[] { 1 });
        table.SetWidth(UnitValue.CreatePercentValue(100));
        table.AddCell("Laser toner cartridge");
        document.Add(table);

        var table2 = new Table(new float[] { 3, 1 });
        table2.SetWidth(UnitValue.CreatePercentValue(100));
        table2.AddCell("Test");
        table2.AddCell(img);
        document.Add(table2);

        var table3 = new Table(new float[] { 1 });
        table3.SetWidth(UnitValue.CreatePercentValue(100));
        table3.AddCell("Test");
        document.Add(table3);

        var table4 = new Table(new float[] { 2, 2 });
        table4.SetWidth(UnitValue.CreatePercentValue(100));
        table4.AddCell(img2);          
        table4.AddCell(cellean);
        document.Add(table4);

        document.Close();
    }

这现在创建了具有正确尺寸的正确 table。

谢谢 Paulo Soares 的想法。

private void StickerInMM()
{
        var Sticker = new Rectangle(241, 142);
        var writer = new PdfWriter("Test " + DateTime.Now.ToString("dd-MM-yyy") + ".pdf");
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf, new PageSize(Sticker));
        document.SetMargins(0, 0, 0, 0);


        ImageData data = ImageDataFactory.Create("c:/Users/Public/Cyaan.jpg");
        Image img = new Image(data);
        img.SetHeight(15);

        ImageData data2 = ImageDataFactory.Create("c:/Users/Public/Tekst.jpg");
        Image img2 = new Image(data2);
        img2.ScaleToFit(100, 100);

        var EANFONT = PdfFontFactory.CreateFont("c:/Users/Public/ean13.ttf", PdfEncodings.IDENTITY_H, true);
        var cellean = new Paragraph(richTextBox2.Text)
            .SetFontSize(38)
            .SetFont(EANFONT);

        var table = new Table(new float[] { 1 });
        table.SetWidth(UnitValue.CreatePercentValue(100));
        table.AddCell("Laser toner cartridge");
        document.Add(table);

        var table2 = new Table(new float[] { 3, 1 });
        table2.SetWidth(UnitValue.CreatePercentValue(100));
        table2.AddCell("Test");
        table2.AddCell(img);
        document.Add(table2);

        var table3 = new Table(new float[] { 1 });
        table3.SetWidth(UnitValue.CreatePercentValue(100));
        table3.AddCell("Test");
        document.Add(table3);

        var table4 = new Table(new float[] { 2, 2 });
        table4.SetWidth(UnitValue.CreatePercentValue(100));
        table4.AddCell(img2);          
        table4.AddCell(cellean);
        document.Add(table4);

        document.Close();
    }

用上面的代码修复了它 :D !