Unable to read a byte array (created from a .docx file) into a Doc object using ABCPDF

.net, .net-3.5, abcpdf, c#

Solution

If you know the file extension of your file bytes (which you should) you can solve your problem by:

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);

This approach worked for me. When you provide correct file extension you won't need to set ReadModule property of your XReadOptions object. ToUpper() is not mandatory.

Problem

I am retrieving a .docx file as a byte array. I am then trying to call the Doc’s read() function with said byte array as the data parameter but I am getting an unrecognized file extension error. I retrieve the byte array with the following (c#) code: ``` WebClient testWc = new WebClient(); testWc.Credentials = CredentialCache.DefaultCredentials; byte[] data = testWc.DownloadData("http://localhost/Lists/incidents/Attachments/1/Testdocnospaces.docx"); ``` IF at this point I output the byte array as a .docx file, my program will correctly allow me to open or save the file. For this reason, I believe the byte array has been retrieved correctly. Here is a sample of what I mean by outputting a .docx file: ``` Response.ClearHeaders(); Response.Clear(); Response.AppendHeader("Content-Disposition", "attachment;Filename=test.docx"); Response.BinaryWrite(data); Response.Flush(); Response.End(); ``` However, if I try to read the byte array into a Doc like so: ``` Doc doc = new Doc(); XReadOptions xr = new XReadOptions(); xr.ReadModule = ReadModuleType.MSOffice; doc.Read(data, xr); ``` My program will error out at the last line of said code, throwing the following: “FileExtension '' was invalid for ReadModuleType.MSOffice.” The Doc.Read() function seems to be finding an empty string where it would typically be finding the file type. Also, I do have Office 2007 installed on this machine.

Original source