如何在 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. 使用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.

在不同情境下使用元數據

在開發企業應用程式時,元資料在文件分類和檢索中起著至關重要的作用。 以下是一個實施綜合元資料管理系統的範例:

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 文件中刪除自訂元資料。 使用可透過 Metadata 屬性存取的 RemoveMetaDataKey 方法,或使用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.文件:維護一個註冊表,用於記錄系統中使用的自訂元資料欄位。
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 下載 18,135,201 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 變成 PDF。