使用 C# REST API 下载 PDF 附件

本教程提供使用基于 .NET 的 SDK 读取信息和使用 C# REST API 下载 PDF 附件的指导。您将学习读取 PDF 文件中附件的详细信息并在需要时下载特定附件。它有一个示例代码来分析 PDF 文件附件和使用 C# REST API 查看 PDF 中的附件

先决条件

使用 C# REST 接口打开 PDF 附件的步骤

  1. 通过设置 API key 和 APP SID 来配置 PdfApi 对象,用于读取附件信息
  2. 将源PDF文件连同附件上传至云存储,以便阅读详细信息
  3. 调用 GetDocumentAttachments() 方法获取附件的信息
  4. 显示 PDF 文件附件数量和基本信息

这些步骤定义了使用基于 C# .NET 的 API 读取 Adobe PDF 附件信息 的过程。将源 PDF 文件连同附件一起加载到云存储。调用 PdfApi 类中的 GetDocumentAttachments() 方法并显示附件列表和数量。

使用 C# REST 接口查看 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 DownloadAttachments()
{
PdfApi pdfApi = new PdfApi("API Key", "APP SID");
String fileName = "Attachments.pdf";
try
{
// Read the source PDF file
FilesUploadResult result = pdfApi.UploadFile(fileName, new MemoryStream(File.ReadAllBytes(fileName)));
// Fetch the attachments information
AttachmentsResponse apiResponse = pdfApi.GetDocumentAttachments(fileName);
int totalAttachments = apiResponse.Attachments.List.Count;
if (apiResponse != null && apiResponse.Status.Equals("OK"))
{
Console.WriteLine($"There are {totalAttachments} attachments");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
}
}
}

此代码演示如何使用 C# REStTful 服务访问 PDF 文件中的附件。它提供了附件列表和信息,例如 Href、Rel、Type 和 Title。您可以使用 GetDownloadDocumentAttachmentByIndex() 方法下载目标附件流以供读取并保存在磁盘上。

本文教了我们访问 PDF 附件的过程。如果您想从 PDF 文件中读取文本,可以参考 使用 C# REST API 从 PDF 文档中提取文本 上的文章。

 简体中文