📜  使用Java创建一个空的 PDF 文档

📅  最后修改于: 2022-05-13 01:54:43.417000             🧑  作者: Mango

使用Java创建一个空的 PDF 文档

要使用Java创建 PDF 文档,我们需要知道编写代码所需的包/库。因此,为了创建 PDF 文档,我们将使用iText 7库。要了解如何在您的工作区中安装此库,您可以点击此链接。

创建一个空的 PDF 文档

要创建一个空的 PDF 文档,我们首先需要实例化Document类,在此过程中,我们必须传递一个 PdfDocument 对象作为参数。以下是用于创建空 PDF 文档的组件。

1.PDFWritter

PdfWritter 类属于包com.itextpdf.kernel.pdf。我们可以说这个类是 PDF 的 Doc Writer。在实例化 PdfWritter 对象的对象时,我们必须传递一个字符串值,它表示我们希望创建 PDF 的文件路径。

注意:在传递文件路径时请确保您的 IDE 具有在该目录中写入/读取文件的权限,否则会出现FileNotFound错误。

句法

String path = "C:/JavaExamples/example.pdf";
PdfWriter writer = new PdfWriter(path);

2.PDF文档

PdfDocument 类属于包com.itextpdf.kernel.pdf 。此类代表PDF 文档。在实例化此类时,我们必须将PdfWriter 对象作为参数传递。

句法

//writer is the PdfWriter object
PdfDocument pdf = new PdfDocument(writer);

3. 文件

Document 类属于包com.itextpdf.layout 。它是IText中的核心类之一。如果要从头开始生成 PDF,则必须使用Document Class 。在实例化 Document 类时,我们需要将PdfDocument 对象作为参数传递。

创建文档对象的语法:

//pdf is the PdfDocument object
Document doc = new Document(pdf);

4. 关闭文档

使用Document类的close()方法关闭文档,如下所示。

// Closing the document 
doc.close();

示例 1:

Java
// let us import all required packages
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
 
public class Pdf {
 
    public static void main(String args[]) throws Exception
    {
        // Creating a PdfWriter to C:/example.pdf
        String path = "C:/example.pdf";
        PdfWriter writer = new PdfWriter(path);
 
        // Creating a PdfDocument object
        PdfDocument pdf = new PdfDocument(writer);
 
        // Creating a Document object
        Document document = new Document(pdf);
 
        // to check if its created or not
        System.out.println("Your PDF has been created");
    }
}


Java
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
 
public class Pdf {
 
    public static void main(String args[]) throws Exception
    {
        // Setting the Path
        String path = "C:/example2.pdf";
 
        // Creating a PdfWriter object
        PdfWriter writer = new PdfWriter(path);
 
        // Creating a PdfDocument object
        PdfDocument pdf = new PdfDocument(writer);
 
        // this line is used to add a
        // new page in the pdf
        pdfDoc.addNewPage();
 
        // Creating a Document object
        Document document = new Document(pdf);
 
        // Closing the document object
        document.close();
 
        System.out.println("Your pdf has been created");
    }
}


输出

创建一个pdf

笔记:

  1.  这是一个空文件, 你可能会得到 打开此 PDF 时出错,因为这是一个 0 页的 PDF。
  2. 如果您没有在该目录中写入/读取文件的权限,您可能会收到FIileNotFound 错误,因此您可以以管理员身份运行您的 IDE ,然后它就会工作。

示例 2:

现在让我们创建一个带有空白页面的 PDF。

Java

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
 
public class Pdf {
 
    public static void main(String args[]) throws Exception
    {
        // Setting the Path
        String path = "C:/example2.pdf";
 
        // Creating a PdfWriter object
        PdfWriter writer = new PdfWriter(path);
 
        // Creating a PdfDocument object
        PdfDocument pdf = new PdfDocument(writer);
 
        // this line is used to add a
        // new page in the pdf
        pdfDoc.addNewPage();
 
        // Creating a Document object
        Document document = new Document(pdf);
 
        // Closing the document object
        document.close();
 
        System.out.println("Your pdf has been created");
    }
}

输出

创建一个pdf

让我们打开那个PDF。

打开pdf