【问题标题】:How do I set a table with different amount of columns on each row?如何设置每行具有不同列数的表格?
【发布时间】:2019-10-21 14:44:37
【问题描述】:

我只是在学习 iText7,但我似乎无法修复我的表格布局。 我在绘画中画了我想要的东西,只是为了向你展示(请不要讨厌我的绘画技巧: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();
    }

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

我一定是忽略了什么。

提前谢谢你。

编辑:

我玩弄了多张桌子的想法,而不是用一张桌子来固定它。这对我有用:)!

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();
    }

这会创建具有正确大小的正确表格。

感谢 Paulo Soares 的想法。

【问题讨论】:

  • 你想要的不是一张桌子,而是一堆堆叠的桌子。您可以使用列跨度,但使用多个表可能更容易。
  • 这是一个很好的解决方法,但它给我留下了表格之间的空白。不知道如何解决这个问题:S
  • 请使用解决方案对您的问题进行编辑(不是对问题的编辑),然后您可以接受。
  • Tagging help: “你应该在标题中使用标签的唯一时间是当它们与标题的对话语气有机结合时。”

标签: c# pdf itext7


【解决方案1】:
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!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多