using GroupDocs.Metadata;
using GroupDocs.Metadata.Common;
using GroupDocs.Metadata.Tagging;
using System.Text.RegularExpressions;

namespace ReadMetadataFromPDFUsingCSharp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Set License to avoid the limitations of Metadata library
            License lic = new License();
            lic.SetLicense(@"GroupDocs.Metadata.lic");

            // Pass absolute or relative path of document to Metadata's constructor
            using (Metadata metadata = new Metadata(@"input.pdf"))
            {
                if (metadata.FileFormat != FileFormat.Unknown && !metadata.GetDocumentInfo().IsEncrypted)
                {
                    Console.WriteLine();

                    // Fetch all metadata properties that fall into a particular category
                    var properties = metadata.FindProperties(p => p.Tags.Any(t => t.Category == Tags.Content));
                    Console.WriteLine("The metadata properties describing some characteristics of the file content: title, keywords, language, etc.");
                    foreach (var property in properties)
                    {
                        Console.WriteLine("{0} = {1}", property.Name, property.Value);
                    }

                    // Fetch all properties having a specific type and value
                    var year = DateTime.Today.Year;
                    properties = metadata.FindProperties(p => p.Value.Type == MetadataPropertyType.DateTime &&
                                                             p.Value.ToStruct(DateTime.MinValue).Year == year);
                    Console.WriteLine("All datetime properties with the year value equal to the current year");
                    foreach (var property in properties)
                    {
                        Console.WriteLine("{0} = {1}", property.Name, property.Value);
                    }

                    // Fetch all properties whose names match the specified regex
                    const string pattern = "^author|company|(.+date.*)$";
                    Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
                    properties = metadata.FindProperties(p => regex.IsMatch(p.Name));
                    Console.WriteLine("All properties whose names match the following regex: {0}", pattern);
                    foreach (var property in properties)
                    {
                        Console.WriteLine("{0} = {1}", property.Name, property.Value);
                    }
                }
            }
        }
    }
}