Get PDF file information
Contents
[
Hide
]
In order to get file specific information of a PDF file, you need to create an object of PdfFileInfo class. After that, you can get values of the individual properties like Subject, Title, Keywords and Creator etc.
The following code snippet shows you how to get PDF file information.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetPdfInfo()
{
// Define the directory for input files
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var fileInfo = new Aspose.Pdf.Facades.PdfFileInfo(dataDir + "sample.pdf"))
{
// Get and display PDF information
Console.WriteLine("Subject: {0}", fileInfo.Subject);
Console.WriteLine("Title: {0}", fileInfo.Title);
Console.WriteLine("Keywords: {0}", fileInfo.Keywords);
Console.WriteLine("Creator: {0}", fileInfo.Creator);
Console.WriteLine("Creation Date: {0}", fileInfo.CreationDate);
Console.WriteLine("Modification Date: {0}", fileInfo.ModDate);
// Check if the file is a valid PDF and if it is encrypted
Console.WriteLine("Is Valid PDF: {0}", fileInfo.IsPdfFile);
Console.WriteLine("Is Encrypted: {0}", fileInfo.IsEncrypted);
// Get dimensions of the first page
Console.WriteLine("Page width: {0}", fileInfo.GetPageWidth(1));
Console.WriteLine("Page height: {0}", fileInfo.GetPageHeight(1));
}
}
Get Meta Info
In order to get information, we use the Header property. With ‘Hashtable’ we get all the possible values.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetMetaInfo()
{
// Define the directory for input files
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Create an instance of PdfFileInfo object
using (var fileInfo = new Aspose.Pdf.Facades.PdfFileInfo(dataDir + "SetMetaInfo_out.pdf"))
{
// Retrieve all existing custom attributes
var hashTable = new System.Collections.Hashtable(fileInfo.Header);
// Enumerate and display all custom attributes
var enumerator = hashTable.GetEnumerator();
while (enumerator.MoveNext())
{
string output = $"{enumerator.Key} {enumerator.Value}";
Console.WriteLine(output);
}
// Retrieve and display a specific custom attribute
Console.WriteLine("Reviewer: " + fileInfo.GetMetaInfo("Reviewer"));
}
}