示例#1
0
        public void DetectFileFormat_EnumConversions()
        {
            //ExStart
            //ExFor:FileFormatUtil.DetectFileFormat(Stream)
            //ExFor:FileFormatUtil.LoadFormatToExtension(LoadFormat)
            //ExFor:FileFormatUtil.ExtensionToSaveFormat(String)
            //ExFor:FileFormatUtil.SaveFormatToExtension(SaveFormat)
            //ExFor:FileFormatUtil.LoadFormatToSaveFormat(LoadFormat)
            //ExFor:Document.OriginalFileName
            //ExFor:FileFormatInfo.LoadFormat
            //ExSummary:Shows how to use the FileFormatUtil methods to detect the format of a document without any extension and save it with the correct file extension.
            // Load the document without a file extension into a stream and use the DetectFileFormat method to detect it's format.
            // These are both times where you might need extract the file format as it's not visible
            FileStream
                docStream = File.OpenRead(
                MyDir + "Document.FileWithoutExtension");     // The file format of this document is actually ".doc"
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(docStream);

            // Retrieve the LoadFormat of the document.
            LoadFormat loadFormat = info.LoadFormat;

            // Let's show the different methods of converting LoadFormat enumerations to SaveFormat enumerations.
            //
            // Method #1
            // Convert the LoadFormat to a String first for working with. The String will include the leading dot in front of the extension.
            String fileExtension = FileFormatUtil.LoadFormatToExtension(loadFormat);
            // Now convert this extension into the corresponding SaveFormat enumeration
            SaveFormat saveFormat = FileFormatUtil.ExtensionToSaveFormat(fileExtension);

            // Method #2
            // Convert the LoadFormat enumeration directly to the SaveFormat enumeration.
            saveFormat = FileFormatUtil.LoadFormatToSaveFormat(loadFormat);

            // Load a document from the stream.
            Document doc = new Document(docStream);

            // Save the document with the original file name, " Out" and the document's file extension.
            doc.Save(
                ArtifactsDir + "Document.WithFileExtension" + FileFormatUtil.SaveFormatToExtension(saveFormat));
            //ExEnd

            Assert.AreEqual(".doc", FileFormatUtil.SaveFormatToExtension(saveFormat));
        }
示例#2
0
        public void SaveToDetectedFileFormat()
        {
            //ExStart
            //ExFor:FileFormatUtil.DetectFileFormat(Stream)
            //ExFor:FileFormatUtil.LoadFormatToExtension(LoadFormat)
            //ExFor:FileFormatUtil.ExtensionToSaveFormat(String)
            //ExFor:FileFormatUtil.SaveFormatToExtension(SaveFormat)
            //ExFor:FileFormatUtil.LoadFormatToSaveFormat(LoadFormat)
            //ExFor:Document.OriginalFileName
            //ExFor:FileFormatInfo.LoadFormat
            //ExFor:LoadFormat
            //ExSummary:Shows how to use the FileFormatUtil methods to detect the format of a document.
            // Load a document from a file that is missing a file extension, and then detect its file format.
            using (FileStream docStream = File.OpenRead(MyDir + "Word document with missing file extension"))
            {
                FileFormatInfo info       = FileFormatUtil.DetectFileFormat(docStream);
                LoadFormat     loadFormat = info.LoadFormat;

                Assert.AreEqual(LoadFormat.Doc, loadFormat);

                // Below are two methods of converting a LoadFormat to its corresponding SaveFormat.
                // 1 -  Get the file extension string for the LoadFormat, then get the corresponding SaveFormat from that string:
                string     fileExtension = FileFormatUtil.LoadFormatToExtension(loadFormat);
                SaveFormat saveFormat    = FileFormatUtil.ExtensionToSaveFormat(fileExtension);

                // 2 -  Convert the LoadFormat directly to its SaveFormat:
                saveFormat = FileFormatUtil.LoadFormatToSaveFormat(loadFormat);

                // Load a document from the stream, and then save it to the automatically detected file extension.
                Document doc = new Document(docStream);

                Assert.AreEqual(".doc", FileFormatUtil.SaveFormatToExtension(saveFormat));

                doc.Save(ArtifactsDir + "File.SaveToDetectedFileFormat" + FileFormatUtil.SaveFormatToExtension(saveFormat));
            }
            //ExEnd
        }