如何使用C#设置和编辑PDF元数据

如何使用 IronPDF 在 C# 中设置和编辑 PDF 元数据

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 使开发人员能够在 C# 应用程序中以编程方式设置和编辑 PDF 元数据,包括标题、作者和关键字等标准属性,以及用于增强文档组织和可搜索性的自定义元数据字段。 无论您是在构建需要文档跟踪的业务应用程序、实施合规性功能,还是在组织 PDF 库,IronPDF 都能提供全面的元数据操作功能。 该功能可与 IronPDF 的 HTML 至 PDF 转换和其他文档处理功能无缝集成。

快速入门:立即修改 PDF 元数据

using IronPDF 管理 PDF 元数据,只需几行代码。 加载 PDF,更新标题、作者或关键词等元数据,然后保存更改。 本指南简化了设置和编辑元数据的过程,确保您的文档结构良好且可搜索。 通过遵循这种简单的方法增强您的 PDF 能力。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 复制并运行这段代码。

    IronPdf.PdfDocument.FromFile("example.pdf")
        .MetaData = new IronPdf.PdfMetaData { 
            Title="MyDoc", Author="Me", Subject="Demo", Keywords="ironpdf,metadata", Creator="MyApp", Producer="IronPDF", CreationDate=DateTime.Today, ModifiedDate=DateTime.Now 
        }
        .SaveAs("updated_example.pdf");
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPDF

    arrow pointer


如何设置和编辑 PDF 元数据?

using IronPDF 时,设置和编辑 PDF 中的通用元数据字段非常简单。 访问MetaData属性可修改可用的元数据字段。 在使用 PDF表单数字签名 或实施文档管理系统时,该功能尤其有用。

:path=/static-assets/pdf/content-code-examples/how-to/metadata-set-edit.cs
using IronPdf;
using System;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>");

// Access the MetaData class and set the pre-defined metadata properties.
pdf.MetaData.Author = "Iron Software";
pdf.MetaData.CreationDate = DateTime.Today;
pdf.MetaData.Creator = "IronPDF";
pdf.MetaData.Keywords = "ironsoftware,ironpdf,pdf";
pdf.MetaData.ModifiedDate = DateTime.Now;
pdf.MetaData.Producer = "IronPDF";
pdf.MetaData.Subject = "Metadata Tutorial";
pdf.MetaData.Title = "IronPDF Metadata Tutorial";

pdf.SaveAs("pdf-with-metadata.pdf");
Imports IronPdf
Imports System

Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>")

' Access the MetaData class and set the pre-defined metadata properties.
pdf.MetaData.Author = "Iron Software"
pdf.MetaData.CreationDate = DateTime.Today
pdf.MetaData.Creator = "IronPDF"
pdf.MetaData.Keywords = "ironsoftware,ironpdf,pdf"
pdf.MetaData.ModifiedDate = DateTime.Now
pdf.MetaData.Producer = "IronPDF"
pdf.MetaData.Subject = "Metadata Tutorial"
pdf.MetaData.Title = "IronPDF Metadata Tutorial"

pdf.SaveAs("pdf-with-metadata.pdf")
$vbLabelText   $csharpLabel

如何查看输出 PDF 中的元数据?

要查看文档元数据,请单击三个竖点并访问文档属性。 这些元数据对于文档组织至关重要,尤其是在实施 PDF/A 兼容文档以进行长期存档时。

如何设置和检索元数据字典?

GetMetaDataDictionary方法检索现有的元数据字典并访问存储在文档中的元数据信息。 SetMetaDataDictionary方法提供了重写元数据字典的有效方法。 如果通用元数据字段中没有关键字,则该关键字将成为自定义元数据属性。 这种方法在处理 合并多个 PDF 和整合不同来源的元数据时特别有用。

:path=/static-assets/pdf/content-code-examples/how-to/metadata-set-and-get-metadata-dictionary.cs
using IronPdf;
using System.Collections.Generic;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>");

Dictionary<string, string> newMetadata = new Dictionary<string, string>();
newMetadata.Add("Title", "How to article");
newMetadata.Add("Author", "IronPDF");

// Set metadata dictionary
pdf.MetaData.SetMetaDataDictionary(newMetadata);

// Retreive metadata dictionary
Dictionary<string, string> metadataProperties = pdf.MetaData.GetMetaDataDictionary();
Imports IronPdf
Imports System.Collections.Generic

Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>")

Dim newMetadata As New Dictionary(Of String, String)()
newMetadata.Add("Title", "How to article")
newMetadata.Add("Author", "IronPDF")

' Set metadata dictionary
pdf.MetaData.SetMetaDataDictionary(newMetadata)

' Retrieve metadata dictionary
Dim metadataProperties As Dictionary(Of String, String) = pdf.MetaData.GetMetaDataDictionary()
$vbLabelText   $csharpLabel

使用元数据词典时会发生什么?

要查看文档元数据,请单击三个竖点并访问文档属性。 元数据字典方法在实施批处理工作流程或在多个文档中实现元数据标准化时特别有用。 对于高级文档管理场景,可考虑结合PDF压缩技术来优化文件存储。

在不同的上下文中使用元数据

在开发企业应用程序时,元数据在文档分类和检索中起着至关重要的作用。 下面是一个实施综合元数据管理系统的示例:

using IronPdf;
using System;
using System.Collections.Generic;

public class PDFMetadataManager
{
    public static void ProcessBatchMetadata(List<string> pdfPaths)
    {
        foreach (string path in pdfPaths)
        {
            var pdf = PdfDocument.FromFile(path);

            // Standardize metadata across all documents
            pdf.MetaData.Producer = "Company Document System v2.0";
            pdf.MetaData.Creator = "IronPDF";

            // Add processing timestamp
            pdf.MetaData.ModifiedDate = DateTime.Now;

            // Add document classification
            var metadata = pdf.MetaData.GetMetaDataDictionary();
            metadata["DocumentType"] = "Internal Report";
            metadata["Department"] = "Finance";
            metadata["SecurityLevel"] = "Confidential";

            pdf.MetaData.SetMetaDataDictionary(metadata);
            pdf.SaveAs(path.Replace(".pdf", "_processed.pdf"));
        }
    }
}
using IronPdf;
using System;
using System.Collections.Generic;

public class PDFMetadataManager
{
    public static void ProcessBatchMetadata(List<string> pdfPaths)
    {
        foreach (string path in pdfPaths)
        {
            var pdf = PdfDocument.FromFile(path);

            // Standardize metadata across all documents
            pdf.MetaData.Producer = "Company Document System v2.0";
            pdf.MetaData.Creator = "IronPDF";

            // Add processing timestamp
            pdf.MetaData.ModifiedDate = DateTime.Now;

            // Add document classification
            var metadata = pdf.MetaData.GetMetaDataDictionary();
            metadata["DocumentType"] = "Internal Report";
            metadata["Department"] = "Finance";
            metadata["SecurityLevel"] = "Confidential";

            pdf.MetaData.SetMetaDataDictionary(metadata);
            pdf.SaveAs(path.Replace(".pdf", "_processed.pdf"));
        }
    }
}
Imports IronPdf
Imports System
Imports System.Collections.Generic

Public Class PDFMetadataManager
    Public Shared Sub ProcessBatchMetadata(pdfPaths As List(Of String))
        For Each path As String In pdfPaths
            Dim pdf = PdfDocument.FromFile(path)

            ' Standardize metadata across all documents
            pdf.MetaData.Producer = "Company Document System v2.0"
            pdf.MetaData.Creator = "IronPDF"

            ' Add processing timestamp
            pdf.MetaData.ModifiedDate = DateTime.Now

            ' Add document classification
            Dim metadata = pdf.MetaData.GetMetaDataDictionary()
            metadata("DocumentType") = "Internal Report"
            metadata("Department") = "Finance"
            metadata("SecurityLevel") = "Confidential"

            pdf.MetaData.SetMetaDataDictionary(metadata)
            pdf.SaveAs(path.Replace(".pdf", "_processed.pdf"))
        Next
    End Sub
End Class
$vbLabelText   $csharpLabel
Icon Quote related to 在不同的上下文中使用元数据

我最喜欢的这种库是 IronPDF。它允许快速高效地操作 PDF 文件。它还具有许多有价值的功能,比如导出为 PDF/A 格式和数字签名 PDF 文档。

Milan Jovanovic related to 在不同的上下文中使用元数据

Milan Jovanovic

微软MVP

查看案例研究
Icon Quote related to 在不同的上下文中使用元数据

IronOCR 意味着我们每年可以节省 $40,000 的人工处理成本,同时提高生产力,并释放资源用于高影响任务。我强烈推荐它。

Brent Matzelle related to 在不同的上下文中使用元数据

Brent Matzelle

首席技术官,OPYN

查看案例研究
Icon Quote related to 在不同的上下文中使用元数据

Iron Suite 在我们的运营中起着至关重要的作用。这些工具提高了业务各方面的效率,包括创建平面图和改善库存管理。

David Jones related to 在不同的上下文中使用元数据

David Jones

首席软件工程师,Agorus Build

查看案例研究

如何添加、编辑和删除自定义元数据?

除了 PDF 文档的标准元数据外,您还可以加入自定义元数据属性。 这些自定义属性在 PDF 阅读器软件中通常是不可见的,因为它们通常只显示通用元数据,可能无法检索所有现有的元数据属性。 在实施 DF 安全功能或创建专门的文档工作流程时,自定义元数据尤其有价值。

如何添加和编辑自定义元数据属性?

要添加自定义元数据,访问Add方法。 编辑自定义元数据需要将键值传递给CustomProperties属性并重新分配其值。 该功能可与 PDF 表单编辑场景很好地集成,在这些场景中,您可能需要跟踪表单提交元数据。

:path=/static-assets/pdf/content-code-examples/how-to/metadata-custom-properties.cs
using IronPdf;
using IronPdf.MetaData;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>");

PdfCustomMetadataProperties customProperties = pdf.MetaData.CustomProperties;

// Add custom property
customProperties.Add("foo", "bar"); // Key: foo, Value: bar

// Edit custom property
customProperties["foo"] = "baz";
Imports IronPdf
Imports IronPdf.MetaData

Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>")

Dim customProperties As PdfCustomMetadataProperties = pdf.MetaData.CustomProperties

' Add custom property
customProperties.Add("foo", "bar") ' Key: foo, Value: bar

' Edit custom property
customProperties("foo") = "baz"
$vbLabelText   $csharpLabel

高级自定义元数据场景

在构建文档管理系统时,自定义元数据变得尤为强大。 下面是一个展示实际使用情况的综合示例:

using IronPdf;
using System;
using System.Linq;

public class DocumentTrackingSystem
{
    public static void AddTrackingMetadata(PdfDocument pdf, string userId, string projectId)
    {
        var customProps = pdf.MetaData.CustomProperties;

        // Add tracking information
        customProps.Add("ProcessedBy", userId);
        customProps.Add("ProcessedDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        customProps.Add("ProjectID", projectId);
        customProps.Add("DocumentVersion", "1.0");
        customProps.Add("ReviewStatus", "Pending");

        // Add workflow metadata
        customProps.Add("WorkflowStep", "Initial Review");
        customProps.Add("NextReviewer", "John.Doe@company.com");
        customProps.Add("DueDate", DateTime.Now.AddDays(7).ToString("yyyy-MM-dd"));

        // Add compliance metadata
        customProps.Add("ComplianceChecked", "false");
        customProps.Add("RetentionPeriod", "7 years");
        customProps.Add("Classification", "Internal Use Only");
    }

    public static void UpdateReviewStatus(PdfDocument pdf, string status, string reviewer)
    {
        var customProps = pdf.MetaData.CustomProperties;

        // Update existing properties
        customProps["ReviewStatus"] = status;
        customProps["LastReviewedBy"] = reviewer;
        customProps["LastReviewDate"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        // Increment version if approved
        if (status == "Approved" && customProps.ContainsKey("DocumentVersion"))
        {
            var currentVersion = customProps["DocumentVersion"];
            var versionParts = currentVersion.Split('.');
            var minorVersion = int.Parse(versionParts[1]) + 1;
            customProps["DocumentVersion"] = $"{versionParts[0]}.{minorVersion}";
        }
    }
}
using IronPdf;
using System;
using System.Linq;

public class DocumentTrackingSystem
{
    public static void AddTrackingMetadata(PdfDocument pdf, string userId, string projectId)
    {
        var customProps = pdf.MetaData.CustomProperties;

        // Add tracking information
        customProps.Add("ProcessedBy", userId);
        customProps.Add("ProcessedDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        customProps.Add("ProjectID", projectId);
        customProps.Add("DocumentVersion", "1.0");
        customProps.Add("ReviewStatus", "Pending");

        // Add workflow metadata
        customProps.Add("WorkflowStep", "Initial Review");
        customProps.Add("NextReviewer", "John.Doe@company.com");
        customProps.Add("DueDate", DateTime.Now.AddDays(7).ToString("yyyy-MM-dd"));

        // Add compliance metadata
        customProps.Add("ComplianceChecked", "false");
        customProps.Add("RetentionPeriod", "7 years");
        customProps.Add("Classification", "Internal Use Only");
    }

    public static void UpdateReviewStatus(PdfDocument pdf, string status, string reviewer)
    {
        var customProps = pdf.MetaData.CustomProperties;

        // Update existing properties
        customProps["ReviewStatus"] = status;
        customProps["LastReviewedBy"] = reviewer;
        customProps["LastReviewDate"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        // Increment version if approved
        if (status == "Approved" && customProps.ContainsKey("DocumentVersion"))
        {
            var currentVersion = customProps["DocumentVersion"];
            var versionParts = currentVersion.Split('.');
            var minorVersion = int.Parse(versionParts[1]) + 1;
            customProps["DocumentVersion"] = $"{versionParts[0]}.{minorVersion}";
        }
    }
}
Imports IronPdf
Imports System
Imports System.Linq

Public Class DocumentTrackingSystem
    Public Shared Sub AddTrackingMetadata(pdf As PdfDocument, userId As String, projectId As String)
        Dim customProps = pdf.MetaData.CustomProperties

        ' Add tracking information
        customProps.Add("ProcessedBy", userId)
        customProps.Add("ProcessedDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
        customProps.Add("ProjectID", projectId)
        customProps.Add("DocumentVersion", "1.0")
        customProps.Add("ReviewStatus", "Pending")

        ' Add workflow metadata
        customProps.Add("WorkflowStep", "Initial Review")
        customProps.Add("NextReviewer", "John.Doe@company.com")
        customProps.Add("DueDate", DateTime.Now.AddDays(7).ToString("yyyy-MM-dd"))

        ' Add compliance metadata
        customProps.Add("ComplianceChecked", "false")
        customProps.Add("RetentionPeriod", "7 years")
        customProps.Add("Classification", "Internal Use Only")
    End Sub

    Public Shared Sub UpdateReviewStatus(pdf As PdfDocument, status As String, reviewer As String)
        Dim customProps = pdf.MetaData.CustomProperties

        ' Update existing properties
        customProps("ReviewStatus") = status
        customProps("LastReviewedBy") = reviewer
        customProps("LastReviewDate") = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

        ' Increment version if approved
        If status = "Approved" AndAlso customProps.ContainsKey("DocumentVersion") Then
            Dim currentVersion = customProps("DocumentVersion")
            Dim versionParts = currentVersion.Split("."c)
            Dim minorVersion = Integer.Parse(versionParts(1)) + 1
            customProps("DocumentVersion") = $"{versionParts(0)}.{minorVersion}"
        End If
    End Sub
End Class
$vbLabelText   $csharpLabel

如何删除自定义元数据属性?

从 PDF 文档中删除自定义元数据有两种方法。 使用通过Remove方法。 这在对用于公开发布的 PDF 进行消毒或准备文件存档时尤其有用。

:path=/static-assets/pdf/content-code-examples/how-to/metadata-remove-custom-properties.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>");

// Add custom property to be deleted
pdf.MetaData.CustomProperties.Add("willBeDeleted", "value");

// Remove custom property _ two ways
pdf.MetaData.RemoveMetaDataKey("willBeDeleted");
pdf.MetaData.CustomProperties.Remove("willBeDeleted");
Imports IronPdf

Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>")

' Add custom property to be deleted
pdf.MetaData.CustomProperties.Add("willBeDeleted", "value")

' Remove custom property _ two ways
pdf.MetaData.RemoveMetaDataKey("willBeDeleted")
pdf.MetaData.CustomProperties.Remove("willBeDeleted")
$vbLabelText   $csharpLabel

简介

PDF 元数据是文档管理系统的支柱,可实现高效的组织、可搜索性和合规性跟踪。 IronPDF 的元数据功能不仅限于简单的属性设置,还为构建复杂的文档工作流提供了一个全面的框架。 无论是实施PDF/UA 兼容文档以实现可访问性,还是创建自定义跟踪系统,元数据管理都是必不可少的。

元数据管理的最佳实践

  1. 一致性: 为自定义元数据键建立命名惯例
  2. 文档资料: 维护您的系统中使用的自定义元数据字段的注册表
  3. 验证: 在设置元数据值之前实施验证规则
  4. 安全: 除非PDF被加密,否则避免在元数据中存储敏感信息 5.合规性:确保元数据符合行业监管要求

通过利用 IronPDF 的元数据功能以及数字签名PDF 加密等功能,您可以构建满足企业要求的强大文档管理解决方案。

准备好看看您还能做些什么吗? 查看我们的教程页面:签名和保护PDFs

常见问题解答

如何在 C# 中设置标题和作者等 PDF 元数据属性?

using IronPDF,您可以通过访问 PDF 文档的 MetaData 属性轻松设置 PDF 元数据。只需分配一个新的 PdfMetaData 对象,其中包含标题、作者、主题、关键词、创建者和日期等属性。IronPDF 可直接以编程方式更新这些标准元数据字段。

除了标准属性外,我还能为 PDF 添加自定义元数据字段吗?

是的,IronPDF 通过元数据字典支持自定义元数据字段。使用 SetMetaDataDictionary 方法,您可以添加标准字段之外的任何键值对。如果键值与通用元数据字段不匹配,IronPDF 会自动将其视为自定义元数据。

如何从 PDF 文档中检索现有元数据?

IronPDF 提供了 GetMetaDataDictionary 方法,用于检索 PDF 文档中的所有现有元数据。该方法会返回一个包含标准元数据字段和自定义元数据字段的字典,允许您访问和处理存储在文档中的元数据信息。

有哪些标准元数据字段可供编辑?

IronPDF 支持所有标准的 PDF 元数据字段,包括标题、作者、主题、关键词、创建者、制作者、创建日期和修改日期。这些字段可通过 PdfMetaData 对象设置,对于文档的组织和可搜索性至关重要。

在 PDF 中设置元数据后如何查看?

using IronPDF 设置元数据后,您可以通过访问文档属性在任何 PDF 阅读器中查看。在大多数 PDF 阅读器中,点击三个竖点或转到文件菜单并选择文档属性,即可查看所有元数据字段,包括以编程方式设置的元数据字段。

元数据处理能否与其他 PDF 操作相结合?

当然可以!IronPDF 允许您将元数据编辑与 HTML 到 PDF 的转换、PDF 表单创建和数字签名等其他功能结合起来。这使其成为构建综合文档管理系统或合规功能的理想选择。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 19,936,792 | 版本: 2026.7 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPdf
运行示例看着你的HTML代码变成PDF文件。