如何在 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 皆提供全面的元資料處理功能。 This functionality integrates seamlessly with IronPDF's HTML to PDF conversion and other document processing features.

快速入門:立即修改 PDF 元資料

只需幾行程式碼,即可使用 IronPDF 管理 PDF 元資料。 載入您的 PDF 檔案,更新標題、作者或關鍵字等元資料,並儲存變更。 本指南簡化了元資料的設定與編輯流程,確保您的文件井然有序且便於搜尋。 透過這個簡單的方法,提升您的 PDF 處理能力。

  1. using 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 元資料?

使用 IronPDF 時,設定和編輯 PDF 中的通用元資料欄位非常簡單。 請存取 MetaData 屬性以修改可用的元資料欄位。 This functionality is particularly useful when working with PDF forms, digital signatures, or implementing document management systems.

: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 中的元資料?

若要檢視文件元資料,請點擊三個垂直點,然後進入"文件屬性"。 This metadata is crucial for document organization, especially when implementing PDF/A compliant documents for long-term archival.

如何設定及取得元資料字典?

GetMetaDataDictionary 方法會檢索現有的元資料字典,並存取儲存於文件中的元資料資訊。 SetMetaDataDictionary 方法提供了一種重寫元數據字典的有效方式。 若某個鍵值未出現在通用元資料欄位中,則會成為自訂元資料屬性。 This approach is particularly useful when working with merging multiple PDFs and consolidating metadata from different sources.

: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

使用元數據字典時會發生什麼情況?

若要檢視文件元資料,請點擊三個垂直點,然後進入"文件屬性"。 在實作批次處理工作流程或跨多份文件標準化元資料時,元資料字典的方法特別有用。 For advanced document management scenarios, consider combining this with PDF compression techniques to optimize file storage.

在不同情境下處理元資料

在開發Enterprise級應用程式時,元資料對於文件的分類與檢索扮演著關鍵角色。 以下是一個實作全面性元資料管理系統的範例:

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

如何新增、編輯及移除自訂元資料?

除了 PDF 文件的標準元資料外,您還可以加入自訂的元資料屬性。 這些自訂屬性通常在 PDF 檢視軟體中無法顯示,因為此類軟體通常僅顯示通用元資料,且可能無法擷取所有現有的元資料屬性。 Custom metadata is particularly valuable when implementing PDF security features or creating specialized document workflows.

如何新增和編輯自訂元資料屬性?

若要新增自訂元資料,請存取 CustomProperties 屬性並呼叫 Add 方法。 編輯自訂元資料時,需將鍵值傳遞至 CustomProperties 屬性,並重新設定其值。 This functionality integrates well with PDF form editing scenarios where you might need to track form submission metadata.

: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 文件中移除自訂元資料。 請使用 RemoveMetaDataKey 方法(可透過 Metadata 屬性存取),或使用 Remove 方法(可透過 CustomProperties 屬性存取)。 This is particularly useful when sanitizing PDFs for public distribution or preparing documents for archival.

: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 的元資料功能不僅限於簡單的屬性設定——它提供了一個全面的框架,用於建立複雜的文件工作流程。 Whether implementing PDF/UA compliant documents for accessibility or creating custom tracking systems, metadata management is essential.

元資料管理的最佳實踐

  1. 一致性:為自訂元數據鍵建立命名規範
  2. 文件:建立系統中使用的自訂元資料欄位登錄檔
  3. 驗證:在設定元資料值之前,請先實施驗證規則
  4. 安全性:除非 PDF 已加密,否則請避免在元資料中儲存敏感資訊
  5. 合規性:確保元資料符合您所在產業的法規要求

By leveraging IronPDF's metadata capabilities alongside features like digital signatures and PDF encryption, you can build robust document management solutions that meet enterprise requirements.

準備好探索更多可能性了嗎? 請點此查看我們的教學頁面:簽署與保護 PDF 文件

常見問題

如何在 C# 中設定 PDF 元資料屬性(例如標題和作者)?

透過 IronPDF,您可以透過存取 PDF 文件的 MetaData 屬性,輕鬆設定 PDF 元資料。只需將新的 PdfMetaData 物件賦予 Title、Author、Subject、Keywords、Creator 及日期等屬性即可。IronPDF 讓您能透過程式設計方式輕鬆更新這些標準元資料欄位。

除了標準屬性之外,我能否在 PDF 中新增自訂元資料欄位?

是的,IronPDF 透過元資料字典支援自訂元資料欄位。使用 SetMetaDataDictionary 方法,您可以在標準欄位之外新增任何鍵值對。若某個鍵不符合作為通用元資料欄位,IronPDF 會自動將其視為自訂元資料。

如何從 PDF 文件中擷取現有的元資料?

IronPDF 提供 GetMetaDataDictionary 方法,用於從 PDF 文件中擷取所有現有元資料。此方法會傳回一個字典,其中包含標準與自訂的元資料欄位,讓您能夠存取並處理文件中儲存的元資料資訊。

有哪些標準的元資料欄位可供編輯?

IronPDF 支援所有標準的 PDF 元資料欄位,包括標題 (Title)、作者 (Author)、主題 (Subject)、關鍵字 (Keywords)、建立者 (Creator)、製作人 (Producer)、建立日期 (CreationDate) 及修改日期 (ModifiedDate)。這些欄位可透過 PdfMetaData 物件設定,對於文件的組織與檢索功能至關重要。

在 PDF 中設定元資料後,該如何檢視這些元資料?

using IronPDF 設定元資料後,您可在任何 PDF 閱讀器中透過檢視文件屬性來查看這些資料。在大多數 PDF 閱讀器中,點擊三個垂直點圖示,或前往「檔案」選單並選擇「文件屬性」,即可查看所有元資料欄位,包括透過程式設定的欄位。

元資料操作能否與其他 PDF 操作結合使用?

當然可以!IronPDF 讓您能將元資料編輯功能與其他功能結合使用,例如 HTML 轉 PDF、PDF 表單建立以及數位簽章。這使其成為建置全面性文件管理系統或合規功能的理想選擇。

Jordi Bardia
軟體工程師
Jordi 最擅長 Python、C# 和 C++;當他不在 Iron Software 發揮所長時,他會從事遊戲程式設計。Jordi 共同負責產品測試、產品開發與研究,為產品的持續改進帶來極大的價值。 豐富多樣的經驗讓他始終充滿挑戰與投入,他表示這正是他在 Iron Software 工作最喜愛的一點。Jordi 成長於佛羅里達州邁阿密,並於佛羅里達大學攻讀電腦科學與統計學。
準備開始了嗎?
Nuget 下載 18,926,724 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。