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

1

6

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.

Unhesitating answered 12/12, 2012 at 14:47 Comment(1)
Having the exact same problem. Appears to me as if the doc.Read() method only properly supports the XReadOptions object when read directly from a file (In which case it checks the given filename parameter for a valid extension). Pretty inconvenient flaw in the implementation.Cheekpiece
B
8

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.

Blinker answered 18/2, 2013 at 3:2 Comment(1)
Eccellent. I am using ABCPDF version 8 and the setting the 'FileExtension' property of the XReadOptions object works (I was totally mislead by the ABCPDF's Documentation around ReadModule property and setting "xr.ReadModule = ReadModuleType.MSOffice" never worked).Vaporetto

© 2022 - 2024 — McMap. All rights reserved.