在java中使用iText创建pdf

时间:2012-08-20 11:01:19

标签: java swing itext

我下载了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()。 为什么会出现此错误?有谁能告诉我?

3 个答案:

答案 0 :(得分:1)

我尝试使用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>

答案 1 :(得分:0)

这对我有用:

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在该位置具有写访问权。

如果这不能解决您的问题,请提供完整的堆栈跟踪。

答案 2 :(得分:0)

您的代码适合我。我唯一需要做的改变是输出文件名public static final String RESULT = "C:\\hello.pdf";输出文件名需要转义字符&#34; \&#34;

我使用 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();
    }
}