本文介绍如何 使用 C# REST API 删除 PDF 元数据。您将学习使用基于 .NET 的 Cloud SDK 自动 使用 C# 低代码 API 从 PDF 中删除元数据。还将共享处理 PDF 文件元数据的详细信息,例如插入和删除自定义文件属性。
先决条件
下载 Aspose.PDF Cloud SDK for Dotnet to delete custom metadata
使用上述 SDK 设置 C# 项目以擦除文件属性
使用 C# REST 接口清除 PDF 中元数据的步骤
- 通过设置 API 密钥和应用程序 SID 来实例化 PdfApi 对象以删除自定义属性
- 将目标 PDF 文件加载到具有自定义属性的 Document 对象中
- 调用 DeleteProperties() 方法删除自定义属性
- 如果需要,使用 pdfApi.GetDocumentProperties() 方法显示属性的精简列表
- 下载更新的 PDF 文件
上述步骤总结了使用 C# 低代码 API* 开发 *PDF 属性删除器的过程。调用UploadFile()方法上传目标PDF文件,调用DeleteProperties()方法删除自定义属性。请注意,您无法删除内置属性,但可以使用 pdfApi.PutSetProperty() 方法并将 Value 属性设置为 null。
使用 C# RESTful 服务剥离 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 DeleteMetadata() | |
{ | |
PdfApi pdfApi = new PdfApi("Api Key", "App Sid"); | |
string fileName = "input.pdf"; | |
try | |
{ | |
// Upload the PDF file for removing properties | |
FilesUploadResult result = pdfApi.UploadFile(fileName, new MemoryStream(File.ReadAllBytes(fileName))); | |
// Call the DeleteProperties() method | |
AsposeResponse apiResponse = pdfApi.DeleteProperties(fileName); | |
if (apiResponse.Status == "OK") | |
{ | |
Console.WriteLine("Custom Properties deleted successfully"); | |
// Download created pdf file | |
Stream storageRes = pdfApi.DownloadFile(fileName); | |
storageRes.Position = 0; | |
using (FileStream fileStream = new FileStream("Sample_out.pdf", FileMode.Create, FileAccess.Write)) | |
{ | |
storageRes.CopyTo(fileStream); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
} | |
} |
此代码展示了如何使用基于 C# .NET 的 API 清理 PDF 元数据。如果要添加新的自定义属性,可以使用 pdfApi.PutSetProperty() 方法检查具有该名称的属性是否可用,然后更新其值,否则将其添加为自定义属性。如果要显示 PDF 文件中的当前属性集,请使用 GetDocumentProperties() 方法并循环访问 API 响应中的列表。
本文教会了我们删除PDF文件中所有自定义属性的过程。如果您想要添加新的自定义属性或设置现有属性的值,请参阅有关 使用 C# REST API 更新 PDF 元数据 的文章。