Get and remove the value of custom properties for PDF file in C#

With the help of Spire.PDF, we can easily set the custom properties for PDF files. Sometimes, we may need to get the custom properties and update some information on it. This article will show you how to get and remove the value of custom properties area for PDF file in C#.

Spire.PDF provides a DocumentInformation class and we can set PDF custom properties through setting properties of DocumentInformation.SetCustomerDefined. We can also get the custom properties via DocumentInformation.GetCustomerDefined and remove the custom properties via DocumentInformation.RemoveCustomerDefined.

Here comes to the code snippet of how to get and remove the value of custom properties for PDF file in C#. Firstly, view the value of custom properties area for the PDF file:

Get and remove the value of custom properties for PDF file in C#

Step 1: Create a new PDF document and load from the file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");

Step 2: Get the value at the custom properties area of PDF file.

//Get the value for Name at the custom property area of pdf document information
string name = doc.DocumentInformation.GetCustomerDefined("Name");
//Get the value for Number at the custom property area of pdf document information
string number = doc.DocumentInformation.GetCustomerDefined("Number");

Step 3: Remove the value for Name from the custom property area of PDF file.

doc.DocumentInformation.RemoveCustomerDefined("Name");

Step 4: Save the document to file and launch to preview it.

doc.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");

Effective screenshot after remove the Name value from the custom properties area for PDF file:

Get and remove the value of custom properties for PDF file in C#

Full codes:

static void Main(string[] args)

 {
     PdfDocument doc = new PdfDocument();
     doc.LoadFromFile("sample.pdf");

     string name = doc.DocumentInformation.GetCustomerDefined("Name");
     string number = doc.DocumentInformation.GetCustomerDefined("Number");

     doc.DocumentInformation.RemoveCustomerDefined("Name");
     doc.SaveToFile("result.pdf");
 }