繁体   English   中英

在Java中使用iText创建pdf

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM