Passwords, Security & Metadata
IronPDF allows the ability to encrypt, decrypt, modify metadata, and set certain permissions regarding annotations, copying and pasting content, form fields, and printing.
To open a password-protected PDF document, use the open
method and provide the password as the second parameter. Setting metadata requires creating a new map and inputting key-value pairs corresponding to the metadata information. Pass the new metadata map into the overrideMetadata
method to override the old metadata.
using IronPdf;
using System.Collections.Generic;
class PDFExample
{
static void Main()
{
// Assuming pdfPath is the path to your PDF document
string pdfPath = "path/to/your/document.pdf";
string password = "yourPassword";
// Open a password-protected PDF
PdfDocument pdf = PdfDocument.Load(pdfPath, password);
// Create new metadata to override existing metadata
Dictionary<string, string> newMetadata = new Dictionary<string, string>
{
{ "Title", "New Title" },
{ "Author", "Author Name" },
{ "Subject", "Subject Content" }
};
// Override old metadata with new metadata
pdf.OverrideMetadata(newMetadata);
}
}
using IronPdf;
using System.Collections.Generic;
class PDFExample
{
static void Main()
{
// Assuming pdfPath is the path to your PDF document
string pdfPath = "path/to/your/document.pdf";
string password = "yourPassword";
// Open a password-protected PDF
PdfDocument pdf = PdfDocument.Load(pdfPath, password);
// Create new metadata to override existing metadata
Dictionary<string, string> newMetadata = new Dictionary<string, string>
{
{ "Title", "New Title" },
{ "Author", "Author Name" },
{ "Subject", "Subject Content" }
};
// Override old metadata with new metadata
pdf.OverrideMetadata(newMetadata);
}
}
Imports IronPdf
Imports System.Collections.Generic
Friend Class PDFExample
Shared Sub Main()
' Assuming pdfPath is the path to your PDF document
Dim pdfPath As String = "path/to/your/document.pdf"
Dim password As String = "yourPassword"
' Open a password-protected PDF
Dim pdf As PdfDocument = PdfDocument.Load(pdfPath, password)
' Create new metadata to override existing metadata
Dim newMetadata As New Dictionary(Of String, String) From {
{"Title", "New Title"},
{"Author", "Author Name"},
{"Subject", "Subject Content"}
}
' Override old metadata with new metadata
pdf.OverrideMetadata(newMetadata)
End Sub
End Class
The removePasswordsAndEncryption
method is used to remove any passwords from the document. The makePdfDocumentReadOnly
method will make this PDF document read-only, encrypting content at 128 bits and disallowing copy and paste of content, annotations, and form editing.
class PDFSecurity
{
static void MakeReadOnly(string pdfPath, string outputPdfPath)
{
// Load the PDF document
PdfDocument pdf = PdfDocument.Load(pdfPath);
// Remove passwords and encryption
pdf.RemovePasswordsAndEncryption();
// Make the PDF document read-only
pdf.MakePdfDocumentReadOnly();
// Save the changes to a new PDF
pdf.SaveAs(outputPdfPath);
}
}
class PDFSecurity
{
static void MakeReadOnly(string pdfPath, string outputPdfPath)
{
// Load the PDF document
PdfDocument pdf = PdfDocument.Load(pdfPath);
// Remove passwords and encryption
pdf.RemovePasswordsAndEncryption();
// Make the PDF document read-only
pdf.MakePdfDocumentReadOnly();
// Save the changes to a new PDF
pdf.SaveAs(outputPdfPath);
}
}
Friend Class PDFSecurity
Private Shared Sub MakeReadOnly(ByVal pdfPath As String, ByVal outputPdfPath As String)
' Load the PDF document
Dim pdf As PdfDocument = PdfDocument.Load(pdfPath)
' Remove passwords and encryption
pdf.RemovePasswordsAndEncryption()
' Make the PDF document read-only
pdf.MakePdfDocumentReadOnly()
' Save the changes to a new PDF
pdf.SaveAs(outputPdfPath)
End Sub
End Class
Configure permissions by creating a permission object with permission settings and pass it to the setPermission
method.
class PDFPermissions
{
static void SetPermissions(string pdfPath, string outputPdfPath)
{
// Load PDF
PdfDocument pdf = PdfDocument.Load(pdfPath);
// Configure permissions
PdfPermissions permissions = new PdfPermissions
{
AllowCopyContent = false,
AllowEditAnnotations = false,
AllowEditContent = false,
AllowPrint = false
};
// Set the permission settings for the PDF
pdf.SetPermissions(permissions);
// Save the permission-changed document
pdf.SaveAs(outputPdfPath);
}
}
class PDFPermissions
{
static void SetPermissions(string pdfPath, string outputPdfPath)
{
// Load PDF
PdfDocument pdf = PdfDocument.Load(pdfPath);
// Configure permissions
PdfPermissions permissions = new PdfPermissions
{
AllowCopyContent = false,
AllowEditAnnotations = false,
AllowEditContent = false,
AllowPrint = false
};
// Set the permission settings for the PDF
pdf.SetPermissions(permissions);
// Save the permission-changed document
pdf.SaveAs(outputPdfPath);
}
}
Friend Class PDFPermissions
Private Shared Sub SetPermissions(ByVal pdfPath As String, ByVal outputPdfPath As String)
' Load PDF
Dim pdf As PdfDocument = PdfDocument.Load(pdfPath)
' Configure permissions
Dim permissions As New PdfPermissions With {
.AllowCopyContent = False,
.AllowEditAnnotations = False,
.AllowEditContent = False,
.AllowPrint = False
}
' Set the permission settings for the PDF
pdf.SetPermissions(permissions)
' Save the permission-changed document
pdf.SaveAs(outputPdfPath)
End Sub
End Class
Lastly, the saveAs
method can facilitate an object with ownerPassword
and userPassword
properties to be set respectively.
class PDFSaveWithPassword
{
static void SaveWithPassword(string pdfPath, string outputPdfPath, string ownerPassword, string userPassword)
{
// Load PDF
PdfDocument pdf = PdfDocument.Load(pdfPath);
// Save the PDF with owner and user passwords specified
pdf.SaveAs(outputPdfPath, ownerPassword, userPassword);
}
}
class PDFSaveWithPassword
{
static void SaveWithPassword(string pdfPath, string outputPdfPath, string ownerPassword, string userPassword)
{
// Load PDF
PdfDocument pdf = PdfDocument.Load(pdfPath);
// Save the PDF with owner and user passwords specified
pdf.SaveAs(outputPdfPath, ownerPassword, userPassword);
}
}
Friend Class PDFSaveWithPassword
Private Shared Sub SaveWithPassword(ByVal pdfPath As String, ByVal outputPdfPath As String, ByVal ownerPassword As String, ByVal userPassword As String)
' Load PDF
Dim pdf As PdfDocument = PdfDocument.Load(pdfPath)
' Save the PDF with owner and user passwords specified
pdf.SaveAs(outputPdfPath, ownerPassword, userPassword)
End Sub
End Class
For more detailed information about the capabilities and uses of IronPDF, visit the IronPDF product page or explore more about Iron Software's other products on their website.