/// <summary>
            /// Shows how to read APEV2 tags in MP3 format
            /// Feature is supported in version 17.9.0 or greater of the API
            /// </summary>
            public static void ReadApev2Tag()
            {
                //ExStart:ReadApev2TagMp3
                // initialize Mp3Format. If file is not Mp3 then appropriate exception will throw.
                using (Mp3Format mp3Format = new Mp3Format(Common.MapSourceFilePath(filePath)))
                {
                    // get APEv2 tag
                    Apev2Metadata apev2 = mp3Format.APEv2;

                    //NOTE: please remember you may use different approaches to getting metadata

                    // second approach
                    apev2 = (Apev2Metadata)MetadataUtility.ExtractSpecificMetadata(Common.MapSourceFilePath(filePath), MetadataType.APEv2);

                    // check if APEv2 tag is presented
                    if (apev2 != null)
                    {
                        // Display tag properties
                        Console.WriteLine("Album: {0}", apev2.Album);
                        Console.WriteLine("Artist: {0}", apev2.Artist);
                        Console.WriteLine("Comment: {0}", apev2.Comment);
                        Console.WriteLine("Genre: {0}", apev2.Genre);
                        Console.WriteLine("Title: {0}", apev2.Title);
                        Console.WriteLine("Track: {0}", apev2.Track);
                    }
                }
                //ExEnd:ReadApev2TagMp3
            }
예제 #2
0
        /// <summary>
        /// Removes GPS data and updates the image files in a directory
        /// </summary>
        public void RemoveExifLocation()
        {
            // Map directory in source folder
            string sourceDirectoryPath = Common.MapSourceFilePath(this.CleanerPath);

            // get array of file in specific directory
            string[] files = Directory.GetFiles(sourceDirectoryPath);

            foreach (string path in files)
            {
                // get EXIF data if exists
                ExifMetadata exifMetadata = (ExifMetadata)MetadataUtility.ExtractSpecificMetadata(path, MetadataType.EXIF);

                if (exifMetadata != null)
                {
                    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;
                    }

                    // and update file
                    MetadataUtility.UpdateMetadata(path, exifMetadata);
                }
            }
            Console.WriteLine("Press any key to exit.");
        }
 /// <summary>
 /// Get Dublin Core metadata using MetadataUtility class.
 /// Feature is supported in version 18.5 or greater of the API
 /// </summary>
 public static void GetDublinCoreMetadata()
 {
     try
     {
         string[] files = Directory.GetFiles(XMPFilesDirectory);
         foreach (string file in files)
         {
             try
             {
                 DublinCoreMetadata dublinCoreMetadata = (DublinCoreMetadata)MetadataUtility.ExtractSpecificMetadata(file, MetadataType.DublinCore);
                 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;
     }
 }
            /// <summary>
            /// Reads MPEG audio information
            /// </summary>
            ///
            public static void ReadMPEGAudioInfo()
            {
                try
                {
                    //ExStart:ReadMPEGAudioInfo
                    // get MPEG audio info
                    MpegAudio audioInfo = (MpegAudio)MetadataUtility.ExtractSpecificMetadata(Common.MapSourceFilePath(filePath), MetadataType.MpegAudio);

                    // another approach is to use Mp3Format directly:

                    // init Mp3Format class
                    // Mp3Format mp3Format = new Mp3Format((Common.MapSourceFilePath(filePath));

                    // get MPEG audio info
                    // MpegAudio audioInfo = mp3Format.AudioDetails;

                    // display MPEG audio version
                    Console.WriteLine("MPEG audio version: {0}", audioInfo.MpegAudioVersion);

                    // display layer version
                    Console.WriteLine("Layer version: {0}", audioInfo.LayerVersion);

                    // display header offset
                    Console.WriteLine("Header offset: {0}", audioInfo.HeaderPosition);

                    // display bitrate
                    Console.WriteLine("Bitrate: {0}", audioInfo.Bitrate);

                    // display frequency
                    Console.WriteLine("Frequency: {0}", audioInfo.Frequency);

                    // display channel mode
                    Console.WriteLine("Channel mode: {0}", audioInfo.ChannelMode);

                    // display original bit
                    Console.WriteLine("Is original: {0}", audioInfo.IsOriginal);

                    // display protected bit
                    Console.WriteLine("Is protected: {0}", audioInfo.IsProtected);
                    //ExEnd:ReadMPEGAudioInfo
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            /// <summary>
            /// Read ImageCover using Metadata Utility
            /// Feature is supported in version 18.4 or greater of the API
            /// </summary>
            public static void ReadImageCoverMetadataUtility()
            {
                try
                {
                    //Read DublinCore Metadata
                    ThumbnailMetadata thumbnailMetadata = (ThumbnailMetadata)MetadataUtility.ExtractSpecificMetadata(Common.MapSourceFilePath(filePath), MetadataType.Thumbnail);

                    if (thumbnailMetadata != null)
                    {
                        // get Mime Type
                        Console.WriteLine(thumbnailMetadata.MimeType);
                        // get Length
                        Console.WriteLine(thumbnailMetadata.ImageData.Length);
                    }
                }
                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);
            }
        }