Aspose.Words for .NET使用教程(十一):如何检测文件格式和检查格式兼容性?

Aspose.Words无需Microsoft Word也可在任何平台上满足Word文档的一切操作需求。本文将与大家分享如何检测文件格式和检查格式兼容性。

                           【下载Aspose.Words for .NET最新试用版

有时我们需要在打开文件之前检测文档文件的格式,因为文件扩展名不能保证文件内容是合适的。

例如,大家都知道,Crystal Reports通常以RTF格式输出文档,但文档的扩展名却是.doc。因此,如果你不确定文件的实际内容是什么并且希望在打开文件过程中不要出现异常,则可以使用FileFormatUtil.DetectFileFormat方法。 这是一个静态方法,它接受包含文件数据的文件名或流对象。该方法返回一个FileFormatInfo对象,该对象包含检测到的有关文件类型的信息。

当你处理各种文件格式的多个文档时,你可能需要将那些可以由Aspose.Words处理的文件和那些不能处理的文件分开。你可能还想知道为什么某些文档无法处理。

如果你尝试将文件加载到Document对象中并且Aspose.Words无法识别文件格式或不支持该格式,Aspose.Words将出现异常。你可以记录这些异常并对其进行分析,但Aspose.Words还提供了一种专门的方法,可以快速确定文件格式,而不需要加载可能有异常的文档。

我们将在代码中执行以下步骤,以检查所选文件夹中所有文件的格式兼容性,并按文件格式将它们排序到适当的子文件夹中。

  1. 获取所选文件夹中所有文件的集合。
  2. 循环收集。
  3. 对于每个文件:
    • 检查文件格式。
    • 显示检查结果。
    • 将文件移动到适当的文件夹。

此示例中使用了以下文件。文件名在左侧,其描述在右侧。测试支持的文件格式:

输入文件类型
测试文件(XML).xmlFlatOPC OOXML文档。
测试文件(WordML).xmlMicrosoft Word 2003 WordprocessingML文档。
测试文件(rtf).rtf富文本格式文档。
测试文件(odt).odtOpenDocument文本格式(OpenOffice Writer)。
测试文件(MHTML).mhtmlMHTML(Web档案)文档。
测试文件(HTML).htmlHTML文档。
测试文件(dotx).dotxOffice Open XML WordprocessingML模板。
测试文件(dot).dotMicrosoft Word 97 - 2003模板
测试文件(docx).docx没有宏的Office Open XML WordprocessingML文档。
测试文件(docm).docm有宏的Office Open XML WordprocessingML文档。
测试文件(doc).docMicrosoft Word 97 - 2003文档。

测试加密文档:

输入文件类型
测试文件(enc).doc加密的Microsoft Word 97 - 2003文档。
测试文件(enc).docx加密的Office Open XML WordprocessingML文档。

不支持的文件格式:

输入文件类型
测试文件(pre97).docMicrosoft Word 95文档。
测试文件(JPG).jpgJPEG图像文件。

当我们处理文件夹中的内容时,我们首先要做的是使用Directory类的GetFiles方法(来自System.IO命名空间)获取此文件夹中所有文件的集合。

收集完所有文件后,其余工作由Aspose.Words组件中的 FileFormatUtil.DetectFileFormat 方法完成。FileFormatUtil.DetectFileFormat 方法检查文件格式,但请注意它只检查文件格式,它不验证文件格式。 这意味着即使FileFormatUtil.DetectFileFormat 的返回结果表明此文件格式是受支持的格式之一,也无法保证文件将被顺利打开。这是因为FileFormatUtil.DetectFileFormat 方法只读取文件格式的部分数据,足以检查文件格式,但不足以完成验证。 以下代码演示检查文件格式:

using System;using System.Collections;using System.IO; using Aspose.Words;using Aspose.Words.Tables;using System.Diagnostics; namespace Aspose.Words.Examples.CSharp.Loading_Saving{    class CheckFormat    {        public static void Run()        {            // ExStart:CheckFormatCompatibility            // The path to the documents directory.            string dataDir = RunExamples.GetDataDir_LoadingAndSaving();             string supportedDir = dataDir + "OutSupported";            string unknownDir = dataDir + "OutUnknown";            string encryptedDir = dataDir + "OutEncrypted";            string pre97Dir = dataDir + "OutPre97";             // Create the directories if they do not already exist            if (Directory.Exists(supportedDir) == false)                Directory.CreateDirectory(supportedDir);            if (Directory.Exists(unknownDir) == false)                Directory.CreateDirectory(unknownDir);            if (Directory.Exists(encryptedDir) == false)                Directory.CreateDirectory(encryptedDir);            if (Directory.Exists(pre97Dir) == false)                Directory.CreateDirectory(pre97Dir);             // ExStart:GetListOfFilesInFolder            string[] fileList = Directory.GetFiles(dataDir);            // ExEnd:GetListOfFilesInFolder            // Loop through all found files.            foreach (string fileName in fileList)            {                // Extract and display the file name without the path.                string nameOnly = Path.GetFileName(fileName);                Console.Write(nameOnly);                // ExStart:DetectFileFormat                // Check the file format and move the file to the appropriate folder.                FileFormatInfo info = FileFormatUtil.DetectFileFormat(fileName);                                 // Display the document type.                switch (info.LoadFormat)                {                    case LoadFormat.Doc:                        Console.WriteLine("\tMicrosoft Word 97-2003 document.");                        break;                    case LoadFormat.Dot:                        Console.WriteLine("\tMicrosoft Word 97-2003 template.");                        break;                    case LoadFormat.Docx:                        Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Free Document.");                        break;                    case LoadFormat.Docm:                        Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Enabled Document.");                        break;                    case LoadFormat.Dotx:                        Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Free Template.");                        break;                    case LoadFormat.Dotm:                        Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Enabled Template.");                        break;                    case LoadFormat.FlatOpc:                        Console.WriteLine("\tFlat OPC document.");                        break;                    case LoadFormat.Rtf:                        Console.WriteLine("\tRTF format.");                        break;                    case LoadFormat.WordML:                        Console.WriteLine("\tMicrosoft Word 2003 WordprocessingML format.");                        break;                    case LoadFormat.Html:                        Console.WriteLine("\tHTML format.");                        break;                    case LoadFormat.Mhtml:                        Console.WriteLine("\tMHTML (Web archive) format.");                        break;                    case LoadFormat.Odt:                        Console.WriteLine("\tOpenDocument Text.");                        break;                    case LoadFormat.Ott:                        Console.WriteLine("\tOpenDocument Text Template.");                        break;                    case LoadFormat.DocPreWord60:                        Console.WriteLine("\tMS Word 6 or Word 95 format.");                        break;                    case LoadFormat.Unknown:                    default:                        Console.WriteLine("\tUnknown format.");                        break;                }                // ExEnd:DetectFileFormat                 // Now copy the document into the appropriate folder.                if (info.IsEncrypted)                {                    Console.WriteLine("\tAn encrypted document.");                    File.Copy(fileName, Path.Combine(encryptedDir, nameOnly), true);                }                else                {                    switch (info.LoadFormat)                    {                        case LoadFormat.DocPreWord60:                            File.Copy(fileName, Path.Combine(pre97Dir, nameOnly), true);                            break;                        case LoadFormat.Unknown:                            File.Copy(fileName, Path.Combine(unknownDir, nameOnly), true);                            break;                        default:                            File.Copy(fileName, Path.Combine(supportedDir, nameOnly), true);                            break;                    }                }            }            // ExEnd:CheckFormatCompatibility            Console.WriteLine("\nChecked the format of all documents successfully.");        }    }}       复制代码

                   

                             为你推荐:Aspose专题 - Aspose最新资源合集


转载于:https://juejin.im/post/5c90998c5188252d876e42d5

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值