To edit MS office files the best approach Available after Office 2007 onwards (when doc files became docx and ppt became pptx and so on) is to use Open Office SDK.
Initially editing doc files required MS Word to be installed on a particular machine but with Open XML format it is not the case. To better Understand how these files work now you can change the extension of the Word file to Zip instead of docx and unzip it. Explore through the files and folders available in the folder and you will get a fair idea about the scenario.
To begin with the demo here.
Say we have a requirement to Add Header to a Word Document or Say Replace the Header of a Word Document. For this requirement we already have a Code in SDK which can help us Out.
For that You can Search “How to: Replace the Header in a Word Processing Document” or go to given Hyperlink which has same code.
http://msdn.microsoft.com/en-us/library/cc546917.aspx
Create a Console Application and Add given References and Method to the application.
1 |
<span>using</span> System.Collections.Generic;<br><span>using</span> System.Linq;<br><span>using </span>System.Text;<br><span>using</span> DocumentFormat.OpenXml.Packaging; <span><br>using</span> DocumentFormat.OpenXml.Wordprocessing;<br><br><br><span><br>Reference for last two namespaces is in "DocumentFormat.OpenXml.dll" which can be found at "C:Program Files (x86)Open XML SDKV2.0lib" after installation of SDK<br><br><br>After That You'll need to add reference to "WindowsBase" which is in "C:Program Files (x86)Reference AssembliesMicrosoftFrameworkv3.0WindowsBase.dll"<br>or can be simply found in .Net tab After you click for adding references.<br><br><br>Now Add the Given method to the class<br><br><br><br><br><span> public static void AddHeaderFromTo(string filepathTo, string header)</span><br><span> {<br><br></span><span> // Replace header in target document with header of source document.</span><br><span> using (WordprocessingDocument</span><br><span> wdDoc = WordprocessingDocument.Open(filepathTo, true))</span><br><span> {</span><br><span> MainDocumentPart mainPart = wdDoc.MainDocumentPart;</span><br><br><span> // Delete the existing header part.</span><br><span> mainPart.DeleteParts(mainPart.HeaderParts);</span><br><br><span> // Create a new header part.</span><br><span> var headerPart = mainPart.AddNewPart();</span><br><br><span> // Get Id of the headerPart.</span><br><span> string rId = mainPart.GetIdOfPart(headerPart);</span><br><br><br><span> string test = "" +</span><br><span> "" + header + " "; </span><br><br><span> // convert string to stream </span><br><span> byte[] byteArray = Encoding.ASCII.GetBytes( test ); </span><br><span> Stream stream = new MemoryStream( byteArray ); </span><br><span> headerPart.FeedData(stream);</span><br><span> </span><br><span> // Get SectionProperties and Replace HeaderReference with new Id.</span><br><span> var sectPrs = mainPart.Document.Body.Elements();</span><br><span> <br> foreach (var sectPr in sectPrs)</span><br><span> {</span><br><span> // Delete existing references to headers.</span><br><span> sectPr.RemoveAllChildren();</span><br><br><span> // Create the new header reference node.</span><br><span> sectPr.PrependChild(new HeaderReference() { Id = rId });</span><br><span> }</span><br><span> }</span><br><span> }</span><br><br>and Call Method in Main<br><br></span> |
static void Main(string[] args)
{
AddHeaderFromTo(@”D:File.docx”, “This is The Header”);
}
Now here in the method we are passing file “File.docx” and Header “This is Header”.
This will add header to the file.
PS: This code will give exception if the file is not having any data or it is open. Do proper Exception handling for that.
For further reference you can go through sample codes provided by Microsoft.
Sample Code