Hi,
I am using OpenXML to open an existing Word document and modify it. After modifications I want to save it to a database. When I am using a stream in the WordprocessingDocument.MainDocumentPart.Document.Save method, only the main document part is saved and not the whole docx file.
Is there a method available which saves the whole file to a stream or converts it to a byte array?
Thx,
Danny
John GlennPosted Mar 8, 2012, 3:55 AM
you can easily C# save Word to a stream (whole docx file) with this C# / VB.NET Word library.
Here is a sample C# code how to save it to ASP.NET output stream:
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Create a new empty document.
DocumentModel document = new DocumentModel();
// Add document content.
document.Sections.Add(new Section(document, new Paragraph(document, "Hello World!")));
// Microsoft Packaging API cannot write directly to Response.OutputStream.
// Therefore we use temporary MemoryStream.
using (MemoryStream documentStream = new MemoryStream())
{
document.Save(documentStream, SaveOptions.DocxDefault);
// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats";
Response.AddHeader("Content-Disposition", "attachment; filename=Document.docx");
documentStream.WriteTo(Response.OutputStream);
Response.End();
}
Dorababu MekaPosted Dec 14, 2011, 12:07 AM
http://www.exsilio.com/blog/post/2011/08/18/Using-Open-Xml-to-Export-MS-Word-Document.aspx