Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save modified WordprocessingDocument to new file

I'm attempting to open a Word document, change some text and then save the changes to a new document. I can get the first bit done using the code below but I can't figure out how to save the changes to a NEW document (specifying the path and file name).

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using DocumentFormat.OpenXml.Packaging; using System.IO;  namespace WordTest { class Program {     static void Main(string[] args)     {         string template = @"c:\data\hello.docx";         string documentText;          using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(template, true))         {             using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream()))             {                 documentText = reader.ReadToEnd();             }               documentText = documentText.Replace("##Name##", "Paul");             documentText = documentText.Replace("##Make##", "Samsung");              using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))             {                 writer.Write(documentText);             }         }       }     } } 

I'm a complete beginner at this, so forgive the basic question!

like image 766
Paul Avatar asked Jan 11 '12 11:01

Paul


2 Answers

If you use a MemoryStream you can save the changes to a new file like this:

byte[] byteArray = File.ReadAllBytes("c:\\data\\hello.docx"); using (MemoryStream stream = new MemoryStream()) {     stream.Write(byteArray, 0, (int)byteArray.Length);     using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true))     {        // Do work here     }     // Save the file with the new name     File.WriteAllBytes("C:\\data\\newFileName.docx", stream.ToArray());  } 
like image 156
amurra Avatar answered Sep 22 '22 17:09

amurra


In Open XML SDK 2.5:

    File.Copy(originalFilePath, modifiedFilePath);      using (var wordprocessingDocument = WordprocessingDocument.Open(modifiedFilePath, isEditable: true))     {         // Do changes here...     } 

wordprocessingDocument.AutoSave is true by default so Close and Dispose will save changes. wordprocessingDocument.Close is not needed explicitly because the using block will call it.

This approach doesn't require entire file content to be loaded into memory like in accepted answer. It isn't a problem for small files, but in my case I have to process more docx files with embedded xlsx and pdf content at the same time so the memory usage would be quite high.

like image 38
user3285954 Avatar answered Sep 22 '22 17:09

user3285954