Aspose.Words for .NET 如何检测文件格式和检查格式兼容性?

 

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

 

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

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

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

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

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

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

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

输入文件类型
测试文件(XML).xml FlatOPC OOXML文档。
测试文件(WordML).xml Microsoft Word 2003 WordprocessingML文档。
测试文件(rtf).rtf 富文本格式文档。
测试文件(odt).odt OpenDocument文本格式(OpenOffice Writer)。
测试文件(MHTML).mhtml MHTML(Web档案)文档。
测试文件(HTML).html HTML文档。
测试文件(dotx).dotx Office Open XML WordprocessingML模板。
测试文件(dot).dot Microsoft Word 97 - 2003模板
测试文件(docx).docx 没有宏的Office Open XML WordprocessingML文档。
测试文件(docm).docm 有宏的Office Open XML WordprocessingML文档。
测试文件(doc).doc Microsoft Word 97 - 2003文档。

测试加密文档:

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

不支持的文件格式:

输入文件类型
测试文件(pre97).doc Microsoft Word 95文档。
测试文件(JPG).jpg JPEG图像文件。

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

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



 
  1 using System;
  2 using System.Collections;
  3 using System.IO;
  4 using Aspose.Words;
  5 using Aspose.Words.Tables;
  6 using System.Diagnostics;
  7 namespace Aspose.Words.Examples.CSharp.Loading_Saving
  8 {
  9     class CheckFormat
 10     {
 11         public static void Run()
 12         {
 13             // ExStart:CheckFormatCompatibility
 14             // The path to the documents directory.
 15             string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 16             string supportedDir = dataDir + "OutSupported";
 17             string unknownDir = dataDir + "OutUnknown";
 18             string encryptedDir = dataDir + "OutEncrypted";
 19             string pre97Dir = dataDir + "OutPre97";
 20             // Create the directories if they do not already exist
 21             if (Directory.Exists(supportedDir) == false)
 22                 Directory.CreateDirectory(supportedDir);
 23             if (Directory.Exists(unknownDir) == false)
 24                 Directory.CreateDirectory(unknownDir);
 25             if (Directory.Exists(encryptedDir) == false)
 26                 Directory.CreateDirectory(encryptedDir);
 27             if (Directory.Exists(pre97Dir) == false)
 28                 Directory.CreateDirectory(pre97Dir);
 29             // ExStart:GetListOfFilesInFolder
 30             string[] fileList = Directory.GetFiles(dataDir);
 31             // ExEnd:GetListOfFilesInFolder
 32             // Loop through all found files.
 33             foreach (string fileName in fileList)
 34             {
 35                 // Extract and display the file name without the path.
 36                 string nameOnly = Path.GetFileName(fileName);
 37                 Console.Write(nameOnly);
 38                 // ExStart:DetectFileFormat
 39                 // Check the file format and move the file to the appropriate folder.
 40                 FileFormatInfo info = FileFormatUtil.DetectFileFormat(fileName);
 41 
 42                 // Display the document type.
 43                 switch (info.LoadFormat)
 44                 {
 45                     case LoadFormat.Doc:
 46 
 47                         Console.WriteLine("\tMicrosoft Word 97-2003 document.");
 48 
 49                         break;
 50                     case LoadFormat.Dot:
 51 
 52                         Console.WriteLine("\tMicrosoft Word 97-2003 template.");
 53 
 54                         break;
 55                     case LoadFormat.Docx:
 56 
 57                         Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Free Document.");
 58 
 59                         break;
 60                     case LoadFormat.Docm:
 61 
 62                         Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Enabled Document.");
 63 
 64                         break;
 65                     case LoadFormat.Dotx:
 66 
 67                         Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Free Template.");
 68 
 69                         break;
 70                     case LoadFormat.Dotm:
 71 
 72                         Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Enabled Template.");
 73 
 74                         break;
 75                     case LoadFormat.FlatOpc:
 76 
 77                         Console.WriteLine("\tFlat OPC document.");
 78 
 79                         break;
 80                     case LoadFormat.Rtf:
 81 
 82                         Console.WriteLine("\tRTF format.");
 83 
 84                         break;
 85                     case LoadFormat.WordML:
 86 
 87                         Console.WriteLine("\tMicrosoft Word 2003 WordprocessingML format.");
 88 
 89                         break;
 90                     case LoadFormat.Html:
 91 
 92                         Console.WriteLine("\tHTML format.");
 93 
 94                         break;
 95                     case LoadFormat.Mhtml:
 96 
 97                         Console.WriteLine("\tMHTML (Web archive) format.");
 98 
 99                         break;
100                     case LoadFormat.Odt:
101 
102                         Console.WriteLine("\tOpenDocument Text.");
103 
104                         break;
105                     case LoadFormat.Ott:
106 
107                         Console.WriteLine("\tOpenDocument Text Template.");
108 
109                         break;
110                     case LoadFormat.DocPreWord60:
111 
112                         Console.WriteLine("\tMS Word 6 or Word 95 format.");
113 
114                         break;
115                     case LoadFormat.Unknown:
116                     default:
117 
118                         Console.WriteLine("\tUnknown format.");
119 
120                         break;
121                 }
122                 // ExEnd:DetectFileFormat
123                 // Now copy the document into the appropriate folder.
124                 if (info.IsEncrypted)
125                 {
126                     Console.WriteLine("\tAn encrypted document.");
127                     File.Copy(fileName, Path.Combine(encryptedDir, nameOnly), true);
128                 }
129                 else
130                 {
131                     switch (info.LoadFormat)
132                     {
133 
134                         case LoadFormat.DocPreWord60:
135 
136                             File.Copy(fileName, Path.Combine(pre97Dir, nameOnly), true);
137 
138                             break;
139 
140                         case LoadFormat.Unknown:
141 
142                             File.Copy(fileName, Path.Combine(unknownDir, nameOnly), true);
143 
144                             break;
145 
146                         default:
147 
148                             File.Copy(fileName, Path.Combine(supportedDir, nameOnly), true);
149 
150                             break;
151                     }
152                 }
153             }
154             // ExEnd:CheckFormatCompatibility
155             Console.WriteLine("\nChecked the format of all documents successfully.");
156         }
157     }
158 }

 

 
posted @ 2020-08-31 10:31  特洛伊-Micro  阅读(416)  评论(0编辑  收藏  举报