简体   繁体   English

使用 itext 时出错

[英]Errors while using itext

I am getting 2 errors in this code -我在此代码中收到 2 个错误 -

  1. The method setWidthPercentage(int) is undefined for the type table.类型表的 setWidthPercentage(int) 方法未定义。

  2. The method add(IBlockElement) in the type Cell is not applicable for the arguments (String). Cell 类型中的 add(IBlockElement) 方法不适用于参数 (String)。

I am using the following jars - io-7.1.4, kernel-7.1.4, layout-7.1.4, svg-7.1.4, slf4j-api-1.7.13, styled-xml-parser-7.1.4我正在使用以下 jars - io-7.1.4, kernel-7.1.4, layout-7.1.4, svg-7.1.4, slf4j-api-1.7.13, styled-xml-parser-7.1.4

I am trying the example ColspanRowspan which is given on Itext Website.我正在尝试在 Itext 网站上提供的示例ColspanRowspan

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;

import java.io.File;

public class ColspanRowspan {
    public static final String DEST = "./target/test/resources/sandbox/tables/simple_row_colspan.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ColspanRowspan().manipulatePdf(DEST);
    }


    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Table table = new Table(new float[]{1, 2, 2, 2, 1});
        table.setWidthPercent(100);

        Cell cell;
        cell = new Cell(2, 1).add("S/N");
        table.addCell(cell);
        cell = new Cell(1, 3).add("Name");
        table.addCell(cell);
        cell = new Cell(2, 1);
                cell.add("Age");
        table.addCell(cell);
        table.addCell("SURNAME");
        table.addCell("FIRST NAME");
        table.addCell("MIDDLE NAME");
        table.addCell("1");
        table.addCell("James");
        table.addCell("Fish");
        table.addCell("Stone");
        table.addCell("17");
        doc.add(table);

        doc.close();
    }
}

Actually, you aren't using itext API's correctly in your class.实际上,您没有在类中正确使用itext API Below are the remedies for your errors:-以下是针对您的错误的补救措施:-

  1. Method name itself is incorrect.方法名称本身不正确。 In fact, there is no method with this name in Table class.事实上,Table类中并没有这个名字的方法。 It's Table.setWidth(UnitValue width) which " Sets the full width of the table ".它是Table.setWidth(UnitValue width) ,它“设置表格的全宽”。

--> Reference:- com.itextpdf.layout.element.Table --> 参考:- com.itextpdf.layout.element.Table

  1. String type isn't supposed to be passed as an argument in your scenario for add(IBlockElement) method.add(IBlockElement)方法的场景中,不应将字符串类型作为参数传递。 Rather than you should use this Cell.add(IBlockElement element) looking properly for your requirement which " Adds any block element to the cell's contents ".而不是您应该使用此Cell.add(IBlockElement element)正确查找“将任何块元素添加到单元格的内容”的要求。

--> Reference:- com.itextpdf.layout.element.Cell --> 参考:- com.itextpdf.layout.element.Cell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM