参考:
http://www.codeproject.com/useritems/Office_To_Text.asp
http://channel9.msdn.com/wiki/default.aspx/Channel9.DesktopSearchIFilters
http://www.seekafile.org/plug-in-system.html
一个源码学习:
该源码使用了注册系统自带的DLL来处把其不同格式的文件流转换在文本格式,而不需要安装办公软件.详情请看微软官方说明第二个网址.
下面给出一段使用的代码:
String txt=Seekafile.Server.IFilter.DefaultParser. Extract(filepath);
只要把文件的路径传递给它就可以反加该文件里的文本数据,如果是不支持格式的文件也不会引起异常,只是返回了空字符.调用下面静态方法就要以知道是否支持该文件的转换了.
Bool isParseable= Seekafile.Server.IFilter.DefaultParser. IsParseable(filepath)
出现的问题:当PDF格式的文件大于1M时,其返回值是空字符,如果是小于1M就没问题,可以返回文件中的文本字符,如果各位能发现问题所在的话请回复哦.
请教: 不知道那位用过Interop.Word.dll来调用接口来,提取WORD文档中的字符信息.
源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
Copyright ?2006 Annpoint, s.r.o.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
http://www.apache.org/licenses/LICENSE-2.0
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
-------------------------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NOTE: Reuse requires the following acknowledgement (see also NOTICE):
This product includes Seekafile Server (http://www.seekafile.org)
developed by Annpoint, s.r.o.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
using System.Runtime.InteropServices;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Seekafile.Server.IFilter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
[Flags]
public enum IFILTER_INIT : uint
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
NONE = 0,
CANON_PARAGRAPHS = 1,
HARD_LINE_BREAKS = 2,
CANON_HYPHENS = 4,
CANON_SPACES = 8,
APPLY_INDEX_ATTRIBUTES = 16,
APPLY_CRAWL_ATTRIBUTES = 256,
APPLY_OTHER_ATTRIBUTES = 32,
INDEXING_ONLY = 64,
SEARCH_LINKS = 128,
FILTER_OWNED_VALUE_OK = 512
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public enum CHUNK_BREAKTYPE
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CHUNK_NO_BREAK = 0,
CHUNK_EOW = 1,
CHUNK_EOS = 2,
CHUNK_EOP = 3,
CHUNK_EOC = 4
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Flags]
public enum CHUNKSTATE
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CHUNK_TEXT = 0x1,
CHUNK_VALUE = 0x2,
CHUNK_FILTER_OWNED_VALUE = 0x4
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[StructLayout(LayoutKind.Sequential)]
public struct PROPSPEC
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public uint ulKind;
public uint propid;
public IntPtr lpwstr;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[StructLayout(LayoutKind.Sequential)]
public struct FULLPROPSPEC
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Guid guidPropSet;
public PROPSPEC psProperty;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[StructLayout(LayoutKind.Sequential)]
public struct STAT_CHUNK
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public uint idChunk;
[MarshalAs(UnmanagedType.U4)] public CHUNK_BREAKTYPE breakType;
[MarshalAs(UnmanagedType.U4)] public CHUNKSTATE flags;
public uint locale;
[MarshalAs(UnmanagedType.Struct)] public FULLPROPSPEC attribute;
public uint idChunkSource;
public uint cwcStartSource;
public uint cwcLenSource;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[StructLayout(LayoutKind.Sequential)]
public struct FILTERREGION
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public uint idChunk;
public uint cwcStart;
public uint cwcExtent;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[ComImport]
[Guid("89BCB740-6119-101A-BCB7-00DD010655AF")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IFilter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[PreserveSig]
int Init([MarshalAs(UnmanagedType.U4)] IFILTER_INIT grfFlags, uint cAttributes, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] FULLPROPSPEC[] aAttributes, ref uint pdwFlags);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[PreserveSig]
int GetChunk(out STAT_CHUNK pStat);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[PreserveSig]
int GetText(ref uint pcwcBuffer, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buffer);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void GetValue(ref UIntPtr ppPropValue);
void BindRegion([MarshalAs(UnmanagedType.Struct)] FILTERREGION origPos, ref Guid riid, ref UIntPtr ppunk);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[ComImport]
[Guid("f07f3920-7b8c-11cf-9be8-00aa004b9986")]
public class CFilter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class IFilterConstants
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public const uint PID_STG_DIRECTORY = 0x00000002;
public const uint PID_STG_CLASSID = 0x00000003;
public const uint PID_STG_STORAGETYPE = 0x00000004;
public const uint PID_STG_VOLUME_ID = 0x00000005;
public const uint PID_STG_PARENT_WORKID = 0x00000006;
public const uint PID_STG_SECONDARYSTORE = 0x00000007;
public const uint PID_STG_FILEINDEX = 0x00000008;
public const uint PID_STG_LASTCHANGEUSN = 0x00000009;
public const uint PID_STG_NAME = 0x0000000a;
public const uint PID_STG_PATH = 0x0000000b;
public const uint PID_STG_SIZE = 0x0000000c;
public const uint PID_STG_ATTRIBUTES = 0x0000000d;
public const uint PID_STG_WRITETIME = 0x0000000e;
public const uint PID_STG_CREATETIME = 0x0000000f;
public const uint PID_STG_ACCESSTIME = 0x00000010;
public const uint PID_STG_CHANGETIME = 0x00000011;
public const uint PID_STG_CONTENTS = 0x00000013;
public const uint PID_STG_SHORTNAME = 0x00000014;
public const int FILTER_E_END_OF_CHUNKS = (unchecked((int) 0x80041700));
public const int FILTER_E_NO_MORE_TEXT = (unchecked((int) 0x80041701));
public const int FILTER_E_NO_MORE_VALUES = (unchecked((int) 0x80041702));
public const int FILTER_E_NO_TEXT = (unchecked((int) 0x80041705));
public const int FILTER_E_NO_VALUES = (unchecked((int) 0x80041706));
public const int FILTER_S_LAST_TEXT = (unchecked((int) 0x00041709));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// IFilter return codes
///
public enum IFilterReturnCodes : uint
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// Success
///
S_OK = 0,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// The function was denied access to the filter file.
///
E_ACCESSDENIED = 0x80070005,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// The function encountered an invalid handle, probably due to a low-memory situation.
///
E_HANDLE = 0x80070006,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// The function received an invalid parameter.
///
E_INVALIDARG = 0x80070057,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// Out of memory
///
E_OUTOFMEMORY = 0x8007000E,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// Not implemented
///
E_NOTIMPL = 0x80004001,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// Unknown error
///
E_FAIL = 0x80000008,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// File not filtered due to password protection
///
FILTER_E_PASSWORD = 0x8004170B,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// The document format is not recognised by the filter
///
FILTER_E_UNKNOWNFORMAT = 0x8004170C,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// No text in current chunk
///
FILTER_E_NO_TEXT = 0x80041705,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// No more chunks of text available in object
///
FILTER_E_END_OF_CHUNKS = 0x80041700,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// No more text available in chunk
///
FILTER_E_NO_MORE_TEXT = 0x80041701,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// No more property values available in chunk
///
FILTER_E_NO_MORE_VALUES = 0x80041702,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// Unable to access object
///
FILTER_E_ACCESS = 0x80041703,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// Moniker doesn't cover entire region
///
FILTER_W_MONIKER_CLIPPED = 0x00041704,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// Unable to bind IFilter for embedded object
///
FILTER_E_EMBEDDING_UNAVAILABLE = 0x80041707,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// Unable to bind IFilter for linked object
///
FILTER_E_LINK_UNAVAILABLE = 0x80041708,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// This is the last text in the current chunk
///
FILTER_S_LAST_TEXT = 0x00041709,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// This is the last value in the current chunk
///
FILTER_S_LAST_VALUES = 0x0004170A
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Convenience class which provides static methods to extract text from files using installed IFilters
/// </summary>
public class DefaultParser
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public DefaultParser()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("query.dll", CharSet = CharSet.Unicode)]
private extern static int LoadIFilter(string pwcsPath, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, ref IFilter ppIUnk);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static IFilter loadIFilter(string filename)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
object outer = null;
IFilter filter = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Try to load the corresponding IFilter
int resultLoad = LoadIFilter(filename, outer, ref filter);
if (resultLoad != (int) IFilterReturnCodes.S_OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
return filter;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool IsParseable(string filename)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return loadIFilter(filename) != null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static string Extract(string path)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StringBuilder sb = new StringBuilder();
IFilter filter = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
filter = loadIFilter(path);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (filter == null)
return String.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
uint i = 0;
STAT_CHUNK ps = new STAT_CHUNK();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IFILTER_INIT iflags =
IFILTER_INIT.CANON_HYPHENS |
IFILTER_INIT.CANON_PARAGRAPHS |
IFILTER_INIT.CANON_SPACES |
IFILTER_INIT.APPLY_CRAWL_ATTRIBUTES |
IFILTER_INIT.APPLY_INDEX_ATTRIBUTES |
IFILTER_INIT.APPLY_OTHER_ATTRIBUTES |
IFILTER_INIT.HARD_LINE_BREAKS |
IFILTER_INIT.SEARCH_LINKS |
IFILTER_INIT.FILTER_OWNED_VALUE_OK;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (filter.Init(iflags, 0, null, ref i) != (int) IFilterReturnCodes.S_OK)
throw new Exception("Problem initializing an IFilter for:\n" + path + " \n\n");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (filter.GetChunk(out ps) == (int) (IFilterReturnCodes.S_OK))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ps.flags == CHUNKSTATE.CHUNK_TEXT)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IFilterReturnCodes scode = 0;
while (scode == IFilterReturnCodes.S_OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
uint pcwcBuffer = 65536;
System.Text.StringBuilder sbBuffer = new System.Text.StringBuilder((int)pcwcBuffer);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
scode = (IFilterReturnCodes) filter.GetText(ref pcwcBuffer, sbBuffer);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (pcwcBuffer > 0 && sbBuffer.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append(sbBuffer.ToString(0, (int) pcwcBuffer));
sb.Append(" "); // "\r\n"
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (filter != null)
Marshal.ReleaseComObject(filter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return sb.ToString();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)