【问题标题】:Unable to read a byte array (created from a .docx file) into a Doc object using ABCPDF无法使用 ABCPDF 将字节数组(从 .docx 文件创建)读入 Doc 对象
【发布时间】:2012-11-30 07:16:06
【问题描述】:

我正在检索 .docx 文件作为字节数组。然后,我尝试使用所述字节数组作为数据参数调用 Doc 的 read() 函数,但出现无法识别的文件扩展名错误。

我使用以下 (c#) 代码检索字节数组:

    WebClient testWc = new WebClient();
    testWc.Credentials = CredentialCache.DefaultCredentials;
    byte[] data = testWc.DownloadData("http://localhost/Lists/incidents/Attachments/1/Testdocnospaces.docx");

如果此时我将字节数组输出为 .docx 文件,我的程序将正确地允许我打开或保存文件。出于这个原因,我相信字节数组已被正确检索。这是我输出 .docx 文件的意思示例:

    Response.ClearHeaders();
    Response.Clear();
    Response.AppendHeader("Content-Disposition", "attachment;Filename=test.docx");
    Response.BinaryWrite(data);
    Response.Flush();
    Response.End();

但是,如果我尝试将字节数组读入 Doc,如下所示:

    Doc doc = new Doc();
    XReadOptions xr = new XReadOptions();
    xr.ReadModule = ReadModuleType.MSOffice;
    doc.Read(data, xr);

我的程序将在所述代码的最后一行出错,抛出以下内容:“FileExtension '' is invalid for ReadModuleType.MSOffice.”

Doc.Read() 函数似乎正在查找一个空字符串,它通常会查找文件类型。 另外,我确实在这台机器上安装了 Office 2007。

【问题讨论】:

  • 有完全相同的问题。在我看来,好像 doc.Read() 方法仅在直接从文件中读取时才正确支持 XReadOptions 对象(在这种情况下,它会检查给定的文件名参数以获取有效的扩展名)。实施中存在相当不便的缺陷。

标签: c# .net .net-3.5 abcpdf


【解决方案1】:

如果您知道文件字节的文件扩展名(您应该知道),您可以通过以下方式解决您的问题:

Doc doc = new Doc();
string extension = Path.GetExtension("your file name/path").Substring(1).ToUpper();
XReadOptions opts = new XReadOptions();
opts.FileExtension = extension;
doc.Read(fileBytes, opts);

这种方法对我有用。当您提供正确的文件扩展名时,您不需要设置 XReadOptions 对象的 ReadModule 属性。 ToUpper() 不是强制性的。

【讨论】:

  • 优秀。我正在使用 ABCPDF 版本 8,并且设置 XReadOptions 对象的“FileExtension”属性有效(我完全被 ABCPDF 的关于 ReadModule 属性的文档误导,并且设置“xr.ReadModule = ReadModuleType.MSOffice”从未奏效)。
猜你喜欢
  • 2022-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 2011-04-11
相关资源
最近更新 更多