I have the whole MS Word file itself saved into a byte array.A want to load it the way I would if it was on file system but with the minimal use of Microsoft.Office.Interop.Word because it is very slow when it gets the the .Open(args[])
part.
asked Aug 12, 2013 at 10:22
mathinvalidnikmathinvalidnik
1,53610 gold badges35 silver badges56 bronze badges
6
Try this….
byte[] bte = File.ReadAllBytes("E:\test.doc"); // Put the Reading file
File.WriteAllBytes(@"E:\test1.doc", bte); // Same contents you will get in byte[] and that will be save here
answered Aug 12, 2013 at 10:33
1
There is no supported way to do it right off-the-bat using Interop.Word, as there are no methods supporting byte arrays.
As a viable workaround you can use a temporary file in the following way:
// byte[] fileBytes = getFileBytesFromDB();
var tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileBytes);
Application app = new word.Application();
Document doc = app.Documents.Open(filePath);
// .. do your stuff here ...
doc.Close();
app.Quit();
byte[] newFileBytes = File.ReadAllBytes(tmpFile);
File.Delete(tmpFile);
Fore additional info, read this post on my blog.
answered May 3, 2017 at 14:16
DarksealDarkseal
8,9838 gold badges78 silver badges110 bronze badges
1
The method public static byte[] ReadAllBytes(
returns all the file information into a byte array. You dont have to worry about the stream, as the MSDN documentation says:
string path
)
«Given a file path, this method opens the file, reads the contents of the file into a byte array, and then closes the file.»
Check out this link if you want more information
answered Aug 12, 2013 at 11:59
Table of Contents
- Introducing Interop.Word
- Working with the Document
- Find and Replace Text
- Find and replace Bookmarks
- Convert a DOC / DOCX file to PDF
- Export a DOC / DOCX file into a PDF
- From a Byte Array
If you’re working with ASP.NET C# and you need to open, edit or otherwise access a Microsoft Word DOC or DOCX file, you can easily do that using the Microsoft.Office.Interop.Word library package. This post explains how to do so: you might find it useful in case you need to perform such task or whenever you want to read some insights regarding the process.
Introducing Interop.Word
To access the namespace from your ASP.NET project you have two main choices:
- Install the official Microsoft Office primary interop assemblies (PIAs) package on your machine by downloading and executing the runtime installer, then manually add a Reference to the Microsoft.Office.Interop.Word.dll file.
- Install the appropriate NuGet package within Visual Studio using the Package Manager Console.
Needless to say, you should really go for the second option, but we’ll leave that to you.
Working with the Document
As soon as you have the namespace available, you can do the following:
// NS alias to avoid writing the required namespace all the time using word = Microsoft.Office.Interop.Word; // […] Application app = new word.Application(); Document doc = app.Documents.Open(filePath); |
Once you have the app and the doc objects you can perform a lot of editing task, such as:
Find and Replace Text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
var textToFind = «any source text»; var textToReplace = «any replacement text»; var matchCase = true; var matchWholeWord = true; var matchWildcards = false; var matchSoundsLike = false; var matchAllWordForms = false; var forward = true; var wrap = 1; var format = false; var replace = 2; app.Selection.Find.Execute( textToFind, matchCase, matchWholeWord, matchWildcards, matchSoundsLike, matchAllWordForms, forward, wrap, format, textToReplace, replace); |
Find and replace Bookmarks
var bookmarkName = «anyName»; var bookmarkNewValue = «anyValue»; if (doc.Bookmarks.Exists(bookmarkName)) { doc.Bookmarks[bookmarkName].Select(); app.Selection.TypeText(bookmarkNewValue); } |
Convert a DOC / DOCX file to PDF
Surprisingly enough, we can even do that with an one-liner thanks to the native «Save As PDF…» feature introduced with Office 2010.
doc.SaveAs2(«path-to-pdf-file.pdf», word.WdSaveFormat.wdFormatPDF); |
Export a DOC / DOCX file into a PDF
This one is almost identical to the previous one in terms of results.
doc.ExportAsFixedFormat(tmpFile, WdExportFormat.wdExportFormatPDF); |
… and so on.
For additional info regarding word-to-pdf conversion, you can also read this dedicated post: otherwise, keep reading.
What if you have the DOC or DOCX file stored outside the FileSystem, such as in blob-format within a Database? If that’s the case you need to use a temporary file, because most Office Interop methods do not support working with byte arrays, streams and so on.
Here’s a decent workaround you can use:
// byte[] fileBytes = getFileBytesFromDB(); var tmpFile = Path.GetTempFileName(); File.WriteAllBytes(tmpFile, fileBytes); Application app = new word.Application(); Document doc = app.Documents.Open(filePath); // .. do your stuff here … doc.Close(); app.Quit(); byte[] newFileBytes = File.ReadAllBytes(tmpFile); File.Delete(tmpFile); |
You might notice that we used the
Close()
method in order to close (and thus save) the file. In case you wan’t to save your changes to the DOC / DOCX file you opened, you need to explicitly say it by adding the
WdSaveOptions.wdDoNotSaveChanges
object parameter in the following way:
doc.Close(word.WdSaveOptions.wdDoNotSaveChanges); |
IMPORTANT: Do not underestimate the call to
app.Quit()
! If you don’t do that, the MS Word instance will be left open on your server (see this thread on StackOverflow for more info on that issue). If you want to be sure to avoid such dreadful scenario entirely you should strengthen the given implementation adding a try/catch fallback strategy such as the follow:
Application app = null; Document doc = null; try { app = new word.Application(); doc = Document doc = app.Documents.Open(filePath); // .. do your stuff here … doc.Close(); app.Quit(); } catch (Exception e) { if (doc != null) doc.Close(); if (app != null) app.Quit(); } |
Unfortunately these objects don’t implement IDisposable, otherwise it would’ve been even easier.
That’s pretty much it: happy coding!
- Remove From My Forums
-
Question
-
User-483706746 posted
Hi all,
I am using a WebService in ASP.Net 2.0 to retrieve Data in XTHML format. I want to put this data in a Word Document and send this document to the client. Till now, I just used Response.AddHeader and set the type to «Application/MSWord». It was a plain text
file, but because of the extension .doc, the user coult opened the document in word. And Word is able to show HTML Documents, so everything worked fine.But now, I have to put images in that document. So I can’t use the HTML File approach anymore. So I have used the Word Interop to create a word document. Afterwards I put the XHTML Resopnse into the word document via the
Selection.InsertXML method. Now it is time to send this word document to the client, without saving it to disk. What I need is to convert the word document to a byte array or a memory stream, but I couldn’t find out how.So can you tell me how I can convert a Word Document in memory to a byte array or to a memroyStream?
Thanks for help
Answers
-
User1268090313 posted
I am using a WebService in ASP.Net 2.0 to retrieve Data in XTHML format. I want to put this data in a Word Document and send this document to the client. Till now, I just used Response.AddHeader and set the type to «Application/MSWord». It was a plain text
file, but because of the extension .doc, the user coult opened the document in word. And Word is able to show HTML Documents, so everything worked fine.But now, I have to put images in that document. So I can’t use the HTML File approach anymore. So I have used the Word Interop to create a word document. Afterwards I put the XHTML Resopnse into the word document via the
Selection.InsertXML method. Now it is time to send this word document to the client, without saving it to disk. What I need is to convert the word document to a byte array or a memory stream, but I couldn’t find out how.So can you tell me how I can convert a Word Document in memory to a byte array or to a memroyStream?
Hi koraykazgan,
From your description, I understand that there is a .Net XML Web Service which creates a word document
file in a memory, and you want to response the word file to the client browser in a web form page.
Please feel free to correct me if I have misunderstood your concern.
I am not exact sure the type and format of the word memory file; however, if the type of the memory file
is an Object type, perhaps you can try the following code :
Object obj;
byte [] buffer = (byte[])obj;
MemoryStream ms = new MemoryStream(buffer);And I do not think that put a file in the memory is an efficient way in the web server side, I want to
give you a suggestion that create the word document file in the disk, and then output the file like:
Response.WriteFile(file_path);Hope this Information is helpful!
Xun-
Marked as answer by
Thursday, October 7, 2021 12:00 AM
-
Marked as answer by
I have a sequence of bytes as follows (Word format) exported from crystal report
Stream stream = cryRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows);
I would like to convert that stream to a PDF stream to be stored in a varbinary column in sql server database.
There is a barcode in the document and i am unable to embed it when I export to pdf. Exporting to word works though so I want to try from Word to PDF
Can anyone assist me in getting this PDF byte[]?
asked Jan 14, 2015 at 15:48
5
You can do that using the Microsoft.Office.Interop.Word NuGet Package. Once you added it on your application you can flush your Byte Array to a temporary file, then open the temp file with Interop.Word, work with it, save the result and read the result back into a Byte Array.
Here’s a sample code snippet that does just that:
// byte[] fileBytes = getFileBytesFromDB();
var tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileBytes);
Application app = new word.Application();
Document doc = app.Documents.Open(filePath);
// Save DOCX into a PDF
var pdfPath = "path-to-pdf-file.pdf";
doc.SaveAs2(pdfPath, word.WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit(); // VERY IMPORTANT: do this to close the MS Word instance
byte[] pdfFileBytes = File.ReadAllBytes(pdfPath);
File.Delete(tmpFile);
For more info regarding the topic you can also read this post on my blog.
answered Apr 27, 2017 at 17:07
DarksealDarkseal
8,9838 gold badges78 silver badges110 bronze badges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function byteArrayToWordArray(ba) { | |
var wa = [], | |
i; | |
for (i = 0; i < ba.length; i++) { | |
wa[(i / 4) | 0] |= ba[i] << (24 — 8 * i); | |
} | |
return CryptoJS.lib.WordArray.create(wa, ba.length); | |
} | |
function wordToByteArray(word, length) { | |
var ba = [], | |
i, | |
xFF = 0xFF; | |
if (length > 0) | |
ba.push(word >>> 24); | |
if (length > 1) | |
ba.push((word >>> 16) & xFF); | |
if (length > 2) | |
ba.push((word >>> 8) & xFF); | |
if (length > 3) | |
ba.push(word & xFF); | |
return ba; | |
} | |
function wordArrayToByteArray(wordArray, length) { | |
if (wordArray.hasOwnProperty(«sigBytes») && wordArray.hasOwnProperty(«words»)) { | |
length = wordArray.sigBytes; | |
wordArray = wordArray.words; | |
} | |
var result = [], | |
bytes, | |
i = 0; | |
while (length > 0) { | |
bytes = wordToByteArray(wordArray[i], Math.min(4, length)); | |
length -= bytes.length; | |
result.push(bytes); | |
i++; | |
} | |
return [].concat.apply([], result); | |
} |
This function block allows you to convert a variable WORD in two variables BYTE. The 8 high bits of In will be transferred to the operand MSB, low 8 bits will be transferred to the operand LSB.
Function lock
CODESYS: Not available
LogicLab: eLLabUtyLib
In (WORD) Variable to convert.
MSB (BYTE) MSB of the input value.
LSB (BYTE) LSB of the input value.
Examples
How to use the examples.
In the example a variable WORD with value 16 # 1234 is transferred in two variables BYTE which will have value 16 # 12 and 16 # 34. In the example in language ST it is highlighted how the same operation is much simpler by writing it directly with the operands of the language.
LogicLab (Ptp114)
PROGRAM ST_WordToByte VAR WData : WORD := 16#1234; (* Word data *) High : ARRAY[ 0..1 ] OF BYTE; (* MSB byte *) Low : ARRAY[ 0..1 ] OF BYTE; (* LSB byte *) WDec : WordToByte; (* Word decompress *) END_VAR // ***************************************************************************** // PROGRAM "ST_WordToByte" // ***************************************************************************** // This program shows the use of WordToByte function block. // ----------------------------------------------------------------------------- // ------------------------------------------------------------------------- // DECOMPRESS WORD // ------------------------------------------------------------------------- // Decompress word using the FB. WDec(In:=WData); High[0]:=WDec.MSB; //MSB byte Low[0]:=WDec.LSB; //LSB byte // ------------------------------------------------------------------------- // DECOMPRESS WORD // ------------------------------------------------------------------------- // The same operation as above executed directly using ST statements. High[1]:=TO_BYTE(WData/256); //MSB byte Low[1]:=TO_BYTE(WData); //LSB byte // [End of file]
Was this article helpful?
Чтобы реализовать чтение и запись файлов Word на сервере Sql, вам необходимо добавить столбцы типа изображения в таблицу, к которой будет осуществляться доступ. Пример структуры таблицы:
CREATE TABLE CONTRACTS ( ID VARCHAR (50), CONTRACT_FILE IMAGE);
Чтобы сохранить файл Word в поле CONTRACT_FILE базы данных, вам необходимо преобразовать файл в массив байтов. Конкретный код выглядит следующим образом:
Преобразовать файл в байтовый массив /// <summary> Преобразовать файл в байты /// </summary> /// <param name = "fileName"> </param> /// <returns> </summary> возвращает> общедоступный статический байт [] File2Bytes (строка fileName) {FileStream fs = new FileStream (fileName, FileMode.OpenOrCreate, FileAccess.Read); FileDatas = новый байт [fs.Length]; FileDatas = новый байт [fs.Length]; , System.Convert.ToInt32 (fs.Length)); fs.Close (); return fileDatas;}
Затем сохраните преобразованный byte [] в соответствующее поле базы данных:
Сохраните файл в базе данных /// <summary> Обновите файл контракта /// </summary> /// <param name = "id"> </param> /// <param name = "fileBytes"> </param> /// <returns> </returns> public bool UpdateContractFile (string id, byte [] fileBytes) {string sql = "UPDATE CONTRACTS SET [email protected]_FILE WHERE ID[email protected]ID"; using (SqlConnection conn = new SqlConnection(this.m_DataAccess.ConnectString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandText = sql; cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@CONTRACT_FILE", SqlDbType.Image)); cmd.Parameters["@CONTRACT_FILE"].Value = fileBytes; cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar)); cmd.Parameters["@ID"].Value = id; return cmd.ExecuteNonQuery() > 0 ? true : false; } } }
Чтобы прочитать файл Word, хранящийся в базе данных, вам необходимо преобразовать поле Тип изображения в байты []. Конкретный код выглядит следующим образом:
Получить массив байтов файла по идентификатору /// <summary> Получить документы контракта /// </summary> /// <param name = "id"> </param> /// <returns> </returns> public byte [] GetContractFile (идентификатор строки) {строка sql = "SELECT CONTRACT_FILE FROM CONTRACTS WHERE ID='{0}'"; sql = string.Format(sql, id); object contractFile; contractFile = this.m_DataAccess.ExecuteScalar(sql); if (contractFile == null) { return new byte[0]; } else { return (byte[])contractFile; } }
После получения байта [] файла сохраните файл как файл Word с помощью операций с файловым потоком. Конкретный код выглядит следующим образом:
Массив byte [] хранится в виде файла Word byte [] fileBytes = this.m_ContractsBusiness.GetContractFile (id); if (fileBytes.Length == 0) {XMessageBox.ShowError («Файл контракта не найден!»); Return;} SaveFileDialog sfd = new SaveFileDialog (); sfd.Filter = "Файл Word (* .doc) | * .doc"; if (sfd.ShowDialog () == System.Windows.Forms.DialogResult.OK) {попробуйте {string saveFileName = sfd.FileName; int arraysize = new int (); // обратите внимание, что это arrayize = fileBytes.GetUpperBound (0); FileStream fs = new FileStream (saveFileName, FileMode.OpenOrCreate, FileAccess.Write); fs.Write (fileBytes, 0, arrayize); fs.Close (); fs.Close (); if (XMessageBox.ShowQuestion («Загрузка файла прошла успешно, открыть файл немедленно?») == s System. sult.Yes) {Process.Start (saveFileName);}} catch (Exception ex) {XMessageBox.ShowError ("скачанный файл не работает! ");}
Давайте поделимся учебником моего учителя по искусственному интеллекту. С нуля! Легко понять! Смешно и с юмором! Также принесите желтые анекдоты! Надеюсь, вы тоже присоединитесь к нашей команде искусственного интеллекта!https://blog.csdn.net/jiangjunshow