微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iText:如何将字符串添加到单元格

如何解决iText:如何将字符串添加到单元格

大家好,我是一个绝对的初学者,所以对我放轻松,为什么这对我不起作用,我真的不明白为什么这不起作用,但我是初学者,所以如果有人可以为我纠正这个,我真的很感激。 我很惊讶它不起作用



import com.itextpdf.kernel.geom.PageSize;
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.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
        String path = "C:\\Users\\Kevin\OneDrive\\Desktop\\Generated PDFs\\Table.pdf";
        String logosrc = "C:\\Users\\Kevin\\OneDrive\\Desktop\\Generated PDFs\\Images\\mainlogo.png";


        PdfWriter pdfWriter = new PdfWriter(path);
        PdfDocument pdfDocument = new PdfDocument(pdfWriter);
        Document document = new Document(pdfDocument);
        pdfDocument.setDefaultPageSize(PageSize.A4);

        float col = 280f;
        float columnWidth[] = {col,col};
        Table table = new Table(columnWidth);

        table.addCell(new Cell().add("INVOICE"));

我的错误消息:

java: no suitable method found for add(java.lang.String)
    method com.itextpdf.layout.element.Cell.add(com.itextpdf.layout.element.IBlockElement) is not applicable
      (argument mismatch; java.lang.String cannot be converted to com.itextpdf.layout.element.IBlockElement)
    method com.itextpdf.layout.element.Cell.add(com.itextpdf.layout.element.Image) is not applicable
      (argument mismatch; java.lang.String cannot be converted to com.itextpdf.layout.element.Image)

解决方法

iText 7.0 为 add(String) 提供了一个方法 Cell

对于 iText 7.1,作为 documented in the APICell 仅采用 块元素图像 作为其 add() 方法. Block elements in iText 是段落、列表和 div 之类的东西。

您可以将 String 包裹在 Paragraph 中:

table.addCell(new Cell().add(new Paragraph("INVOICE")));

有关 7.0 和 7.1 之间的进一步差异,查看 7.1 migration guide 可能会有所帮助,其中也包括此更改:

com.itextpdf.layout.element.Cell#add(java.lang.String) 已被删除,请改用 BlockElement,例如 Paragraph

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。