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 }
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 }