【问题标题】:How to create a completely new PDF file if there is NO existing file?如果没有现有文件,如何创建一个全新的 PDF 文件?
【发布时间】:2021-01-14 14:43:07
【问题描述】:

我正在使用 iText 库在 java 中创建 PDF 文件。唯一的问题是:我必须先手动创建一个 PDF 文件,然后获取它的路径并像这样放置:

private final static String FILE = "C:\\Projekt\\lul.pdf";
PdfWriter.getInstance(document, new FileOutputStream(FILE));

我的问题是:如何使用 iText 库而无需手动创建 PDF 文件?

【问题讨论】:

    标签: java file pdf itext


    【解决方案1】:
    private final static String FILE = "C:\\Projekt\\lul.pdf";
    File yourFile = new File(FILE);
    yourFile.createNewFile(); // if file already exists will do nothing 
    PdfWriter.getInstance(document, new FileOutputStream(yourFile));
    

    【讨论】:

      【解决方案2】:

      iText 是一个开源 API,它也允许您这样做。

      1. 在 pom.xml 中添加以下依赖:

         <dependency>
             <groupId>com.itextpdf</groupId>
             <artifactId>itextpdf</artifactId>
         </dependency>
        
      2. Java 类:

          Document document = new Document();
          PdfWriter writer = PdfWriter.getInstance(document, new 
          FileOutputStream(Example.pdf;));
          document.open();
          document.add(new Paragraph(Welcome To StackOverflow;));
          document.close();
          writer.close();
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-10
        • 1970-01-01
        • 2015-03-07
        • 2021-09-28
        • 1970-01-01
        • 2017-11-05
        • 2013-07-25
        相关资源
        最近更新 更多