Exemplo n.º 1
0
        /// <summary>
        /// Takes manufacturer as input and returns photos made on particular camera
        /// </summary>
        /// <param name="manufacturer">Camera manufacturer name</param>
        public void FilterByCameraManufacturer(string manufacturer)
        {
            // Map directory in source folder
            string sourceDirectoryPath = Common.MapSourceFilePath(this.PhotosDirectory);

            // get jpeg files
            string[] files = Directory.GetFiles(sourceDirectoryPath, "*.jpg");

            List <string> result = new List <string>();

            foreach (string path in files)
            {
                // recognize file
                FormatBase format = FormatFactory.RecognizeFormat(path);

                // casting to JpegFormat
                if (format is JpegFormat)
                {
                    JpegFormat jpeg = (JpegFormat)format;

                    // get exif data
                    JpegExifInfo exif = (JpegExifInfo)jpeg.GetExifInfo();

                    if (exif != null)
                    {
                        if (string.Compare(exif.Make, manufacturer, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            // add file path to list
                            result.Add(Path.GetFileName(path));
                        }
                    }
                }
            }
            Console.WriteLine(string.Join("\n", result));
        }
 /// <summary>
 /// Get DublinCore Metadata of supported file formats using IDublinCore Interface.
 /// Feature is supported in version 18.5 or greater of the API
 /// </summary>
 public static void GetDublinCoreMetadataUsingIDublinCore()
 {
     try
     {
         string[] files = Directory.GetFiles(XMPFilesDirectory);
         foreach (string file in files)
         {
             try
             {
                 using (FormatBase format = FormatFactory.RecognizeFormat(file))
                 {
                     IDublinCore dublinCoreContainer = format as IDublinCore;
                     if (dublinCoreContainer != null)
                     {
                         DublinCoreMetadata dublinCoreMetadata = dublinCoreContainer.GetDublinCore();
                         if (dublinCoreMetadata != null)
                         {
                             Console.WriteLine(dublinCoreMetadata.Creator);
                             Console.WriteLine(dublinCoreMetadata.Format);
                             Console.WriteLine(dublinCoreMetadata.Subject);
                         }
                     }
                 }
             }
             catch
             {
                 Console.WriteLine("Could not load {0}", file);
             }
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Exemplo n.º 3
0
        //ExEnd:ApplyLicense

        //ExStart:FormatRecognizer
        /// <summary>
        /// Gets directory name and recognizes format of files in that directory
        /// </summary>
        /// <param name="directorPath">Directory path</param>
        public static void GetFileFormats(string directorPath)
        {
            try
            {
                // path to the document
                directorPath = Common.MapSourceFilePath(directorPath);

                // get array of files inside directory
                string[] files = Directory.GetFiles(directorPath);

                foreach (string path in files)
                {
                    // recognize file by it's signature
                    FormatBase format = FormatFactory.RecognizeFormat(path);

                    if (format != null)
                    {
                        Console.WriteLine("File: {0}, type: {1}", Path.GetFileName(path), format.Type);
                    }
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
Exemplo n.º 4
0
        //ExEnd:ReadMetadataUsingKey

        //ExStart:EnumerateMetadata
        /// <summary>
        /// Reads metadata property by defined key for any supported format
        /// </summary>
        /// <param name="directorPath">Directory path</param>
        public static void EnumerateMetadata(string directoryPath)
        {
            try
            {
                // path to the files directory
                directoryPath = MapSourceFilePath(directoryPath);

                // get all files inside the directory
                string[] files = Directory.GetFiles(directoryPath);

                foreach (string file in files)
                {
                    FormatBase format;
                    try
                    {
                        // try to recognize file
                        format = FormatFactory.RecognizeFormat(file);
                    }
                    catch (InvalidFormatException)
                    {
                        // skip unsupported formats
                        continue;
                    }
                    catch (DocumentProtectedException)
                    {
                        // skip password protected documents
                        continue;
                    }

                    if (format == null)
                    {
                        // skip unsupported formats
                        continue;
                    }

                    // get all metadata presented in file
                    Metadata[] metadataArray = format.GetMetadata();

                    // go through metadata array
                    foreach (Metadata metadata in metadataArray)
                    {
                        // and display all metadata items
                        Console.WriteLine("Metadata type: {0}", metadata.MetadataType);

                        // use foreach statement for Metadata class to evaluate all metadata properties
                        foreach (MetadataProperty property in metadata)
                        {
                            Console.WriteLine(property);
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
        /// <summary>
        /// Takes author name and removes metadata in files created by specified author
        /// </summary>
        /// <param name="authorName">Author name</param>
        public void RemoveMetadataByAuthor(string authorName)
        {
            // Map directory in source folder
            string sourceDirectoryPath = Common.MapSourceFilePath(this.DocumentsPath);

            // get files presented in target directory
            string[] files = Directory.GetFiles(sourceDirectoryPath);

            foreach (string path in files)
            {
                // recognize format
                using (FormatBase format = FormatFactory.RecognizeFormat(path))
                {
                    // initialize DocFormat
                    DocFormat docFormat = format as DocFormat;
                    if (docFormat != null)
                    {
                        // get document properties
                        DocMetadata properties = docFormat.DocumentProperties;

                        // check if author is the same
                        if (string.Equals(properties.Author, authorName, StringComparison.OrdinalIgnoreCase))
                        {
                            // remove comments
                            docFormat.ClearComments();

                            List <string> customKeys = new List <string>();

                            // find all custom keys
                            foreach (KeyValuePair <string, PropertyValue> keyValuePair in properties)
                            {
                                if (!properties.IsBuiltIn(keyValuePair.Key))
                                {
                                    customKeys.Add(keyValuePair.Key);
                                }
                            }

                            // and remove all of them
                            foreach (string key in customKeys)
                            {
                                properties.Remove(key);
                            }
                            //====== yet to change things =========================
                            // and commit changes
                            string fileName       = Path.GetFileName(path);
                            string outputFilePath = Common.MapDestinationFilePath(this.DocumentsPath + "/" + fileName);
                            docFormat.Save(outputFilePath);
                            //=====================================================
                        }
                    }
                }
            }

            Console.WriteLine("Press any key to exit.");
        }
            //ExEnd:SourceDocFilePath

            /// <summary>
            /// Detects Zip format via Format Factory
            /// </summary>
            public static void DetectZipFormat()
            {
                //ExStart:DetectZipFormat
                // recognize format
                FormatBase format = FormatFactory.RecognizeFormat(Common.MapSourceFilePath(filePath));

                // check format type
                if (format.Type == DocumentType.Zip)
                {
                    Console.WriteLine("File: {0} has correct format", Path.GetFileName(Common.MapSourceFilePath(filePath)));
                }
                //ExEnd:DetectZipFormat
            }
Exemplo n.º 7
0
            /// <summary>
            /// Detects AVI video format via Format Factory
            /// </summary>
            public static void DetectAviFormat()
            {
                //ExStart:DetectAviFormat
                // recognize format
                FormatBase format = FormatFactory.RecognizeFormat(Common.MapSourceFilePath(filePath));

                // check format type
                if (format.Type == DocumentType.AVI)
                {
                    // cast it to AviFormat
                    AviFormat aviFormat = format as AviFormat;

                    // and get it MIME type
                    string mimeType = aviFormat.MIMEType;
                    Console.WriteLine(mimeType);
                }
                //ExEnd:DetectAviFormat
            }
Exemplo n.º 8
0
        /// <summary>
        /// Gets and returns MIME type in the file using MIMEType property in FormatBase class or it's children.
        /// </summary>
        /// <param name="path">File Path</param>
        public static void GetMimeTypeUsingFormatBaseApproach(string filePath)
        {
            try
            {
                //ExStart: MIMETypeDetectionUsingFormatBase
                // recognize format
                FormatBase format = FormatFactory.RecognizeFormat(Common.MapSourceFilePath(filePath));

                // and get MIME type
                string mimeType = format.MIMEType;
                Console.WriteLine("MIME type: {0}", mimeType);
                //ExEnd: MIMETypeDetectionUsingFormatBase
            }
            catch (Exception exp)
            {
                Console.WriteLine("Exception occurred: " + exp.Message);
            }
        }
            /// <summary>
            /// Detects Mov video format via Format Factory
            /// </summary>
            public static void DetectMovFormat()
            {
                //ExStart:DetectMovFormat
                // recognize format
                using (FormatBase format = FormatFactory.RecognizeFormat(Common.MapSourceFilePath(filePath)))
                {
                    // check format type
                    if (format.Type == DocumentType.Mov)
                    {
                        // cast it to MovFormat
                        MovFormat movFormat = format as MovFormat;

                        // and get it MIME type;
                        string mimeType = movFormat.MIMEType;
                        Console.WriteLine(mimeType);
                    }
                }
                //ExEnd:DetectMovFormat
            }
Exemplo n.º 10
0
        //ExEnd:FormatRecognizer


        //ExStart:ReadMetadataUsingKey
        /// <summary>
        /// Reads metadata property by defined key for any supported format
        /// </summary>
        /// <param name="directorPath">Directory path</param>
        public static void ReadMetadataUsingKey(string directoryPath)
        {
            try
            {
                // path to the files directory
                directoryPath = MapSourceFilePath(directoryPath);

                // get array of files inside directory
                string[] files = Directory.GetFiles(directoryPath);
                foreach (var file in files)
                {
                    // recognize first file
                    FormatBase format = FormatFactory.RecognizeFormat(file);

                    // try get EXIF artist
                    var exifArtist = format[MetadataKey.EXIF.Artist];
                    Console.WriteLine(exifArtist);

                    // try get dc:creator XMP value
                    var creator = format[MetadataKey.XMP.DublinCore.Creator];
                    Console.WriteLine(creator);

                    // try get xmp:creatorTool
                    var creatorTool = format[MetadataKey.XMP.BaseSchema.CreatorTool];
                    Console.WriteLine(creatorTool);

                    // try get IPTC Application Record keywords
                    var iptcKeywords = format[MetadataKey.IPTC.ApplicationRecord.Keywords];
                    Console.WriteLine(iptcKeywords);
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
        //ExEnd:ApplyLicense
        public static string CleanFile(string filePath)
        {
            try
            {
                try
                {
                    //Apply license...
                    ApplyLicense();
                }
                catch (Exception exp)
                {
                    MessageBox.Show("In Licence: " + exp.Message);
                }
                try
                {
                    //Recognize format of file...
                    FormatBase format = FormatFactory.RecognizeFormat(filePath);

                    if (format.Type.ToString().ToLower() == "doc" || format.Type.ToString().ToLower() == "docx")
                    {
                        // initialize DocFormat...
                        DocFormat docFormat = format as DocFormat;
                        if (docFormat != null)
                        {
                            // get document properties...
                            DocMetadata properties = new DocMetadata();
                            properties = docFormat.DocumentProperties;

                            //Remove custom properties...
                            foreach (KeyValuePair <string, PropertyValue> keyValuePair in properties)
                            {
                                if (!properties.IsBuiltIn(keyValuePair.Key))
                                {
                                    properties.Remove(keyValuePair.Key);
                                }
                            }

                            //Reset built-in properties...
                            properties.Author        = "";
                            properties.Category      = "";
                            properties.Comments      = "";
                            properties.Company       = "";
                            properties.ContentStatus = "";
                            properties.HyperlinkBase = "";
                            properties.Keywords      = "";
                            properties.Manager       = "";
                            properties.Title         = "";

                            //Update metadata if file...
                            MetadataUtility.UpdateMetadata(filePath, properties);
                        }
                        return("1");
                    }
                    else if (format.Type.ToString().ToLower() == "xls" || format.Type.ToString().ToLower() == "xlsx")
                    {
                        //Initialize XlsFormat...
                        XlsFormat xlsFormat = format as XlsFormat;
                        if (xlsFormat != null)
                        {
                            //Get document properties...
                            XlsMetadata properties = xlsFormat.DocumentProperties;

                            //Remove custom properties...
                            foreach (KeyValuePair <string, PropertyValue> keyValuePair in properties)
                            {
                                if (!properties.IsBuiltIn(keyValuePair.Key))
                                {
                                    properties.Remove(keyValuePair.Key);
                                }
                            }

                            //Reset built-in properties...
                            properties.Author        = "";
                            properties.Category      = "";
                            properties.Comments      = "";
                            properties.Company       = "";
                            properties.HyperlinkBase = "";
                            properties.Keywords      = "";
                            properties.Manager       = "";
                            properties.Title         = "";
                            properties.Subject       = "";

                            //Update metadata in files...
                            MetadataUtility.UpdateMetadata(filePath, properties);
                        }
                        return("1");
                    }
                    else if (format.Type.ToString().ToLower() == "ppt" || format.Type.ToString().ToLower() == "pptx")
                    {
                        //Initialize PptFormat...
                        PptFormat pptFormat = format as PptFormat;
                        if (pptFormat != null)
                        {
                            //Get document properties...
                            PptMetadata properties = pptFormat.DocumentProperties;

                            //Remove custom properties
                            foreach (KeyValuePair <string, PropertyValue> keyValuePair in properties)
                            {
                                if (!properties.IsBuiltIn(keyValuePair.Key))
                                {
                                    properties.Remove(keyValuePair.Key);
                                }
                            }

                            //Reset built-in properties...
                            properties.Author   = "";
                            properties.Category = "";
                            properties.Comments = "";
                            properties.Company  = "";
                            properties.Keywords = "";
                            properties.Manager  = "";
                            properties.Title    = "";
                            properties.Subject  = "";

                            //Update metadata of file...
                            MetadataUtility.UpdateMetadata(filePath, properties);
                        }
                        return("1");
                    }
                    else if (format.Type.ToString().ToLower() == "pdf")
                    {
                        // initialize PdfFormat...
                        PdfFormat pdfFormat = format as PdfFormat;
                        if (pdfFormat != null)
                        {
                            // get document properties...
                            PdfMetadata properties = pdfFormat.DocumentProperties;

                            // Remove custom properties...
                            foreach (KeyValuePair <string, PropertyValue> keyValuePair in properties)
                            {
                                if (!properties.IsBuiltIn(keyValuePair.Key))
                                {
                                    properties.Remove(keyValuePair.Key);
                                }
                            }

                            //Reset built-in properties...
                            properties.Author       = "";
                            properties.CreatedDate  = DateTime.MinValue;
                            properties.Keywords     = "";
                            properties.ModifiedDate = DateTime.MinValue;
                            properties.Subject      = "";
                            properties.TrappedFlag  = false;
                            properties.Title        = "";

                            //Update metadata of file...
                            MetadataUtility.UpdateMetadata(filePath, properties);
                        }
                        return("1");
                    }
                    else if (format.Type.ToString().ToLower() == "jpeg" || format.Type.ToString().ToLower() == "jpg")
                    {
                        //Get EXIF data if exists
                        ExifMetadata exifMetadata = (ExifMetadata)MetadataUtility.ExtractSpecificMetadata(filePath, MetadataType.EXIF);

                        //Get XMP data if exists
                        XmpMetadata xmpMetadata = (XmpMetadata)MetadataUtility.ExtractSpecificMetadata(filePath, MetadataType.XMP);
                        if (exifMetadata != null)
                        {
                            //Remove exif info...
                            ExifInfo exifInfo = exifMetadata.Data;

                            if (exifInfo.GPSData != null)
                            {
                                // set altitude, latitude and longitude to null values
                                exifInfo.GPSData.Altitude     = null;
                                exifInfo.GPSData.Latitude     = null;
                                exifInfo.GPSData.LatitudeRef  = null;
                                exifInfo.GPSData.Longitude    = null;
                                exifInfo.GPSData.LongitudeRef = null;
                            }
                            exifInfo.BodySerialNumber = "";
                            exifInfo.CameraOwnerName  = "";
                            exifInfo.CFAPattern       = new byte[] { 0 };
                        }
                        else
                        {
                            exifMetadata = new ExifMetadata();
                        }
                        try
                        {
                            //Remove XMP data...
                            XmpPacketWrapper xmpPacket = xmpMetadata.XmpPacket;
                            if (xmpPacket != null)
                            {
                                if (xmpPacket.ContainsPackage(Namespaces.DublinCore))
                                {
                                    // if not - add DublinCore schema
                                    xmpPacket.AddPackage(new DublinCorePackage());
                                    DublinCorePackage dublinCorePackage = (DublinCorePackage)xmpPacket.GetPackage(Namespaces.DublinCore);
                                    dublinCorePackage.Clear();
                                    xmpMetadata.XmpPacket = xmpPacket;
                                }
                            }
                        }
                        catch { }

                        //Update Exif info...
                        try
                        {
                            MetadataUtility.UpdateMetadata(filePath, exifMetadata);
                        }
                        catch { }

                        //Update XMP data...
                        try
                        {
                            MetadataUtility.UpdateMetadata(filePath, xmpMetadata);
                        }
                        catch { }

                        //Remove custom metadata if any...
                        MetadataUtility.CleanMetadata(filePath);

                        return("1");
                    }
                    else if (format.Type.ToString().ToLower() == "png")
                    {
                        //Get XMP data...
                        XmpMetadata xmpMetadata = (XmpMetadata)MetadataUtility.ExtractSpecificMetadata(filePath, MetadataType.XMP);
                        try
                        {
                            //Remove XMP metadata...
                            XmpPacketWrapper xmpPacket = xmpMetadata.XmpPacket;
                            if (xmpPacket != null)
                            {
                                if (xmpPacket.ContainsPackage(Namespaces.DublinCore))
                                {
                                    // if not - add DublinCore schema
                                    xmpPacket.AddPackage(new DublinCorePackage());
                                    DublinCorePackage dublinCorePackage = (DublinCorePackage)xmpPacket.GetPackage(Namespaces.DublinCore);
                                    dublinCorePackage.Clear();
                                    xmpMetadata.XmpPacket = xmpPacket;

                                    //Update XMP metadata in file...
                                    MetadataUtility.UpdateMetadata(filePath, xmpMetadata);

                                    //Clean custom metadata if any...
                                    MetadataUtility.CleanMetadata(filePath);
                                }
                            }
                        }
                        catch { }

                        return("1");
                    }
                    else if (format.Type.ToString().ToLower() == "gif")
                    {
                        //Initialie GifFormat...
                        GifFormat gifFormat = new GifFormat(filePath);

                        //Check if Xmp supported...
                        if (gifFormat.IsSupportedXmp)
                        {
                            //Get XMP data...
                            XmpMetadata xmpMetadata = (XmpMetadata)MetadataUtility.ExtractSpecificMetadata(filePath, MetadataType.XMP);
                            try
                            {
                                XmpPacketWrapper xmpPacket = xmpMetadata.XmpPacket;
                                if (xmpPacket != null)
                                {
                                    if (xmpPacket.ContainsPackage(Namespaces.DublinCore))
                                    {
                                        // if not - add DublinCore schema
                                        xmpPacket.AddPackage(new DublinCorePackage());
                                        DublinCorePackage dublinCorePackage = (DublinCorePackage)xmpPacket.GetPackage(Namespaces.DublinCore);
                                        dublinCorePackage.Clear();
                                        xmpMetadata.XmpPacket = xmpPacket;

                                        //Update Xmp data in file...
                                        MetadataUtility.UpdateMetadata(filePath, xmpMetadata);
                                        //Clean custom metadata if any...
                                        MetadataUtility.CleanMetadata(filePath);
                                    }
                                }
                            }
                            catch { }
                        }
                        return("1");
                    }
                    else
                    {
                        return("Format not supported.");
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show("Exception: " + exp.Message);
                    return(exp.Message);
                }
            }
            catch (Exception exp)
            {
                return(exp.Message);
            }
        }