Exemplo n.º 1
0
        public void SaveDocumentEncryptedWithAPassword(SaveFormat saveFormat)
        {
            //ExStart
            //ExFor:OdtSaveOptions.#ctor(SaveFormat)
            //ExFor:OdtSaveOptions.Password
            //ExFor:OdtSaveOptions.SaveFormat
            //ExSummary:Shows how to encrypted your odt/ott documents with a password.
            Document doc = new Document(MyDir + "Document.docx");

            OdtSaveOptions saveOptions = new OdtSaveOptions(saveFormat);

            saveOptions.Password = "******";

            // Saving document using password property of OdtSaveOptions
            doc.Save(ArtifactsDir + "OdtSaveOptions.SaveDocumentEncryptedWithAPassword" +
                     FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);
            //ExEnd

            // Check that all documents are encrypted with a password
            FileFormatInfo docInfo = FileFormatUtil.DetectFileFormat(
                ArtifactsDir + "OdtSaveOptions.SaveDocumentEncryptedWithAPassword" +
                FileFormatUtil.SaveFormatToExtension(saveFormat));

            Assert.IsTrue(docInfo.IsEncrypted);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 提取word中的图片
        /// </summary>
        /// <param name="filePath">word文件路径</param>
        /// <param name="savePath">保存文件路径</param>
        /// <returns></returns>
        public static IEnumerable <string> ExportImageFromWordFile(string filePath, string savePath = "")
        {
            if (!File.Exists(filePath))
            {
                yield return(string.Empty);
            }
            if (string.IsNullOrEmpty(savePath))
            {
                savePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\Temp\\";
            }

            //加载word
            Document       doc        = new Document(filePath);
            NodeCollection shapes     = doc.GetChildNodes(NodeType.Shape, true);
            int            imageIndex = 0;

            Shape shape;

            for (int i = 0; i < shapes.Count; i++)
            {
                shape = shapes[i] as Shape;
                if (shape.HasImage)
                {
                    //扩展名
                    string ex = FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType);
                    //文件名
                    string fileName = $"{imageIndex + 1}{ex}";
                    shape.ImageData.Save(savePath + fileName);

                    yield return(fileName);

                    imageIndex++;
                }
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            // Check for an Aspose.Words license file in the local file system and apply it, if it exists.
            string licenseFile = AppDomain.CurrentDomain.BaseDirectory + "Aspose.Words.lic";

            if (File.Exists(licenseFile))
            {
                Aspose.Words.License license = new Aspose.Words.License();

                // Use the license from the bin/debug/ Folder.
                license.SetLicense("Aspose.Words.lic");
            }

            string filePath = "../../data/document.doc";

            // Determine whether this document contains a digital signature.
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(filePath);

            if (info.HasDigitalSignature)
            {
                Console.WriteLine($"Document {new FileInfo(filePath).Name} has digital signatures, they will be lost if you open/save this document with Aspose.Words.", new FileInfo(filePath).Name);
            }
            else
            {
                Console.WriteLine("Document has no digital signature.");
            }
        }
Exemplo n.º 4
0
        public void DetectDocumentEncryption()
        {
            //ExStart
            //ExFor:FileFormatUtil.DetectFileFormat(String)
            //ExFor:FileFormatInfo
            //ExFor:FileFormatInfo.LoadFormat
            //ExFor:FileFormatInfo.IsEncrypted
            //ExSummary:Shows how to use the FileFormatUtil class to detect the document format and encryption.
            Document doc = new Document();

            // Configure a SaveOptions object to encrypt the document
            // with a password when we save it, and then save the document.
            OdtSaveOptions saveOptions = new OdtSaveOptions(SaveFormat.Odt);

            saveOptions.Password = "******";

            doc.Save(ArtifactsDir + "File.DetectDocumentEncryption.odt", saveOptions);

            // Verify the file type of our document, and its encryption status.
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(ArtifactsDir + "File.DetectDocumentEncryption.odt");

            Assert.AreEqual(".odt", FileFormatUtil.LoadFormatToExtension(info.LoadFormat));
            Assert.True(info.IsEncrypted);
            //ExEnd
        }
Exemplo n.º 5
0
        public void ExportTextBoxAsSvg(SaveFormat saveFormat, bool isTextBoxAsSvg)
        {
            string[] dirFiles;

            Document doc = new Document(MyDir + "HtmlSaveOptions.ExportTextBoxAsSvg.docx");

            HtmlSaveOptions saveOptions = new HtmlSaveOptions(saveFormat);

            saveOptions.ExportTextBoxAsSvg = isTextBoxAsSvg;

            doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportTextBoxAsSvg" + FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);

            switch (saveFormat)
            {
            case SaveFormat.Html:

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.IsEmpty(dirFiles);
                return;

            case SaveFormat.Epub:

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.IsEmpty(dirFiles);
                return;

            case SaveFormat.Mhtml:     // ToDo: Check results of this assert

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.IsEmpty(dirFiles);
                return;
            }
        }
Exemplo n.º 6
0
        public void DetectDigitalSignatures()
        {
            //ExStart
            //ExFor:FileFormatUtil.DetectFileFormat(String)
            //ExFor:FileFormatInfo
            //ExFor:FileFormatInfo.LoadFormat
            //ExFor:FileFormatInfo.HasDigitalSignature
            //ExSummary:Shows how to use the FileFormatUtil class to detect the document format and presence of digital signatures.
            // Use a FileFormatInfo instance to verify that a document is not digitally signed.
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Document.docx");

            Assert.AreEqual(".docx", FileFormatUtil.LoadFormatToExtension(info.LoadFormat));
            Assert.False(info.HasDigitalSignature);

            CertificateHolder certificateHolder = CertificateHolder.Create(MyDir + "morzal.pfx", "aw", null);

            DigitalSignatureUtil.Sign(MyDir + "Document.docx", ArtifactsDir + "File.DetectDigitalSignatures.docx",
                                      certificateHolder, new SignOptions()
            {
                SignTime = DateTime.Now
            });

            // Use a new FileFormatInstance to confirm that it is signed.
            info = FileFormatUtil.DetectFileFormat(ArtifactsDir + "File.DetectDigitalSignatures.docx");

            Assert.True(info.HasDigitalSignature);

            // We can load and access the signatures of a signed document in a collection like this.
            Assert.AreEqual(1, DigitalSignatureUtil.LoadSignatures(ArtifactsDir + "File.DetectDigitalSignatures.docx").Count);
            //ExEnd
        }
Exemplo n.º 7
0
        public void ExtractImagesToFiles()
        {
            //ExStart
            //ExFor:Shape
            //ExFor:Shape.ImageData
            //ExFor:Shape.HasImage
            //ExFor:ImageData
            //ExFor:FileFormatUtil.ImageTypeToExtension(ImageType)
            //ExFor:ImageData.ImageType
            //ExFor:ImageData.Save(String)
            //ExFor:CompositeNode.GetChildNodes(NodeType, bool)
            //ExSummary:Shows how to extract images from a document and save them as files.
            Document doc = new Document(MyDir + "Images.docx");

            NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

            Assert.AreEqual(9, shapes.Count(s => ((Shape)s).HasImage));

            int imageIndex = 0;

            foreach (Shape shape in shapes.OfType <Shape>())
            {
                if (shape.HasImage)
                {
                    string imageFileName =
                        $"File.ExtractImagesToFiles.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}";
                    shape.ImageData.Save(ArtifactsDir + imageFileName);
                    imageIndex++;
                }
            }
            //ExEnd

            Assert.AreEqual(9, Directory.GetFiles(ArtifactsDir).
                            Count(s => Regex.IsMatch(s, @"^.+\.(jpeg|png|emf|wmf)$") && s.StartsWith(ArtifactsDir + "File.ExtractImagesToFiles")));
        }
        public void SetEncoding()
        {
            //ExStart
            //ExFor:LoadOptions.Encoding
            //ExSummary:Shows how to set the encoding with which to open a document.
            // A FileFormatInfo object will detect this file as being encoded in something other than UTF-7.
            FileFormatInfo fileFormatInfo = FileFormatUtil.DetectFileFormat(MyDir + "Encoded in UTF-7.txt");

            Assert.AreNotEqual(Encoding.UTF7, fileFormatInfo.Encoding);

            // If we load the document with no loading configurations, Aspose.Words will detect its encoding as UTF-8.
            Document doc = new Document(MyDir + "Encoded in UTF-7.txt");

            // The contents, parsed in UTF-8, create a valid string.
            // However, knowing that the file is in UTF-7, we can see that the result is incorrect.
            Assert.AreEqual("Hello world+ACE-", doc.ToString(SaveFormat.Text).Trim());

            // In cases of ambiguous encoding such as this one, we can set a specific encoding variant
            // to parse the file within a LoadOptions object.
            LoadOptions loadOptions = new LoadOptions
            {
                Encoding = Encoding.UTF7
            };

            // Load the document while passing the LoadOptions object, then verify the document's contents.
            doc = new Document(MyDir + "Encoded in UTF-7.txt", loadOptions);

            Assert.AreEqual("Hello world!", doc.ToString(SaveFormat.Text).Trim());
            //ExEnd
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            // Check for license and apply if exists
            string licenseFile = AppDomain.CurrentDomain.BaseDirectory + "Aspose.Words.lic";

            if (File.Exists(licenseFile))
            {
                // Apply Aspose.Words API License
                Aspose.Words.License license = new Aspose.Words.License();
                // Place license file in Bin/Debug/ Folder
                license.SetLicense("Aspose.Words.lic");
            }

            string filePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\data\" + "Extract Images from Word Document.doc";

            Document wordDocument = new Document(filePath);

            NodeCollection pictures   = wordDocument.GetChildNodes(NodeType.Shape, true);
            int            imageindex = 0;

            foreach (Shape shape in pictures)
            {
                string imageType = FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType);
                if (shape.HasImage)
                {
                    string imageFileName = "Aspose_" + (imageindex++).ToString() + "_" + shape.Name + imageType;
                    shape.ImageData.Save(imageFileName);
                }
            }
        }
Exemplo n.º 10
0
        public void WorkWithEncryptedDocument(SaveFormat saveFormat)
        {
            //ExStart
            //ExFor:OdtSaveOptions.#ctor(String)
            //ExSummary:Shows how to load and change odt/ott encrypted document.
            Document doc = new Document(MyDir + "Encrypted" +
                                        FileFormatUtil.SaveFormatToExtension(saveFormat),
                                        new LoadOptions("@sposeEncrypted_1145"));

            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.MoveToDocumentEnd();
            builder.Writeln("Encrypted document after changes.");

            // Saving document using new instance of OdtSaveOptions
            doc.Save(ArtifactsDir + "OdtSaveOptions.WorkWithEncryptedDocument" +
                     FileFormatUtil.SaveFormatToExtension(saveFormat), new OdtSaveOptions("@sposeEncrypted_1145"));
            //ExEnd

            // Check that document is still encrypted with a password
            FileFormatInfo docInfo =
                FileFormatUtil.DetectFileFormat(ArtifactsDir + "OdtSaveOptions.WorkWithEncryptedDocument" + FileFormatUtil.SaveFormatToExtension(saveFormat));

            Assert.IsTrue(docInfo.IsEncrypted);
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            string FilePath = @"..\..\..\..\Sample Files\";
            string File     = FilePath + "Extract Image - Aspose.docx";

            Document doc = new Document(File);

            // Save document as DOC in memory
            MemoryStream stream = new MemoryStream();

            doc.Save(stream, SaveFormat.Doc);

            // Reload document as DOC to extract images.
            Document       doc2       = new Document(stream);
            NodeCollection shapes     = doc2.GetChildNodes(NodeType.Shape, true);
            int            imageIndex = 0;

            foreach (Shape shape in shapes)
            {
                if (shape.HasImage)
                {
                    string imageFileName = string.Format(
                        "Image.ExportImages.{0}_out_{1}", imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType));
                    shape.ImageData.Save(FilePath + imageFileName);
                    imageIndex++;
                }
            }
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            // Check for license and apply if exists
            string licenseFile = AppDomain.CurrentDomain.BaseDirectory + "Aspose.Words.lic";

            if (File.Exists(licenseFile))
            {
                // Apply Aspose.Words API License
                Aspose.Words.License license = new Aspose.Words.License();
                // Place license file in Bin/Debug/ Folder
                license.SetLicense("Aspose.Words.lic");
            }


            // The path to the document which is to be processed.

            string filePath = "../../data/document.doc";

            FileFormatInfo info = FileFormatUtil.DetectFileFormat(filePath);

            if (info.HasDigitalSignature)
            {
                Console.WriteLine(string.Format("Document {0} has digital signatures, they will be lost if you open/save this document with Aspose.Words.", new FileInfo(filePath).Name));
            }
            else
            {
                Console.WriteLine("Document has no digital signature.");
            }
        }
Exemplo n.º 13
0
        public void ExtractImageFromWordDocumentFeature()
        {
            Document doc = new Document(MyDir + "Extract image.docx");

            // Save the document to memory and reload it.
            using (MemoryStream stream = new MemoryStream())
            {
                doc.Save(stream, SaveFormat.Doc);
                Document doc2 = new Document(stream);

                // "Shape" nodes that have the "HasImage" flag set contain and display images.
                IEnumerable <Shape> shapes = doc2.GetChildNodes(NodeType.Shape, true)
                                             .OfType <Shape>().Where(s => s.HasImage);

                int imageIndex = 0;
                foreach (Shape shape in shapes)
                {
                    string imageFileName =
                        $"Image.ExportImages.{imageIndex}_Aspose.Words_{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}";

                    shape.ImageData.Save(ArtifactsDir + imageFileName);
                    imageIndex++;
                }
            }
        }
        public static void VerifyODTdocument(string dataDir)
        {
            // ExStart:VerifyODTdocument
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(dataDir + @"encrypted.odt");

            Console.WriteLine(info.IsEncrypted);
            // ExEnd:VerifyODTdocument
        }
Exemplo n.º 15
0
        public void VerifyEncryptedDocument()
        {
            //ExStart:VerifyEncryptedDocument
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Encrypted.docx");

            Console.WriteLine(info.IsEncrypted);
            //ExEnd:VerifyEncryptedDocument
        }
Exemplo n.º 16
0
        public void FileFormatToString()
        {
            //ExStart
            //ExFor:FileFormatUtil.ContentTypeToLoadFormat(String)
            //ExFor:FileFormatUtil.ContentTypeToSaveFormat(String)
            //ExSummary:Shows how to find the corresponding Aspose load/save format from an IANA content type string.
            // Trying to search for a SaveFormat with a simple string will not work
            try
            {
                Assert.AreEqual(SaveFormat.Jpeg, FileFormatUtil.ContentTypeToSaveFormat("jpeg"));
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }

            // The convertion methods only accept official IANA type names, which are all listed here:
            //      https://www.iana.org/assignments/media-types/media-types.xhtml
            // Note that if a corresponding SaveFormat or LoadFormat for a type from that list does not exist in the Aspose enums,
            // converting will raise an exception just like in the code above

            // File types that can be saved to but not opened as documents will not have corresponding load formats
            // Attempting to convert them to load formats will raise an exception
            Assert.AreEqual(SaveFormat.Jpeg, FileFormatUtil.ContentTypeToSaveFormat("image/jpeg"));
            Assert.AreEqual(SaveFormat.Png, FileFormatUtil.ContentTypeToSaveFormat("image/png"));
            Assert.AreEqual(SaveFormat.Tiff, FileFormatUtil.ContentTypeToSaveFormat("image/tiff"));
            Assert.AreEqual(SaveFormat.Gif, FileFormatUtil.ContentTypeToSaveFormat("image/gif"));
            Assert.AreEqual(SaveFormat.Emf, FileFormatUtil.ContentTypeToSaveFormat("image/x-emf"));
            Assert.AreEqual(SaveFormat.Xps, FileFormatUtil.ContentTypeToSaveFormat("application/vnd.ms-xpsdocument"));
            Assert.AreEqual(SaveFormat.Pdf, FileFormatUtil.ContentTypeToSaveFormat("application/pdf"));
            Assert.AreEqual(SaveFormat.Svg, FileFormatUtil.ContentTypeToSaveFormat("image/svg+xml"));
            Assert.AreEqual(SaveFormat.Epub, FileFormatUtil.ContentTypeToSaveFormat("application/epub+zip"));

            // File types that can both be loaded and saved have corresponding load and save formats
            Assert.AreEqual(LoadFormat.Doc, FileFormatUtil.ContentTypeToLoadFormat("application/msword"));
            Assert.AreEqual(SaveFormat.Doc, FileFormatUtil.ContentTypeToSaveFormat("application/msword"));

            Assert.AreEqual(LoadFormat.Docx,
                            FileFormatUtil.ContentTypeToLoadFormat(
                                "application/vnd.openxmlformats-officedocument.wordprocessingml.document"));
            Assert.AreEqual(SaveFormat.Docx,
                            FileFormatUtil.ContentTypeToSaveFormat(
                                "application/vnd.openxmlformats-officedocument.wordprocessingml.document"));

            Assert.AreEqual(LoadFormat.Text, FileFormatUtil.ContentTypeToLoadFormat("text/plain"));
            Assert.AreEqual(SaveFormat.Text, FileFormatUtil.ContentTypeToSaveFormat("text/plain"));

            Assert.AreEqual(LoadFormat.Rtf, FileFormatUtil.ContentTypeToLoadFormat("application/rtf"));
            Assert.AreEqual(SaveFormat.Rtf, FileFormatUtil.ContentTypeToSaveFormat("application/rtf"));

            Assert.AreEqual(LoadFormat.Html, FileFormatUtil.ContentTypeToLoadFormat("text/html"));
            Assert.AreEqual(SaveFormat.Html, FileFormatUtil.ContentTypeToSaveFormat("text/html"));

            Assert.AreEqual(LoadFormat.Mhtml, FileFormatUtil.ContentTypeToLoadFormat("multipart/related"));
            Assert.AreEqual(SaveFormat.Mhtml, FileFormatUtil.ContentTypeToSaveFormat("multipart/related"));
            //ExEnd
        }
Exemplo n.º 17
0
        static void Main(string[] args)
        {
            string MyDir = @"E:\Aspose\Aspose Vs VSTO\Aspose.Words Features missing in VSTO 1.1\Sample Files\";

            FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Detect_the_File_Format.doc");

            Console.WriteLine("The document format is: " + FileFormatUtil.LoadFormatToExtension(info.LoadFormat));
            Console.WriteLine("Document is encrypted: " + info.IsEncrypted);
            Console.WriteLine("Document has a digital signature: " + info.HasDigitalSignature);
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            string filePath = @"..\..\..\..\Sample Files\";

            FileFormatInfo info = FileFormatUtil.DetectFileFormat(filePath + "MyDocument.docx");

            Console.WriteLine("The document format is: " + FileFormatUtil.LoadFormatToExtension(info.LoadFormat));
            Console.WriteLine("Document is encrypted: " + info.IsEncrypted);
            Console.WriteLine("Document has a digital signature: " + info.HasDigitalSignature);
        }
Exemplo n.º 19
0
        public void ExportOfficeMath(SaveFormat saveFormat, HtmlOfficeMathOutputMode outputMode)
        {
            Document doc = new Document(MyDir + "OfficeMath.docx");

            HtmlSaveOptions saveOptions = new HtmlSaveOptions();

            saveOptions.OfficeMathOutputMode = outputMode;

            doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportToHtmlUsingImage" + FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);
        }
        public static void Run()
        {
            // ExStart:1
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            // Load File
            FileFormatInfo finfo = FileFormatUtil.DetectFileFormat(dataDir + "sample.xls");

            Console.WriteLine(finfo.FileFormatType == FileFormatType.Excel95);
            // ExEnd:1
        }
Exemplo n.º 21
0
        public static void Run()
        {
            // ExStart:DetectDifferentFileFormats
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_Email();

            // Detect file format and Gets the detected load format
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(dataDir + "message.msg");

            Console.WriteLine("The message format is: " + info.FileFormatType);
            // ExEnd:DetectDifferentFileFormats
        }
Exemplo n.º 22
0
        public void OpenChmFile()
        {
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "HTML help.chm");

            Assert.AreEqual(info.LoadFormat, LoadFormat.Chm);

            LoadOptions loadOptions = new LoadOptions();

            loadOptions.Encoding = Encoding.GetEncoding("windows-1251");

            Document doc = new Document(MyDir + "HTML help.chm", loadOptions);
        }
Exemplo n.º 23
0
        public void DetectFileFormat_SaveFormatToLoadFormat()
        {
            //ExStart
            //ExFor:FileFormatUtil.SaveFormatToLoadFormat(SaveFormat)
            //ExSummary:Shows how to convert a save format to its corresponding load format.
            Assert.AreEqual(LoadFormat.Html, FileFormatUtil.SaveFormatToLoadFormat(SaveFormat.Html));

            // Some file types can have documents saved to, but not loaded from using Aspose.Words.
            // If we attempt to convert a save format of such a type to a load format, an exception will be thrown.
            Assert.Throws <ArgumentException>(() => FileFormatUtil.SaveFormatToLoadFormat(SaveFormat.Jpeg));
            //ExEnd
        }
Exemplo n.º 24
0
        public void ExportPageMargins(SaveFormat saveFormat)
        {
            Document doc = new Document(MyDir + "HtmlSaveOptions.ExportPageMargins.docx");

            HtmlSaveOptions saveOptions = new HtmlSaveOptions
            {
                SaveFormat        = saveFormat,
                ExportPageMargins = true
            };

            doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportPageMargins" + FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);
        }
Exemplo n.º 25
0
        public void FileFormatToString()
        {
            //ExStart
            //ExFor:FileFormatUtil.ContentTypeToLoadFormat(String)
            //ExFor:FileFormatUtil.ContentTypeToSaveFormat(String)
            //ExSummary:Shows how to find the corresponding Aspose load/save format from each media type string.
            // The ContentTypeToSaveFormat/ContentTypeToLoadFormat methods only accept official IANA media type names, also known as MIME types.
            // All valid media types are listed here: https://www.iana.org/assignments/media-types/media-types.xhtml.

            // Trying to associate a SaveFormat with a partial media type string will not work.
            Assert.Throws <ArgumentException>(() => FileFormatUtil.ContentTypeToSaveFormat("jpeg"));

            // If Aspose.Words does not have a corresponding save/load format for a content type, an exception will also be thrown.
            Assert.Throws <ArgumentException>(() => FileFormatUtil.ContentTypeToSaveFormat("application/zip"));

            // Files of the types listed below can be saved, but not loaded using Aspose.Words.
            Assert.Throws <ArgumentException>(() => FileFormatUtil.ContentTypeToLoadFormat("image/jpeg"));

            Assert.AreEqual(SaveFormat.Jpeg, FileFormatUtil.ContentTypeToSaveFormat("image/jpeg"));
            Assert.AreEqual(SaveFormat.Png, FileFormatUtil.ContentTypeToSaveFormat("image/png"));
            Assert.AreEqual(SaveFormat.Tiff, FileFormatUtil.ContentTypeToSaveFormat("image/tiff"));
            Assert.AreEqual(SaveFormat.Gif, FileFormatUtil.ContentTypeToSaveFormat("image/gif"));
            Assert.AreEqual(SaveFormat.Emf, FileFormatUtil.ContentTypeToSaveFormat("image/x-emf"));
            Assert.AreEqual(SaveFormat.Xps, FileFormatUtil.ContentTypeToSaveFormat("application/vnd.ms-xpsdocument"));
            Assert.AreEqual(SaveFormat.Pdf, FileFormatUtil.ContentTypeToSaveFormat("application/pdf"));
            Assert.AreEqual(SaveFormat.Svg, FileFormatUtil.ContentTypeToSaveFormat("image/svg+xml"));
            Assert.AreEqual(SaveFormat.Epub, FileFormatUtil.ContentTypeToSaveFormat("application/epub+zip"));

            // For file types that can be saved and loaded, we can match a media type to both a load format and a save format.
            Assert.AreEqual(LoadFormat.Doc, FileFormatUtil.ContentTypeToLoadFormat("application/msword"));
            Assert.AreEqual(SaveFormat.Doc, FileFormatUtil.ContentTypeToSaveFormat("application/msword"));

            Assert.AreEqual(LoadFormat.Docx,
                            FileFormatUtil.ContentTypeToLoadFormat(
                                "application/vnd.openxmlformats-officedocument.wordprocessingml.document"));
            Assert.AreEqual(SaveFormat.Docx,
                            FileFormatUtil.ContentTypeToSaveFormat(
                                "application/vnd.openxmlformats-officedocument.wordprocessingml.document"));

            Assert.AreEqual(LoadFormat.Text, FileFormatUtil.ContentTypeToLoadFormat("text/plain"));
            Assert.AreEqual(SaveFormat.Text, FileFormatUtil.ContentTypeToSaveFormat("text/plain"));

            Assert.AreEqual(LoadFormat.Rtf, FileFormatUtil.ContentTypeToLoadFormat("application/rtf"));
            Assert.AreEqual(SaveFormat.Rtf, FileFormatUtil.ContentTypeToSaveFormat("application/rtf"));

            Assert.AreEqual(LoadFormat.Html, FileFormatUtil.ContentTypeToLoadFormat("text/html"));
            Assert.AreEqual(SaveFormat.Html, FileFormatUtil.ContentTypeToSaveFormat("text/html"));

            Assert.AreEqual(LoadFormat.Mhtml, FileFormatUtil.ContentTypeToLoadFormat("multipart/related"));
            Assert.AreEqual(SaveFormat.Mhtml, FileFormatUtil.ContentTypeToSaveFormat("multipart/related"));
            //ExEnd
        }
Exemplo n.º 26
0
        public void DetectDocumentSignatures()
        {
            //ExStart:DetectDocumentSignatures
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Digitally signed.docx");

            if (info.HasDigitalSignature)
            {
                Console.WriteLine(
                    $"Document {Path.GetFileName(MyDir + "Digitally signed.docx")} has digital signatures, " +
                    "they will be lost if you open/save this document with Aspose.Words.");
            }
            //ExEnd:DetectDocumentSignatures
        }
Exemplo n.º 27
0
        public bool CheckWordFileForDigitalSignatures(string fileLocation)
        {
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(fileLocation);

            if (info.HasDigitalSignature)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 28
0
        public static void Run()
        {
            // ExStart:1
            //Source directory
            string sourceDir = RunExamples.Get_SourceDirectory();

            var filename = sourceDir + "encryptedBook1.out.tmp";

            Stream stream = File.Open(filename, FileMode.Open);

            FileFormatInfo fileFormatInfo = FileFormatUtil.DetectFileFormat(stream, "1234"); // The password is 1234

            Console.WriteLine("File Format: " + fileFormatInfo.FileFormatType);
            // ExEnd:1
        }
Exemplo n.º 29
0
        public static void Run()
        {
            //ExStart: PreserveEmbeddedMSGFormatDuringLoad
            string dataDir = RunExamples.GetDataDir_Email();

            MailMessage mail = MailMessage.Load(dataDir + "tnefWithMsgInside.eml", new EmlLoadOptions()
            {
                PreserveEmbeddedMessageFormat = true
            });

            FileFormatType fileFormat = FileFormatUtil.DetectFileFormat(mail.Attachments[0].ContentStream).FileFormatType;

            Console.WriteLine("Embedded message file format: " + fileFormat);
            //ExEnd: PreserveEmbeddedMSGFormatDuringLoad
        }
Exemplo n.º 30
0
        public void DetectFileFormat_SaveFormatToLoadFormat()
        {
            //ExStart
            //ExFor:FileFormatUtil.SaveFormatToLoadFormat(SaveFormat)
            //ExSummary:Shows how to use the FileFormatUtil class and to convert a SaveFormat enumeration into the corresponding LoadFormat enumeration.
            // Define the SaveFormat enumeration to convert.
            SaveFormat saveFormat = SaveFormat.Html;
            // Convert the SaveFormat enumeration to LoadFormat enumeration.
            LoadFormat loadFormat = FileFormatUtil.SaveFormatToLoadFormat(saveFormat);

            Console.WriteLine("The converted LoadFormat is: " + FileFormatUtil.LoadFormatToExtension(loadFormat));
            //ExEnd

            Assert.AreEqual(".html", FileFormatUtil.SaveFormatToExtension(saveFormat));
            Assert.AreEqual(".html", FileFormatUtil.LoadFormatToExtension(loadFormat));
        }