您的位置 首页 java

如何使用 Java 在 PDF 文档中添加、删除或旋转页面

学习 API 解决方案,用于将页面从一个 PDF 添加到另一个、从 PDF 中删除页面、旋转 PDF 中的所有页面以及旋转 PDF 中的部分页面。

如何使用 Java 在 PDF 文档中添加、删除或旋转页面

鉴于在线提供的免费 PDF 编辑工具的数量(包括 PDF 文件中存在的本机工具),在需要时对 PDF 文档进行小规模更改并不难。然而,在更大范围内进行同样的改变带来了全新的挑战。

假设您有 100 个 PDF 文件,每个文件都需要相同的编辑操作。在这种情况下,手动执行更改是没有意义的:该过程的低效率最终会产生比它解决的问题更多的问题,从而妨碍竞争任务/优先级。大规模更改大量 PDF 文档的最有效方法是以编程方式进行,使用旨在与压缩 PDF 文件通信并轻松操作其属性的 API。

在本文中,我将演示四种易于使用的 API 解决方案,它们允许您以编程方式在 PDF 文档中大规模添加、删除或旋转页面。这些解决方案提供以下特定服务:

将一个 PDF 文档中的页面插入/复制到另一个文档中
从 PDF 文档中删除/删除页面
旋转 PDF 文档中的所有页面
旋转 PDF 文档中的特定范围/页面子集
下面,我重点介绍了此列表中每个 API 解决方案的一些用例和所需参数,并提供了 Java 代码示例以帮助您构建 API 调用。这些 API 完全免费用于有限规模的操作(每月涉及不超过 800 次 API 调用的操作),且额外承诺为零。要获得安全的 API 密钥,您只需在我们的网站上注册一个免费帐户即可。一旦你有了它,你就可以在下面提供的代码示例中轻松且重复地验证 API 访问。

安装 API 客户端
在深入研究每个 API 解决方案的用例和参数之前,我们的第一步是安装 Cloudmersive API 客户端。我们可以通过首先添加对我们的 maven POM 文件的引用来开始这样做,如下所示:

<repositories>
<repository>
<id>jitpack.io</id>
<url>
</repository>
</repositories>

然后我们可以在依赖项中包含以下引用,使 JitPack 能够编译库:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

或者,我们可以通过在存储库末尾的根 build. Gradle 中包含以下内容来安装Gradle :

allprojects {
repositories {

maven { url ‘#39; }
}
}

之后,我们可以在 build.gradle 中添加依赖:
dependencies {
implementation ‘com.github.Cloudmersive:Cloudmersive.APIClient.Java:v4.25’
}

最后,我们需要在文件顶部包含以下导入:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.Api Exception ;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditPdfApi;

将页面从一个 PDF 插入、复制到另一个
正如这个 API 的标题所暗示的,我们可以使用这种“插入、复制”解决方案将来自源 PDF 的特定范围的页面包含在目标 PDF 中。这是快速编译大型报告、备忘录、营销样本等的绝佳工具。此 API 的参数允许我们准确指定要从哪些页面开始和结束复制,以及将这些复制的页面插入目标文档的具体位置。虽然源文件和目标文件输入很容易导航,但在配置以下三个参数时,我们需要更加注意:

pageStartSource:开始复制内容的页面(从 1 开始);此页面的内容将包含在目标文件中。
pageEndSource:要从中复制内容的最后一页(从 1 开始);此页面的内容将包含在目标文件中。
pageInsertBeforeDestination:目标 PDF 中应包含复制内容的页面(从 1 开始)。
随着我们的参数平方,我们可以像这样构造 API 调用:

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication(“Apikey”);
Apikey.setApiKey(“YOUR API KEY”);
// Uncomment the following line to set a prefix for the API key, e.g. “Token” (defaults to null)
//Apikey.setApiKeyPrefix(“Token”);

EditPdfApi apiInstance = new EditPdfApi();
File sourceFile = new File(“/path/to/inputfile”); // File | Source PDF file to copy pages from.
File destinationFile = new File(“/path/to/inputfile”); // File | Destination PDF file to copy pages into.
Integer pageStartSource = 56; // Integer | Page number (1 based) to start copying pages from (inclusive) in the Source file.
Integer pageEndSource = 56; // Integer | Page number (1 based) to stop copying pages pages from (inclusive) in the Source file.
Integer pageInsertBeforeDesitnation = 56; // Integer | Page number (1 based) to insert the pages before in the Destination file.
try {
byte [] result = apiInstance.editPdfInsertPages(sourceFile, destinationFile, pageStartSource, pageEndSource, pageInsertBeforeDesitnation);
System.out.println(result);
} catch (ApiException e) {
System.err.println(“Exception when calling EditPdfApi#editPdfInsertPages”);
e.printStackTrace();
}

从 PDF 文档中删除、删除页面
如果我们的目标是从 PDF 文件中大规模删除页面,我们可以采用“删除,删除”解决方案。此页面删除解决方案有许多用例。举一个常见的例子,它在需要跨组织共享多个版本的私人报告/备忘录的情况下特别有用。要从输入 PDF 中删除页面,我们必须包含 PDF 文件路径,然后满足以下参数:

pageStart:指定从哪个页面(从 1 开始)开始删除,并删除该页面
pageEnd:指定从哪个页面(从 1 开始)完成删除,并删除该页面
我们可以像这样构造我们的页面删除 API 调用,再次从我们的 API 密钥授权片段开始:

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication(“Apikey”);
Apikey.setApiKey(“YOUR API KEY”);
// Uncomment the following line to set a prefix for the API key, e.g. “Token” (defaults to null)
//Apikey.setApiKeyPrefix(“Token”);

EditPdfApi apiInstance = new EditPdfApi();
File inputFile = new File(“/path/to/inputfile”); // File | Input file to perform the operation on.
Integer pageStart = 56; // Integer | Page number (1 based) to start deleting pages from (inclusive).
Integer pageEnd = 56; // Integer | Page number (1 based) to stop deleting pages from (inclusive).
try {
byte[] result = apiInstance.editPdfDeletePages(inputFile, pageStart, pageEnd);
System.out.println(result);
} catch (ApiException e) {
System.err.println(“Exception when calling EditPdfApi#editPdfDeletePages”);
e.printStackTrace();
}

旋转 PDF 文档中的所有页面
无论是有意还是由于疏忽,大型 PDF 文件(尤其是从 JPG、PNG、PowerPoint PPTX 等格式转换而来的文件)通常包含以不希望的角度旋转的页面。当 PDF 文件中的每一页都错误地旋转到相同的角度时,我们很幸运:我们可以使用简单的“旋转所有页面”解决方案来立即纠正问题。我们只需要密切关注rotationAngle这个解决方案中的参数,因为它只接受正(向右旋转)和负(向左旋转)整数,它们只能是 90 度的倍数。一旦我们正确指定了这个参数,我们就可以构建 API,如下面的示例所示:

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication(“Apikey”);
Apikey.setApiKey(“YOUR API KEY”);
// Uncomment the following line to set a prefix for the API key, e.g. “Token” (defaults to null)
//Apikey.setApiKeyPrefix(“Token”);

EditPdfApi apiInstance = new EditPdfApi();
File inputFile = new File(“/path/to/inputfile”); // File | Input file to perform the operation on.
Integer rotationAngle = 56; // Integer | The angle to rotate the page in degrees, must be a multiple of 90 degrees, e.g. 90, 180, 270, or -90, -180, -270, etc.
try {
byte[] result = apiInstance.editPdfRotateAllPages(inputFile, rotationAngle);
System.out.println(result);
} catch (ApiException e) {
System.err.println(“Exception when calling EditPdfApi#editPdfRotateAllPages”);
e.printStackTrace();
}

旋转 PDF 文档中的范围、页面子集
当 PDF 文档中只有特定页面子集需要旋转调整时,无需担心:我们可以轻松执行先前概述的操作的更具体版本。我们只需要再次配置旋转角度,以及指定操作应该从哪里开始和结束的两个参数:

rotationAngle:定义指定页面将旋转多少度;接受正(向右旋转)和负(向左旋转)整数,它们是 90 度的倍数
pageStart:指定子集中的哪一页(从 1 开始),并包含该页
pageEnd:指定要在子集中的哪个页面(基于 1)完成,并包含此页面
此 API 调用的结构应如下所示:

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication(“Apikey”);
Apikey.setApiKey(“YOUR API KEY”);
// Uncomment the following line to set a prefix for the API key, e.g. “Token” (defaults to null)
//Apikey.setApiKeyPrefix(“Token”);

EditPdfApi apiInstance = new EditPdfApi();
File inputFile = new File(“/path/to/inputfile”); // File | Input file to perform the operation on.
Integer rotationAngle = 56; // Integer | The angle to rotate the page in degrees, must be a multiple of 90 degrees, e.g. 90, 180, 270, or -90, -180, -270, etc.
Integer pageStart = 56; // Integer | Page number (1 based) to start rotating pages from (inclusive).
Integer pageEnd = 56; // Integer | Page number (1 based) to stop rotating pages from (inclusive).
try {
byte[] result = apiInstance.editPdfRotatePageRange(inputFile, rotationAngle, pageStart, pageEnd);
System.out.println(result);
} catch (ApiException e) {
System.err.println(“Exception when calling EditPdfApi#editPdfRotatePageRange”);
e.printStackTrace();
}

使用上述示例构建 API 调用后,一切就绪:不再需要代码。每个 API 响应都会为新的 PDF 文件生成一个编码字符串,可用于根据需要生成新文件或更新现有文档。

文章来源:智云一二三科技

文章标题:如何使用 Java 在 PDF 文档中添加、删除或旋转页面

文章地址:https://www.zhihuclub.com/178186.shtml

关于作者: 智云科技

热门文章

网站地图