简体   繁体   English

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

[英]iText: how to add a String to a cell

Hi guys I am an absolute beginner so go easy on me, why is this not working for me, I really do not understand why this is not working, but I am a beginner so if someone could correct this for me, I would really appreciate it.大家好,我是一个绝对的初学者,所以 go 对我来说很容易,为什么这对我不起作用,我真的不明白为什么这不起作用,但我是初学者,所以如果有人可以为我纠正这个问题,我将不胜感激它。 I am surprised that it does not work我很惊讶它不起作用



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"));

My Error messege:我的错误信息:

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 had a method add(String) for Cell . iText 7.0 有一个方法add(String)用于Cell

For iText 7.1, as documented in the API , a Cell takes only a block element or an image for its add() method.对于 iText 7.1,如API 中所述Cell仅为其add()方法采用块元素图像 Block elements in iText are things like paragraphs, lists and divs. iText 中的块元素是段落、列表和 div 之类的东西。

You can wrap your String in a Paragraph :您可以将String包装在Paragraph中:

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

For further differences between 7.0 and 7.1, it may be useful to take a look at the 7.1 migration guide , which also include this change:对于 7.0 和 7.1 之间的进一步差异,查看7.1 迁移指南可能会有所帮助,其中也包括此更改:

com.itextpdf.layout.element.Cell#add(java.lang.String) has been removed, use a BlockElement such as Paragraph instead. com.itextpdf.layout.element.Cell#add(java.lang.String)已被删除,请改用诸如Paragraph之类的BlockElement

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

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