0

plainText has encoded word document base64 string

byte[] buffer = Convert.FromBase64String(plainText);
using (Stream ms = new MemoryStream())
                    {
                        ms.Write(buffer, 0, (int)buffer.Length);
                    }

I tried to open word document using OpenXML SDK

WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true)

But it throws File contains corrupted data error.

I've also gone through MSDN link OpenXML SDK

I've created a word document using this byte[] buffer But what I'm trying to achieve is, read the content present in document without creating(saving) the word document. What am I missing ?

Pavithran R
  • 56
  • 2
  • 11
  • If you want to read it with interop I think you have to save it (maybe delete it right after?) – EpicKip Jun 20 '17 at 11:41
  • Hi @EpicKip thanks for the prompt response. I'm getting base64 string form web api, it's a word document encoded as base64 string. I'm trying to read the contents present in it. These all has to be done at client side so I cannot save. Is there any possible ways to achieve it ? – Pavithran R Jun 20 '17 at 11:45
  • I don't think its possible see: https://stackoverflow.com/questions/18184616/how-to-load-word-document-from-byte-array But I think you might be able to save at client if its in the temp appdata folder: `Path.GetTempPath();` – EpicKip Jun 20 '17 at 11:48
  • yeah @EpicKip I understood. But is there any workaround to read content from encoded base64 word document string ? at runtime ? because when I converted the base64 string to byte array and generated a word document. I can able to see the content. It would be a great help If you could find solution. – Pavithran R Jun 20 '17 at 11:58

1 Answers1

1

the most likely problem here is that you have not rewound the stream. Either set

ms.Position = 0;

or use the constructor that accepts a byte[], rather than calling .Write:

using (Stream ms = new MemoryStream(buffer)) {...}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I'm getting "file contains corrupted data" error @Gravell but If i save the byte array to file, I can able to see my content In my case, I shouldn't save. – Pavithran R Jun 20 '17 at 12:14
  • 1
    @Sai if the buffer contains the right contents *and* the stream is correctly rewound, then either `WordprocessingDocument` is fundamentally broken, or you are mistaken about one of the previous two assertions (it containing the right contents *and* is correctly rewound) – Marc Gravell Jun 20 '17 at 12:18
  • I guess buffer contains the right content, because when I generate word file using buffer(byte array). I can able to see my contents. But In my case I should not save the file. – Pavithran R Jun 20 '17 at 12:31
  • @Sai and did you rewind the stream? – Marc Gravell Jun 20 '17 at 12:34
  • I couldn't get you. Can you please tell what do you mean by rewind ?@Gravell – Pavithran R Jun 20 '17 at 12:40
  • @Sai the things I've demonstrated in this answer. I don't know how old you are, but if you remember audio / video tapes: when you call `Write(...)`, you are recording data but the tape remains *at the end*. To listen to what you just wrote (`Read(...)` from the stream), you need to go back to the start. – Marc Gravell Jun 20 '17 at 12:41
  • Sorry @Gravell I thought rewind is a technical term. Yea, I've already set the ms.position = 0; I'm getting base64 string from web api so my assumption is I'm getting entire document content, styles and references as base64 string. so It works only when I save it as word document. I don't know how should I proceed. – Pavithran R Jun 20 '17 at 12:49
  • @Sai if it works if you `File.WriteAllBytes` and load it as a file, but it doesn't work with the API, then there's a bug in the API. I would start by raising an issue - ideally with sample data and a minimal repro - on the project site, i.e. where that `WordprocessingDocument` type came from – Marc Gravell Jun 20 '17 at 12:53
  • Thanks @MarcGravell I rectified it. Web API content was corrupted. Now it's working. I just followed the same thing :) – Pavithran R Jun 20 '17 at 18:43