本简短教程将指导您如何使用 C# REST API 拆分 PDF 文档。您将学习如何使用 .NET 云 SDK 通过 C# REST 接口在线拆分 PDF 文件。它将解释自定义 PDF 拆分过程所需的所有参数。
先决条件
使用上述 SDK 设置 C# 项目
使用 C# Low Code API 拆分 PDF 的步骤
- 使用客户端 ID 和密钥配置 PdfApi 类对象
- 将源 PDF 文件上传至云端并指定名称以供后续参考
- 调用 PostSplitDocument 方法按指定范围内的页面拆分 PDF 文件
- 遍历拆分的 PDF 文档集合并下载每个 PDF 文件
- 使用 API 分配的唯一名称将每个 PDF 文件保存为单独的文件
这些步骤总结了使用基于 C# .NET 的 API 开发 PDF 拆分器软件 的步骤。首先,将源 PDF 文件加载到云端以逐页拆分,然后调用 PdfApi 类中的 PostSplitDocument() 方法对其进行拆分。提供必要的参数,例如对上传的 PDF 文件的引用、文件格式、起始页码、结束页码以及一些可选参数(如果需要)。
使用 C# Low Code API 在线拆分 PDF 的代码
using System; | |
using System.IO; | |
using Aspose.Pdf.Cloud.Sdk.Api; | |
using Aspose.Pdf.Cloud.Sdk.Model; | |
using System.Collections.Generic; | |
namespace Aspose.PDF.Cloud.Examples.Kb | |
{ | |
public class PdfTasks | |
{ | |
public static void SplitPDFFiles() | |
{ | |
PdfApi pdfApi = new PdfApi("API KEY", "API SID");// For splitting the PDF | |
String fileName = "sample.pdf"; | |
String format = "pdf"; | |
int from = 1; | |
int to = 2; | |
String storage = ""; | |
String folder = ""; | |
try | |
{ | |
// Load the input PDF file into the cloud | |
pdfApi.UploadFile(fileName, new MemoryStream( System.IO.File.ReadAllBytes(fileName))); | |
// Split the PDF pages | |
SplitResultResponse apiResponse = pdfApi.PostSplitDocument(fileName, format, from, to, storage, folder); | |
if (apiResponse.Status.Equals("OK")) | |
{ | |
// Download created pdf file | |
foreach(var item in apiResponse.Result.Documents) | |
{ | |
Stream storageRes = pdfApi.DownloadFile(item.Href); | |
storageRes.Position = 0; | |
using (FileStream fileStream = new FileStream(item.Href, FileMode.Create, FileAccess.Write)) | |
{ | |
storageRes.CopyTo(fileStream); | |
} | |
} | |
Console.WriteLine("Split PDF Files, Done!"); | |
Console.ReadKey(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
} | |
} |
此代码演示了带有 C# REST API 的在线 PDF 拆分器软件的工作原理。API 返回一个 SplitResultResponse 对象,其中包含每个页面具有唯一名称的文档列表。您可以根据需要下载所有或选定的页面。
本文教我们如何使用 C# RESTful 服务在线拆分 PDF 文档。要将 PDF 文件合并为单个 PDF,请参阅以下文章:使用 C# REST API 合并 PDF。