[英]creation of pdf using iText in java
我下載了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()
。 為什么會發生此錯誤? 誰能告訴我?
我嘗試使用iText-7.1.3進行此操作。 它為我工作。
public static void main(String[] args) {
try {
PdfWriter writer = new PdfWriter(new FileOutputStream("/home/users/Documents/pdf/hello_world.pdf"));
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
doc.add(new Paragraph("Hello World"));
pdfDoc.addNewPage();
doc.close();
} catch(SvgProcessingException e ){
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
在這里添加了我使用過的jar文件。 我認為這可能對您有幫助。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/layout -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.1.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.1.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.1.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.1.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>7.1.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>sign</artifactId>
<version>7.1.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>barcodes</artifactId>
<version>7.1.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>hyph</artifactId>
<version>7.1.3</version>
</dependency>
這對我有用:
public static void createPdf() throws DocumentException, IOException {
File f = File.createTempFile("test", ".pdf");
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(f));
// step 3:gives error as no suitable method
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
// step 5
document.close();
}
因此,我認為問題出在您的文件名上,因為那是我更改的唯一部分。 嘗試使用E:\\ hello.pdf (帶反斜杠),並確保JVM在該位置具有寫訪問權限。
如果那不能解決您的問題,請提供完整的堆棧跟蹤。
您的代碼對我有用。 我唯一要做的更改是輸出文件名,即public static final String RESULT = "C:\\\\hello.pdf";
輸出文件名需要轉義字符“ \\”。
我使用itextpdf-5.3.2.jar進行了測試。
嘗試這個。
import java.io.FileOutputStream;
import java.io.IOException;
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 CreatePdf2 {
/** Path to the resulting PDF file. */
public static final String RESULT = "C:\\hello.pdf";
/**
* Creates a PDF file: hello.pdf
*
* @param args
* no arguments needed
*/
public static void main(String[] args) throws DocumentException,
IOException {
new CreatePdf2().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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.