管理多个文件在处理报告、发票或学术材料时可能会变得非常繁重。一个 PDF 合并工具可以将 PDF 和 PDF 文档合并为一个文件,从而减少杂乱,方便共享,并确保信息保持集中。

PDF 合并 API

使用 GroupDocs.Merger Cloud SDK for .NET,您可以轻松地以编程方式合并 PDF 文档。这个 SDK 为开发者提供了构建可扩展应用程序的能力,只需几行代码即可合并 PDFs。安装方法:

NuGet\Install-Package GroupDocs.Merger-Cloud -Version 23.10.0

接下来,通过以下 这个链接 获取您的 API 凭据(客户端 ID 和客户端密钥)。

在 C# 中合并 PDF 文档

这里是如何在您的 C# 应用程序中合并多个 PDF 文件:

  1. 创建 Configuration 类的实例,传入客户端凭据作为参数。
var configurations = new Configuration(clientId, clientSecret1);
  1. 初始化 DocumentApi 时传递 Configuration 对象作为参数。
var newApiInstance = new DocumentApi(configurationSettings);
  1. CDefine the source PDF files and pages to be merged using JoinItem.
var item1 = new JoinItem
  1. 准备一个 JoinRequestJoinOptions
var requestOutput = new JoinRequest(options);
  1. 调用 API 合并 PDF 文档并保存输出。
var response = newApiInstance.Join(requestOutput);
// 更多示例见 https://github.com/groupdocs-merger-cloud/groupdocs-merger-cloud-dotnet
// 获取您的 API 凭据 
string clientId = "b7efc309-156b-4496-9501-68197f85c25a";
string clientSecret1 = "985132b15703be48a4bdf897e6c05777";

// 创建 Configuration 类的实例,并用客户端 ID 和客户端密钥初始化它。 
var configurationSettings = new Configuration(clientId, clientSecret1);
// 创建 DocumentApi 的对象,同时将配置对象作为参数传递
var newApiInstance = new DocumentApi(configurationSettings);

try
{
    var item1 = new JoinItem
    {
        FileInfo = new FileInfo
        {
            FilePath = "Binder1.pdf"
        },
        // 将要合并的第一个 PDF 文件的页面
        Pages = new List<int?> { 2, 3 }
    };

    var item2 = new JoinItem
    {
        FileInfo = new FileInfo
        {
            FilePath = "ten-pages.pdf"
            },
        StartPageNumber = 2,
        EndPageNumber = 5,
        RangeMode = JoinItem.RangeModeEnum.OddPages
    };

    var options = new JoinOptions
    {
        JoinItems = new List<JoinItem> { item1, item2 },
        OutputPath = "myResultant.pdf"
    };
    
    // 创建文件 JoinRequest
    var requestOutput = new JoinRequest(options);
    // 合并 PDF 文件并将结果 PDF 上传到云存储
    var response = newApiInstance.Join(requestOutput);
}catch(Exception ex)
{ Console.WriteLine(ex.StackTrace); }
合并 PDF 文件

图像:- 合并 PDF 文件的预览。

使用 cURL 合并 PDF 文件

您还可以使用 cURL 连接 PDF 文档。这非常适合自动化场景。此方法特别有利于自动化文档管理任务,因为它可以快速轻松地将多个 PDF 文件合并为一个文档。

  1. 生成 JWT 令牌:
curl -v "https://api.groupdocs.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=b7efc309-156b-4496-9501-68197f85c25a&client_secret=985132b15703be48a4bdf897e6c05777" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
  1. 合并特定的 PDF 页面:
curl -v "https://api.groupdocs.cloud/v1.0/merger/join" \
-X POST \
-H "accept: application/json" \
-H "authorization: Bearer {accessToken}" \
-H "Content-Type: application/json" \
-d "{  \"JoinItems\": [    {      \"FileInfo\": {        \"FilePath\": \"{sourceFile1}\",        \"StorageName\": \"internal\",      },      \"Pages\": [2,3],    },{      \"FileInfo\": {        \"FilePath\": \"{sourceFile2}\",        \"StorageName\": \"internal\",      },      \"StartPageNumber\": 2,      \"EndPageNumber\": 5    }  ],  \"OutputPath\": \"{resultantFile}\"}"

Replace:

  • sourceFile1 - 第一个 PDF 文件。
  • sourceFile2 - 第二个 PDF 文件。
  • resultantFile - 结果 PDF 文件。
  • accessToken - 上面生成的JWT访问令牌。

试用免费的 PDF 合并应用程序

体验我们的免费 PDF Merger 应用程序,无需安装任何软件即可在线合并 PDF 文件。

pdf merger

有用的链接

结论

使用 GroupDocs.Merger Cloud SDK for .NET 或 cURL 命令,您可以快速安全地将所有 PDF 合并为一个文档。该 SDK 非常适合需要在 C# 项目中集成的开发人员,而 cURL 则为脚本和自动化提供了轻量级的选项。无论您是想合并 PDF、连接 PDF 文档,还是探索 Adobe 合并 PDF 的替代方案,GroupDocs 都提供了满足您文档管理需求的灵活性。

相关文章

我们推荐访问以下链接以了解更多信息: