0

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

4

3 回答 3

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>
于 2018-12-19T12:55:12.633 回答
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 在该位置具有写访问权限。

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

于 2012-08-20T13:57:41.937 回答
0

你的代码对我有用。我要做的唯一更改是输出文件名, 即 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();
    }
}
于 2012-08-23T03:24:54.053 回答