方法add(Element)不适用于参数(String [])

时间:2014-12-09 16:09:37

标签: java swing pdf itext

我正在尝试使用数组元素创建一个段落将其写入pdf文件,如果我只添加一行 paragraph.add(“这是内容”); 其工作细;

当我尝试使用数组时,会出现错误snippet

请给我指示解决此问题,谢谢。

代码:

public void readStuff(){

String[] arrayName = new String[7];

try{
    PdfReader reader = new PdfReader(new FileInputStream("My First PDF Doc.pdf"));

    arrayName[0] = "PDF version: " + reader.getPdfVersion();
    arrayName[1] = "Number of pages " + reader.getNumberOfPages();
    arrayName[2] = "File length: " + reader.getFileLength();
    arrayName[3] = "Is it encrypted: " + reader.isEncrypted();
    arrayName[4] = "Width of the page 1: " + reader.getPageSize(1).getWidth();
    arrayName[5] = "Height of the page 1: " + reader.getPageSize(1).getHeight();
    arrayName[6] = "Rotation of page 1: " + reader.getPageRotation(1);

    for(int x = 0; x < 7; x++){
        System.out.println(arrayName[x]);
    }

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("My First PDF Doc.pdf"));
    document.open();

    Paragraph paragraph = new Paragraph();
    paragraph.add(arrayName);
    document.add(paragraph);
    document.close();


} catch(Exception e){
    e.printStackTrace();
}
};

错误:

The Method add(Element) in the type Paragraph is not applicable for the arguments (String[])

1 个答案:

答案 0 :(得分:5)

用此

替换paragraph.add(arrayName);
for (int i = 0; i < arrayName.length; i++) {
    paragraph.add(arrayName[i]);
}