Example #1
0
            /// <summary>
            /// Removes custom property of Pdf file and creates output file
            /// </summary>
            public static void RemoveCustomProperties()
            {
                try
                {
                    //ExStart:RemoveCustomDocumentPropertiesPdfFormat
                    // initialize PdfFormat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    // initialize PdfMetadata
                    PdfMetadata metadata = pdfFormat.DocumentProperties;

                    string propertyName = "New Custom Property";

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

                    // save file in destination folder
                    pdfFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveCustomDocumentPropertiesPdfFormat
                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
Example #2
0
            /// <summary>
            /// Adds custom property in Pdf file and creates output file
            /// </summary>
            public static void AddCustomProperty()
            {
                try
                {
                    //ExStart:AddCustomDocumentPropertyPdfFormat
                    // initialize PdfFormat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    // initialize PdfMetadata
                    PdfMetadata metadata = pdfFormat.DocumentProperties;

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


                    // check if property already exists
                    if (!metadata.ContainsKey(propertyName))
                    {
                        // add property
                        metadata.Add(propertyName, propertyValue);
                    }

                    // save file in destination folder
                    pdfFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:AddCustomDocumentPropertyPdfFormat
                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
Example #3
0
            /// <summary>
            /// Updates document properties of Pdf file and creates output file
            /// </summary>
            public static void UpdateDocumentProperties()
            {
                try
                {
                    //ExStart:UpdateBuiltinDocumentPropertyPdfFormat
                    // initialize PdfFormat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    // initialize PdfMetadata
                    PdfMetadata pdfMetadata = pdfFormat.DocumentProperties;

                    //update document property...
                    pdfMetadata.Author      = "New author";
                    pdfMetadata.Subject     = "New subject";
                    pdfMetadata.CreatedDate = DateTime.Now;

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

                    //ExEnd:UpdateBuiltinDocumentPropertyPdfFormat
                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
Example #4
0
            /// <summary>
            /// Removes document properties of Pdf file and creates output file
            /// </summary>
            public static void RemoveDocumentProperties()
            {
                try
                {
                    //ExStart:RemoveBuiltinDocumentPropertyPdfFormat
                    // initialize PdfFormat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    pdfFormat.CleanMetadata();

                    //save output file...
                    pdfFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveBuiltinDocumentPropertyPdfFormat

                    Console.WriteLine("File saved in destination folder.");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            public static void RemoveHiddenData()
            {
                try
                {
                    //ExStart:RemoveHiddenDataPdfFormat
                    // initialize Pdfformat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    // inspect document
                    PdfInspectionResult inspectionResult = pdfFormat.InspectDocument();

                    // get annotations
                    PdfAnnotation[] annotation = inspectionResult.Annotations;

                    // if annotations are presented
                    if (annotation.Length > 0)
                    {
                        // remove all annotation
                        pdfFormat.RemoveHiddenData(new PdfInspectionOptions(PdfInspectorOptionsEnum.Annotations));

                        //save output file...
                        pdfFormat.Save(Common.MapDestinationFilePath(filePath));
                    }
                    //ExEnd:RemoveHiddenDataPdfFormat
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Updates XMP Data of Pdf file  
            /// </summary> 
            public static void UpdateXMPProperties()
            {
                try
                {
                    //ExStart:UpdateXMPPropertiesPdfFormat
                    // initialize Pdfformat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    // get pdf schema
                    PdfPackage pdfPackage = pdfFormat.XmpValues.Schemes.Pdf;

                    // update keywords
                    pdfPackage.Keywords = "literature, programming";

                    // update pdf version
                    pdfPackage.PdfVersion = "1.0";

                    // pdf:Producer could not be updated
                    //pdfPackage.Producer="";

                    //save output file...
                    pdfFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:UpdateXMPPropertiesPdfFormat
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Clears custom properties of Pdf file and creates output file
            /// </summary> 
            public static void ClearCustomProperties()
            {
                try
                {
                    //ExStart:ClearCustomPropertyPdfFormat
                    // initialize PdfFormat
                    PdfFormat PdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

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

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

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

                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes custom property of Pdf file and creates output file
            /// </summary> 
            public static void RemoveCustomProperties()
            {
                try
                {
                    //ExStart:RemoveCustomDocumentPropertiesPdfFormat
                    // initialize PdfFormat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    // initialize PdfMetadata
                    PdfMetadata metadata = pdfFormat.DocumentProperties;

                    string propertyName = "New Custom Property";

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

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

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

                    // initialize PdfMetadata
                    PdfMetadata metadata = pdfFormat.DocumentProperties;

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


                    // check if property already exists
                    if (!metadata.ContainsKey(propertyName))
                    {
                        // add property
                        metadata.Add(propertyName, propertyValue);
                    }

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


                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Removes document properties of Pdf file and creates output file
            /// </summary> 
            public static void RemoveDocumentProperties()
            {
                try
                {
                    //ExStart:RemoveBuiltinDocumentPropertyPdfFormat
                    // initialize PdfFormat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    pdfFormat.CleanMetadata();

                    //save output file...
                    pdfFormat.Save(Common.MapDestinationFilePath(filePath));
                    //ExEnd:RemoveBuiltinDocumentPropertyPdfFormat

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


                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            /// <summary>
            /// Updates document properties of Pdf file and creates output file
            /// </summary> 
            public static void UpdateDocumentProperties()
            {
                try
                {

                    //ExStart:UpdateBuiltinDocumentPropertyPdfFormat
                    // initialize PdfFormat
                    PdfFormat pdfFormat = new PdfFormat(Common.MapSourceFilePath(filePath));

                    // initialize PdfMetadata
                    PdfMetadata pdfMetadata = pdfFormat.DocumentProperties;

                    //update document property...
                    pdfMetadata.Author = "New author";
                    pdfMetadata.Subject = "New subject";
                    pdfMetadata.CreatedDate = DateTime.Now;

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

                    //ExEnd:UpdateBuiltinDocumentPropertyPdfFormat
                    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;
            }
        }