示例#1
0
            /// <summary>
            /// Updates document properties of Doc file and creates output file
            /// </summary>
            public static void UpdateDocumentProperties()
            {
                try
                {
                    //ExStart:UpdateBuiltinDocumentPropertiesDocFormat
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // initialize DocMetadata
                    DocMetadata docMetadata = docFormat.DocumentProperties;

                    //update document property...
                    docMetadata.Author  = "Usman";
                    docMetadata.Company = "Aspose";
                    docMetadata.Manager = "Usman Aziz";

                    //save output file...
                    docFormat.Save(Common.MapDestinationFilePath(filePath));

                    //ExEnd:UpdateBuiltinDocumentPropertiesDocFormat
                    Console.WriteLine("Updated Successfully.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Updates document properties of Doc file and creates output file
            /// </summary> 
            public static void UpdateDocumentProperties()
            {
                try
                {
                    //ExStart:UpdateBuiltinDocumentPropertiesDocFormat
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // initialize DocMetadata
                    DocMetadata docMetadata = docFormat.DocumentProperties;

                    //update document property...
                    docMetadata.Author = "Usman";
                    docMetadata.Company = "Aspose";
                    docMetadata.Manager = "Usman Aziz";

                    //save output file...
                    docFormat.Save(Common.MapDestinationFilePath(filePath));

                    //ExEnd:UpdateBuiltinDocumentPropertiesDocFormat
                    Console.WriteLine("Updated Successfully.");

                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }

            }
示例#3
0
            /// <summary>
            /// Adds custom property in Doc file and creates output file
            /// </summary>
            public static void AddCustomProperty()
            {
                try
                {
                    //ExStart:AddCustomPropertyDocFormat
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // initialize DocMetadata
                    DocMetadata metadata = docFormat.DocumentProperties;


                    string propertyName  = "New Custom Property";
                    string propertyValue = "123";

                    // add boolean key
                    if (!metadata.ContainsKey(propertyName))
                    {
                        // add property
                        metadata.Add(propertyName, propertyValue);
                    }

                    // save file in destination folder
                    docFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:AddCustomPropertyDocFormat
                    Console.WriteLine("File saved in destination folder.");
                }
                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.");
        }
示例#5
0
            /// <summary>
            /// Removes custom properties of Doc file and creates output file
            /// </summary>
            public static void RemoveCustomProperties()
            {
                try
                {
                    //ExStart:RemoveCustomPropertyDocFormat
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // initialize DocMetadata
                    DocMetadata metadata = docFormat.DocumentProperties;

                    string propertyName = "New Custom Property";

                    // check if property is not built-in
                    if (metadata.ContainsKey(propertyName))
                    {
                        if (!metadata.IsBuiltIn(propertyName))
                        {
                            // remove property
                            metadata.Remove(propertyName);
                        }
                        else
                        {
                            Console.WriteLine("Can not remove built-in property.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Property does not exist.");
                    }

                    bool isexist = metadata.ContainsKey(propertyName);

                    // save file in destination folder
                    docFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveCustomPropertyDocFormat
                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
示例#6
0
            /// <summary>
            /// Removes document comments of Doc file
            /// </summary>
            public static void RemoveComments()
            {
                try
                {
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // remove comments
                    docFormat.ClearComments();

                    // save file in destination folder
                    docFormat.Save(Common.MapDestinationFilePath(filePath));

                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
示例#7
0
            /// <summary>
            /// Removes document properties of Doc file and creates output file
            /// </summary>
            public static void RemoveDocumentProperties()
            {
                try
                {
                    //ExStart:RemoveBuiltinDocumentPropertiesDocFormat
                    // initialize Docformat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    //Clean metadata
                    docFormat.CleanMetadata();

                    // save output file...
                    docFormat.Save(Common.MapDestinationFilePath(filePath));

                    //ExEnd:RemoveBuiltinDocumentPropertiesDocFormat
                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes document properties of Doc file and creates output file
            /// </summary> 
            public static void RemoveDocumentProperties()
            {
                try
                {
                    //ExStart:RemoveBuiltinDocumentPropertiesDocFormat
                    // initialize Docformat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    //Clean metadata
                    docFormat.CleanMetadata();

                    // save output file...
                    docFormat.Save(Common.MapDestinationFilePath(filePath));

                    //ExEnd:RemoveBuiltinDocumentPropertiesDocFormat
                    Console.WriteLine("File saved in destination folder.");


                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            ///  Save Changes after updating metadata of specific format
            /// </summary>
            public static void SaveFileAfterMetadataUpdate()
            {
                //ExStart:SaveFileAfterMetadataUpdate
                // initialize DocFormat
                DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                // update document properties
                docFormat.DocumentProperties.Author = "Joe Doe";
                docFormat.DocumentProperties.Company = "Aspose";

                // and commit changes
                docFormat.Save();
                //ExEnd:SaveFileAfterMetadataUpdate
            }
            /// <summary>
            /// Gets comments, merge fields and hidden fields of Doc file
            /// </summary> 
            public static void RemoveMergeFields()
            {
                try
                {
                    //ExStart:RemoveHiddenDataInDocument
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // inspect document
                    //InspectionResult inspectionResult = docFormat.InspectDocument();
                    DocInspectionResult inspectionResult = docFormat.InspectDocument();

                    // if merge fields are presented
                    if (inspectionResult.Fields.Length > 0)
                    {
                        // remove it
                        docFormat.RemoveHiddenData(new DocInspectionOptions(DocInspectorOptionsEnum.Fields));

                        // save file in destination folder
                        docFormat.Save(Common.MapDestinationFilePath(filePath));
                    }
                    //ExEnd:RemoveHiddenDataInDocument

                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Updates document comments of Doc file  
            /// </summary> 
            public static void UpdateComments()
            {
                try
                {
                    //ExStart:UpdateDocumentComment
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // extract comments
                    DocComment[] comments = docFormat.ExtractComments();

                    if (comments.Length > 0)
                    {
                        // get first comment if exist
                        var comment = comments[0];

                        // change comment's author
                        comment.Author = "Jack London";

                        // change comment's text
                        comment.Text = "This comment is created using GroupDocs.Metadata";

                        // update comment
                        docFormat.UpdateComment(comment.Id, comment);
                    }

                    // save file in destination folder
                    docFormat.Save(Common.MapDestinationFilePath(filePath));

                    Console.WriteLine("File saved in destination folder.");
                    //ExEnd:UpdateDocumentComment

                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes document comments of Doc file  
            /// </summary> 
            public static void RemoveComments()
            {
                try
                {

                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // remove comments
                    docFormat.ClearComments();

                    // save file in destination folder
                    docFormat.Save(Common.MapDestinationFilePath(filePath));

                    Console.WriteLine("File saved in destination folder.");


                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Clears custom properties of Doc file and creates output file
            /// </summary> 
            public static void ClearCustomProperties()
            {
                try
                {
                    //ExStart:ClearCustomPropertyDocFormat
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // use one of the following methods
                    // method:1 - clear custom properties 
                    docFormat.ClearCustomProperties();

                    // method:2 - clear custom properties 
                    docFormat.DocumentProperties.ClearCustomData();

                    // save file in destination folder
                    docFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:ClearCustomPropertyDocFormat
                    Console.WriteLine("File saved in destination folder.");

                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes custom properties of Doc file and creates output file
            /// </summary> 
            public static void RemoveCustomProperties()
            {
                try
                {
                    //ExStart:RemoveCustomPropertyDocFormat
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // initialize DocMetadata
                    DocMetadata metadata = docFormat.DocumentProperties;

                    string propertyName = "New Custom Property";

                    // check if property is not built-in
                    if (metadata.ContainsKey(propertyName))
                    {
                        if (!metadata.IsBuiltIn(propertyName))
                        {
                            // remove property
                            metadata.Remove(propertyName);

                        }
                        else
                        {
                            Console.WriteLine("Can not remove built-in property.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Property does not exist.");
                    }

                    bool isexist = metadata.ContainsKey(propertyName);

                    // save file in destination folder
                    docFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveCustomPropertyDocFormat
                    Console.WriteLine("File saved in destination folder.");

                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Adds custom property in Doc file and creates output file
            /// </summary> 
            public static void AddCustomProperty()
            {
                try
                {
                    //ExStart:AddCustomPropertyDocFormat
                    // initialize DocFormat
                    DocFormat docFormat = new DocFormat(Common.MapSourceFilePath(filePath));

                    // initialize DocMetadata
                    DocMetadata metadata = docFormat.DocumentProperties;


                    string propertyName = "New Custom Property";
                    string propertyValue = "123";

                    // add boolean key
                    if (!metadata.ContainsKey(propertyName))
                    {
                        // add property
                        metadata.Add(propertyName, propertyValue);
                    }

                    // save file in destination folder
                    docFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:AddCustomPropertyDocFormat
                    Console.WriteLine("File saved in destination folder.");


                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
        public HttpResponseMessage Get(string file)
        {
            try
            {
                File.Copy(Utils._storagePath + "\\" + file, Utils._storagePath + "\\Cleaned_" + file, true);
                FileStream        original          = File.Open(Utils._storagePath + "\\Cleaned_" + file, FileMode.Open, FileAccess.ReadWrite);
                FileFormatChecker fileFormatChecker = new FileFormatChecker(original);
                DocumentType      documentType      = fileFormatChecker.GetDocumentType();


                if (fileFormatChecker.VerifyFormat(documentType))
                {
                    switch (documentType)
                    {
                    case DocumentType.Doc:

                        DocFormat docFormat = new DocFormat(original);
                        docFormat.CleanMetadata();
                        docFormat.ClearBuiltInProperties();
                        docFormat.ClearComments();
                        docFormat.ClearCustomProperties();
                        docFormat.RemoveHiddenData(new DocInspectionOptions(DocInspectorOptionsEnum.All));

                        docFormat.Save(Utils._storagePath + "\\Cleaned_" + file);
                        break;

                    case DocumentType.Xls:

                        XlsFormat xlsFormat = new XlsFormat(original);
                        xlsFormat.CleanMetadata();
                        xlsFormat.ClearBuiltInProperties();
                        xlsFormat.ClearContentTypeProperties();
                        xlsFormat.ClearCustomProperties();
                        xlsFormat.RemoveHiddenData(new XlsInspectionOptions(XlsInspectorOptionsEnum.All));

                        xlsFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Pdf:

                        PdfFormat pdfFormat = new PdfFormat(original);
                        pdfFormat.CleanMetadata();
                        pdfFormat.ClearBuiltInProperties();
                        pdfFormat.ClearCustomProperties();
                        pdfFormat.RemoveHiddenData(new PdfInspectionOptions(PdfInspectorOptionsEnum.All));
                        pdfFormat.RemoveXmpData();

                        pdfFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Png:

                        PngFormat pngFormat = new PngFormat(original);
                        pngFormat.CleanMetadata();
                        pngFormat.RemoveXmpData();

                        pngFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Jpeg:

                        JpegFormat jpegFormat = new JpegFormat(original);
                        jpegFormat.CleanMetadata();
                        jpegFormat.RemoveExifInfo();
                        jpegFormat.RemoveGpsLocation();
                        jpegFormat.RemoveIptc();
                        jpegFormat.RemovePhotoshopData();
                        jpegFormat.RemoveXmpData();

                        jpegFormat.Save(original);

                        break;

                    case DocumentType.Bmp:

                        BmpFormat bmpFormat = new BmpFormat(original);
                        bmpFormat.CleanMetadata();

                        bmpFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Gif:

                        GifFormat gifFormat = new GifFormat(original);
                        gifFormat.CleanMetadata();
                        gifFormat.RemoveXmpData();

                        gifFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Msg:

                        OutlookMessage outlookMessage = new OutlookMessage(original);
                        outlookMessage.CleanMetadata();
                        outlookMessage.RemoveAttachments();

                        outlookMessage.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Eml:

                        EmlFormat emlFormat = new EmlFormat(original);
                        emlFormat.CleanMetadata();
                        emlFormat.RemoveAttachments();

                        emlFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Dwg:

                        DwgFormat dwgFormat = new DwgFormat(original);
                        dwgFormat.CleanMetadata();

                        dwgFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    case DocumentType.Dxf:

                        DxfFormat dxfFormat = new DxfFormat(original);
                        dxfFormat.CleanMetadata();

                        dxfFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;

                    default:

                        DocFormat defaultDocFormat = new DocFormat(original);
                        defaultDocFormat.CleanMetadata();
                        defaultDocFormat.ClearBuiltInProperties();
                        defaultDocFormat.ClearComments();
                        defaultDocFormat.ClearCustomProperties();
                        defaultDocFormat.RemoveHiddenData(new DocInspectionOptions(DocInspectorOptionsEnum.All));

                        defaultDocFormat.Save(Utils._storagePath + "\\Cleaned_" + file);

                        break;
                    }
                }
                else
                {
                    throw new Exception("File format not supported.");
                }

                using (var ms = new MemoryStream())
                {
                    original = File.OpenRead(Utils._storagePath + "\\Cleaned_" + file);
                    original.CopyTo(ms);
                    var result = new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(ms.ToArray())
                    };
                    result.Content.Headers.ContentDisposition =
                        new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                    {
                        FileName = "Cleaned_" + file
                    };
                    result.Content.Headers.ContentType =
                        new MediaTypeHeaderValue("application/octet-stream");

                    original.Close();
                    File.Delete(Utils._storagePath + "\\Cleaned_" + file);
                    return(result);
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }