Pdfbox 简明教程

PDFBox - Environment

Installing PDFBox

以下是下载 Apache PDFBox 的步骤 -

Following are the steps to download Apache PDFBox −

Step 1 − 通过单击以下链接打开 Apache PDFBox 的主页 − https://pdfbox.apache.org/

Step 1 − Open the homepage of Apache PDFBox by clicking on the following link − https://pdfbox.apache.org/

Step 2 − 上述链接会将您带到主页,如下面的屏幕截图所示 −

Step 2 − The above link will direct you to the homepage as shown in the following screenshot −

pdfbox homepage

Step 3 − 现在,在上述屏幕截图中,单击 Downloads 链接。单击后,您将被导向 PDFBox 的下载页面,如下面的屏幕截图所示。

Step 3 − Now, click on the Downloads link highlighted in the above screenshot. On clicking, you will be directed to the downloads page of PDFBox as shown in the following screenshot.

pdfbox downloads

Step 4 − 在下载页面中,您将获得用于下载 PDFBox 的链接。单击相应链接,获取最新版本。比如,我们选择的是 PDFBox 2.0.1 ,单击后,您将被带到所需的 jar 文件,如下面的屏幕截图所示。

Step 4 − In the Downloads page, you will have links for PDFBox. Click on the respective link for the latest release. For instance, we are opting for PDFBox 2.0.1 and on clicking this, you will be directed to the required jar files as shown in the following screenshot.

pdfbox jarfiles

Step 5 − 下载 jar 文件 pdfbox-2.0.1.jar、fontbox-2.0.1.jar、preflight-2.0.1.jar、xmpbox-2.0.1.jar 以及 pdfbox-tools-2.0.1.jar。

Step 5 − Download the jar files pdfbox-2.0.1.jar, fontbox-2.0.1.jar, preflight-2.0.1.jar, xmpbox-2.0.1.jar and, pdfbox-tools-2.0.1.jar.

Eclipse Installation

下载必需的 jar 文件后,您需要将这些 JAR 文件嵌入到 Eclipse 环境中。您可以通过将构建路径设置为这些 JAR 文件并使用 pom.xml 来执行此操作。

After downloading the required jar files, you have to embed these JAR files to your Eclipse environment. You can do this by setting the Build path to these JAR files and by using pom.xml.

Setting Build Path

以下是将 PDFBox 安装到 Eclipse 中所需的步骤 -

Following are the steps to install PDFBox in Eclipse −

Step 1 − 确保在您的系统中安装了 Eclipse。如果没有,请下载并安装 Eclipse。

Step 1 − Ensure that you have installed Eclipse in your system. If not, download and install Eclipse in your system.

Step 2 − 打开 Eclipse,单击文件、新建,打开一个新项目,如下面的屏幕截图所示。

Step 2 − Open Eclipse, click on File, New, and Open a new project as shown in the following screenshot.

eclipse file menu

Step 3 − 选择项目后,您将获得 New Project 向导。在此向导中,选择 Java 项目,然后单击 Next 按钮,如下面的屏幕截图所示。

Step 3 − On selecting the project, you will get New Project wizard. In this wizard, select Java project and proceed by clicking Next button as shown in the following screenshot.

eclipse newproject wizard

Step 4 − 继续前进后,您将被带到 New Java Project wizard 。创建一个新项目,然后单击 Next ,如下面的屏幕截图所示。

Step 4 − On proceeding forward, you will be directed to the New Java Project wizard. Create a new project and click on Next as shown in the following screenshot.

create project wizard

Step 5 − 创建新项目后,右键单击它;选择 Build Path 并单击 Configure Build Path… ,如下面的屏幕截图所示。

Step 5 − After creating a new project, right click on it; select Build Path and click on Configure Build Path… as shown in the following screenshot.

eclipse build path

Step 6 − 单击 Build Path 选项后,您会被定向到 Java Build Path wizard 。 如以下屏幕截图所示,选择 Add External JARs

Step 6 − On clicking on the Build Path option you will be directed to the Java Build Path wizard. Select the Add External JARs as shown in the following screenshot.

eclipse external jars

Step 7 − 如以下屏幕截图所示,选择 Jar 文件 fontbox-2.0.1.jar, pdfbox-2.0.1.jar, pdfbox-tools-2.0.1.jar, preflight-2.0.1.jar, xmpbox-2.0.1.jar

Step 7 − Select the jar files fontbox-2.0.1.jar, pdfbox-2.0.1.jar, pdfbox-tools-2.0.1.jar, preflight-2.0.1.jar, xmpbox-2.0.1.jar as shown in the following screenshot.

jarfiles location

Step 8 − 单击上述屏幕截图中的 Open 按钮后,这些文件将被添加到您的代码库,如下所示。

Step 8 − On clicking the Open button in the above screenshot, those files will be added to your library as shown in the following screenshot.

jarfiles added

Step 9 − 单击 OK ,您将成功地将所需的 JAR 文件添加到当前项目中,并且可以通过展开引用的代码库来验证这些添加的代码库,如下所示。

Step 9 − On clicking OK, you will successfully add the required JAR files to the current project and you can verify these added libraries by expanding the Referenced Libraries as shown in the following screenshot.

eclipse jar files

Using pom.xml

将项目转换为 Maven 项目并添加以下内容到其 pom.xml.

Convert the project into maven project and add the following contents to its pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>my_project</groupId>
   <artifactId>my_project</artifactId>
   <version>0.0.1-SNAPSHOT</version>

   <build>
      <sourceDirectory>src</sourceDirectory>
      <plugins>
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
            </configuration>
         </plugin>
      </plugins>
   </build>

   <dependencies>
      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>pdfbox</artifactId>
         <version>2.0.1</version>
      </dependency>

      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>fontbox</artifactId>
         <version>2.0.0</version>
      </dependency>

      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>jempbox</artifactId>
         <version>1.8.11</version>
      </dependency>

      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>xmpbox</artifactId>
         <version>2.0.0</version>
      </dependency>

      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>preflight</artifactId>
         <version>2.0.0</version>
      </dependency>

      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>pdfbox-tools</artifactId>
         <version>2.0.0</version>
      </dependency>

   </dependencies>

</project>