我下载了 iTextpdf-5.1.0 并将其添加到我的项目库中。
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* First iText example: Hello World.
*/
public class Testcase {
/** Path to the resulting PDF file. */
public static final String RESULT= "E:/hello.pdf";
/**
* Creates a PDF file: hello.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
new Testcase().createPdf(RESULT);
}
/**
* Creates a PDF document.
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3:gives error as no suitable method
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
// step 5
document.close();
}
}
在第 3 步:它给了我以下错误:no suitable method found for getInstance()
. 为什么会出现这个错误?谁能告诉我?