如何在 C# 中設置和編輯 PDF 元數據

如何在 C# 中使用 IronPDF 設定和編輯 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 處理能力。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 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 屬性以修改可用的 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

當我使用 Metadata Dictionary 時會發生什麼事?

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

在不同的情境中使用元資料。

在開發企業應用程式時,元資料在文件分類和檢索中扮演重要角色。 以下是實施全面元資料管理系統的範例:

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

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

<! -- 輸出顯示 IronPdf 中新增、編輯及移除自訂元資料的範例結果 --> --> <!--說明:顯示程式碼執行輸出或結果的截圖 -->

除了 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 屬性存取,或使用 CustomProperties 屬性中的 Remove 方法。 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.Documentation:維護系統中使用的自訂元資料欄位的登錄表 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 物件,其屬性包括標題、作者、主題、關鍵字、創造者和日期。IronPDF 可直接以程式化的方式更新這些標準的 metadata 欄位。

除了標準屬性之外,我還可以為 PDF 新增自訂的元資料欄位嗎?

是的,IronPDF 支持通过元数据字典自定义元数据字段。使用 SetMetaDataDictionary 方法,您可以添加標準欄位以外的任何鍵值對。如果某個 key 與一般的 metadata 欄位不符,IronPDF 會自動將其視為自訂 metadata。

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

IronPDF 提供 GetMetaDataDictionary 方法來擷取 PDF 文件中所有現有的元資料。此方法會傳回一個包含標準與自訂元資料欄位的辭典,讓您可以存取與處理儲存在文件中的元資料資訊。

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

IronPDF 支援所有標準的 PDF 元資料欄位,包括標題(Title)、作者(Author)、主題(Subject)、關鍵字(Keywords)、創作者(Creator)、製作者(Producer)、創建日期(CreationDate)和修改日期(ModifiedDate)。這些欄位可透過 PdfMetaData 物件來設定,對於文件的組織與可搜尋性而言是不可或缺的。

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

使用 IronPDF 設定元資料後,您可以在任何 PDF 閱讀器中存取文件屬性來檢視。在大多數的 PDF 閱讀器中,點擊三個垂直的圓點,或進入檔案功能表,選擇「文件屬性」,即可查看所有的元資料欄位,包括以程式方式設定的欄位。

元資料處理是否可與其他 PDF 作業結合?

絕對可以!IronPDF 允許您結合元資料編輯與其他功能,例如 HTML 到 PDF 的轉換、PDF 表單的建立以及數位簽名。這使其成為建立綜合文件管理系統或合規性功能的理想選擇。

Jordi Bardia
軟體工程師
Jordi 最精通的是 Python、C# 和 C++,當他在 Iron Software 沒有發揮他的技能時;他在進行遊戲程式設計。Jordi 分擔產品測試、產品開發和研究的責任,為產品的持續改善增添無限價值。多樣化的經驗讓他不斷接受挑戰並投入其中,他說這是他在 Iron Software 工作最喜歡的方面之一。Jordi 在佛羅里達州邁阿密長大,在佛羅里達大學主修電腦科學和統計學。
準備好開始了嗎?
Nuget 下載 17,386,124 | 版本: 2026.2 剛剛發布