none
Open XML SDK : How to convert open xml string to word document c# RRS feed

  • Question

  • Hello,

    I am reading one element from code and I am getting Open XML string as a result.

      byte[] binary = Convert.FromBase64String(template.Attributes["body"].ToString());
      string bodyContent = UnicodeEncoding.UTF8.GetString(binary);

    Resultant XML String:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <?mso-application progid="Word.Document"?>
    <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" 
    								xmlns:v="urn:schemas-microsoft-com:vml" 
    								xmlns:w10="urn:schemas-microsoft-com:office:word" 
    								xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" 
    								xmlns:aml="http://schemas.microsoft.com/aml/2001/core" 
    								xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" 
    								xmlns:o="urn:schemas-microsoft-com:office:office" 
    								xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
    								xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" 
    								w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">
    	<w:ignoreElements w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"/>
    	<o:DocumentProperties>
    		 <o:Title>Follow-up to Our Meeting</o:Title><o:Author>Microsoft Corporation</o:Author><o:LastAuthor>ASI User</o:LastAuthor><o:Revision>2</o:Revision><o:TotalTime>0</o:TotalTime><o:Created>2007-08-10T16:40:00Z</o:Created><o:LastSaved>2007-08-10T16:40:00Z</o:LastSaved><o:Pages>1</o:Pages><o:Words>178</o:Words><o:Characters>1019</o:Characters><o:Company>Microsoft Corporation</o:Company><o:Lines>8</o:Lines><o:Paragraphs>2</o:Paragraphs><o:CharactersWithSpaces>1195</o:CharactersWithSpaces><o:Version>11.8134</o:Version>
    	</o:DocumentProperties>
    	<w:fonts>
    		 <w:defaultFonts w:ascii="Times New Roman" w:fareast="SimSun" w:h-ansi="Times New Roman" w:cs="Times New Roman"/>
    		<w:font w:name="SimSun"><w:altName w:val="宋体"/><w:panose-1 w:val="02010600030101010101"/><w:charset w:val="86"/><w:family w:val="Auto"/><w:pitch w:val="variable"/><w:sig w:usb-0="00000003" w:usb-1="080E0000" w:usb-2="00000010" w:usb-3="00000000" w:csb-0="00040001" w:csb-1="00000000"/></w:font><w:font w:name="@SimSun"><w:panose-1 w:val="02010600030101010101"/><w:charset w:val="86"/><w:family w:val="Auto"/><w:pitch w:val="variable"/><w:sig w:usb-0="00000003" w:usb-1="080E0000" w:usb-2="00000010" w:usb-3="00000000" w:csb-0="00040001" w:csb-1="00000000"/></w:font>
    	</w:fonts>
    	<w:styles>
    		 <w:versionOfBuiltInStylenames w:val="4"/><w:latentStyles w:defLockedState="off" w:latentStyleCount="156"/><w:style w:type="paragraph" w:default="on" w:styleId="Normal"><w:name w:val="Normal"/><w:rPr><wx:font wx:val="Times New Roman"/><w:sz w:val="24"/><w:sz-cs w:val="24"/><w:lang w:val="EN-US" w:fareast="ZH-CN" w:bidi="AR-SA"/></w:rPr></w:style><w:style w:type="character" w:default="on" w:styleId="DefaultParagraphFont"><w:name w:val="Default Paragraph Font"/><w:semiHidden/></w:style><w:style w:type="table" w:default="on" w:styleId="TableNormal"><w:name w:val="Normal Table"/><wx:uiName wx:val="Table Normal"/><w:semiHidden/><w:rPr><wx:font wx:val="Times New Roman"/></w:rPr><w:tblPr><w:tblInd w:w="0" w:type="dxa"/><w:tblCellMar><w:top w:w="0" w:type="dxa"/><w:left w:w="108" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style><w:style w:type="list" w:default="on" w:styleId="NoList"><w:name w:val="No List"/><w:semiHidden/></w:style>
    	</w:styles>
    	<w:docPr>
    		 <w:view w:val="print"/><w:zoom w:percent="100"/><w:doNotEmbedSystemFonts/><w:attachedTemplate w:val=""/><w:defaultTabStop w:val="720"/><w:characterSpacingControl w:val="DontCompress"/><w:optimizeForBrowser/><w:validateAgainstSchema/><w:saveInvalidXML w:val="off"/><w:ignoreMixedContent w:val="off"/><w:alwaysShowPlaceholderText w:val="off"/><w:compat><w:breakWrappedTables/><w:snapToGridInCell/><w:wrapTextWithPunct/><w:useAsianBreakRules/><w:useWord2002TableStyleRules/><w:useFELayout/></w:compat>
    		<wsp:rsids>
    				<wsp:rsidRoot wsp:val="00453714"/><wsp:rsid wsp:val="00015B28"/><wsp:rsid wsp:val="000B1B9A"/><wsp:rsid wsp:val="000F4F6A"/><wsp:rsid wsp:val="0010045E"/><wsp:rsid wsp:val="0015095E"/><wsp:rsid wsp:val="0016480E"/><wsp:rsid wsp:val="001B029C"/><wsp:rsid wsp:val="00386D53"/><wsp:rsid wsp:val="003F1099"/><wsp:rsid wsp:val="00453714"/><wsp:rsid wsp:val="004C6C19"/><wsp:rsid wsp:val="004F592D"/><wsp:rsid wsp:val="00511DC8"/><wsp:rsid wsp:val="00583473"/><wsp:rsid wsp:val="005A28AE"/><wsp:rsid wsp:val="005C0D45"/><wsp:rsid wsp:val="005E0E9D"/><wsp:rsid wsp:val="00670858"/><wsp:rsid wsp:val="006C08B9"/><wsp:rsid wsp:val="006C5DCC"/><wsp:rsid wsp:val="007F7DB8"/><wsp:rsid wsp:val="0083137D"/><wsp:rsid wsp:val="009E707D"/><wsp:rsid wsp:val="00AA38F5"/><wsp:rsid wsp:val="00BC08A1"/><wsp:rsid wsp:val="00C0384D"/><wsp:rsid wsp:val="00DE6B6B"/><wsp:rsid wsp:val="00E23D4E"/><wsp:rsid wsp:val="00EA0411"/><wsp:rsid wsp:val="00EB12F6"/>
    			 </wsp:rsids>
    	</w:docPr>
    	<w:body>
    		 <wx:sect>
    				 <w:p wsp:rsidR="001B029C" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_First_Name" </w:instrText></w:r><w:r wsp:rsidR="00670858"><w:instrText>\f" "</w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_First_Name» </w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_Last_Name" </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_Last_Name»</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_Business_Unit" </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_Business_Unit»</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p>
    			 <w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"/><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="0083137D"><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> DATE  \@ "MMMM d, yyyy"  \* MERGEFORMAT </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="0010045E"><w:rPr><w:noProof/></w:rPr><w:t>August 10, 2007</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"/>
    			 <w:p wsp:rsidR="005E0E9D" wsp:rsidRDefault="00453714"><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> ADDRESSBLOCK \f "&lt;&lt;_FIRST0_&gt;&gt;&lt;&lt; _LAST0_&gt;&gt;&lt;&lt; _SUFFIX0_&gt;&gt;</w:instrText></w:r><w:r><w:cr/><w:instrText>&lt;&lt;_STREET1_</w:instrText></w:r><w:r><w:cr/><w:instrText>&gt;&gt;&lt;&lt;_STREET2_</w:instrText></w:r><w:r><w:cr/><w:instrText>&gt;&gt;&lt;&lt;_CITY_&gt;&gt;&lt;&lt;, _STATE_&gt;&gt;&lt;&lt; _POSTAL_&gt;&gt;&lt;&lt;</w:instrText></w:r><w:r><w:cr/><w:instrText>_COUNTRY_&gt;&gt;" \l 1033 \c 1 \e "United States" \d </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«AddressBlock»</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00453714" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00453714"/>
    			 <w:p wsp:rsidR="00015B28" wsp:rsidRDefault="00453714" wsp:rsidP="00386D53"><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> GREETINGLINE \f "&lt;&lt;_BEFORE_ Dear &gt;&gt;&lt;&lt;_TITLE0_&gt;&gt;&lt;&lt; _LAST0_&gt;&gt;
    &lt;&lt;_AFTER_ ,&gt;&gt;" \l 1033 \e "Dear Sir or Madam," </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«GreetingLine»</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00453714" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00453714" wsp:rsidP="00386D53"/><w:p wsp:rsidR="006C08B9" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="006C08B9" wsp:rsidP="006C08B9"><w:r wsp:rsidRPr="006C08B9"><w:t>I would like to thank you for taking the time to meet with us. We were delighted to have the opportunity to discuss in more depth your needs and how our business unit can help you to fulfill them. As discussed, we will send you a detailed proposal soon and I will be in tou</w:t></w:r><w:r wsp:rsidR="005C0D45"><w:t>ch to schedule a follow-up meeting</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:t> next month.</w:t></w:r></w:p>
    			 <w:p wsp:rsidR="006C08B9" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="006C08B9" wsp:rsidP="006C08B9"/><w:p wsp:rsidR="006C08B9" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="006C08B9" wsp:rsidP="006C08B9"><w:r wsp:rsidRPr="006C08B9"><w:t>We look forward to the opportunity to work with you.</w:t></w:r></w:p>
    			 <w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53" wsp:rsidP="00386D53"><w:pPr><w:ind w:left="720"/></w:pPr></w:p>
    			 <w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53" wsp:rsidP="00386D53"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> AUTOTEXTLIST  \s Closing \t "Right-click here to select from a list of standard closing salutations." \* MERGEFORMAT </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:t>Sincerely,</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53" wsp:rsidP="00386D53"><w:pPr><w:ind w:left="720"/></w:pPr></w:p>
    			 <w:p wsp:rsidR="001B029C" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="001B029C"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_First_Name" </w:instrText></w:r><w:r wsp:rsidR="00670858"><w:instrText>\f " "</w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_First_Name» </w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_Last_Name" </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_Last_Name»</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_Title" </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_Title»</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p><w:sectPr wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9">
    						<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:line-pitch="360"/></w:sectPr>
    		 </wx:sect>
    	</w:body>
    </w:wordDocument>

    Now, I want to convert this string in memorystream so that it can be read by WordProcessingDocument as below:

    using (MemoryStream stream = new MemoryStream())
      {
          stream.Write(binary, 0, (int)binary.Length);
            using (WordprocessingDocument wordDoc = wordprocessingDocument.Open(stream, true))
              {
                    File.WriteAllBytes("C:\\data\\newFileName.docx", stream.ToArray());
               }      
      }

    Main purpose of doing this is :

    1. I am reading one Mail Merge Template, which is in XML format.

    2. I want to read all mergefields from that XML string and then replace it's values.

    3. Finally, save that document as Word Document.

    I am not able to find a way using Open XML SDK to do this.

    Any suggestions?

    Thank you,

    Wednesday, July 1, 2015 12:45 PM

Answers

  • Hi MittalPatel

    If I'm reading the XML you've posted correctly, this is not Word Open XML, this is the older WordProcessingML that was an alternate file format for Word 2003 - ONLY. Later versions of Word can open such documents (and write to that file format) using a converter. But the Open XML SDK does not work with WordProcessingML.

    Here's how I recognized this fact:
    <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"

    Also, in a valid WordOpenXML file you won't see the document Properties, Fonts or styles written in the main body of the document.

    If you need to work with this file format then you should use standard XML tools to manipulate the XML.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 2, 2015 1:47 PM
    Moderator
  • Hi Mittal

    I'm sure it's possible to convert/transform Word 2003 WordProcessingML to Word Open XML, but I don't know The (best) way. My recommendation would be to ask in OpenXMLDeveloper.org as I imagine this question came up quite a bit in the transition from Word 2003 to 2007 (and later).

    What I can imagine would be a true Transform (xslt) to the WordOpenXML "flat-file" format. This can then be converted to the Package file format (*.docx). See:

    http://blogs.msdn.com/b/ericwhite/archive/2008/09/29/the-flat-opc-format.aspx

    Another possibility would be a third-party conversion tool, such as that from Aspose.

    Or, if SharePoint Server is available to you, you should be able to use the Word Automation Services to open the file and perform the conversion to a true docx.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 10, 2015 7:45 PM
    Moderator

All replies

  • Hi MittalPatel,

    To achieve your requirement, I suggest you do as below:

    1. Get all the field code elements in the document

    2. Get the MERGEFIELD field from all the fields

    3. Replace the MERGEFIELD text

    You could refer the link below for a demo code.

    # Open XML and MailMerge

    https://social.msdn.microsoft.com/Forums/office/en-US/aa375d39-d216-4d56-ab28-cbaca148bdda/open-xml-and-mailmerge

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 2, 2015 10:02 AM
  • Hi MittalPatel

    If I'm reading the XML you've posted correctly, this is not Word Open XML, this is the older WordProcessingML that was an alternate file format for Word 2003 - ONLY. Later versions of Word can open such documents (and write to that file format) using a converter. But the Open XML SDK does not work with WordProcessingML.

    Here's how I recognized this fact:
    <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"

    Also, in a valid WordOpenXML file you won't see the document Properties, Fonts or styles written in the main body of the document.

    If you need to work with this file format then you should use standard XML tools to manipulate the XML.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 2, 2015 1:47 PM
    Moderator
  • Hello Cindy,

    Thank you for your response.

    Yes you are right. I ran into problems now due to this old format.

    Do you have any idea how to convert docx file( contains merge fields) to XML format which can be read using Open xml sdk ?

    I have searched google for hours, but not able to find it.

    Please suggest.

    Thank you,

    Friday, July 10, 2015 2:34 PM
  • Hi Mittal

    I'm sure it's possible to convert/transform Word 2003 WordProcessingML to Word Open XML, but I don't know The (best) way. My recommendation would be to ask in OpenXMLDeveloper.org as I imagine this question came up quite a bit in the transition from Word 2003 to 2007 (and later).

    What I can imagine would be a true Transform (xslt) to the WordOpenXML "flat-file" format. This can then be converted to the Package file format (*.docx). See:

    http://blogs.msdn.com/b/ericwhite/archive/2008/09/29/the-flat-opc-format.aspx

    Another possibility would be a third-party conversion tool, such as that from Aspose.

    Or, if SharePoint Server is available to you, you should be able to use the Word Automation Services to open the file and perform the conversion to a true docx.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 10, 2015 7:45 PM
    Moderator
  • Hello Mittal .

    Did you find an answer for the question ?

    I'm dealing with similar issue - need to write conversion program to convert few thousands of

    documents that were created in Word 2003 WordProcessingML format ( XML files ) to Word 2013

    and that contain Custom Xml Markup ( which is not supported anymore by MS Word due to patent violation ).

    (I want to convert the xml nodes we used in these documents to content controls in Word 2013)

    Cannot load the docs with OpenXml .

    Cannot use Word either , since it removes these nodes when the document is saved ,

    Aspose can convert them , but also I can't access these nodes using Aspose 

    Monday, July 20, 2015 9:24 PM
  • Hello Alex,

    No, I have not find any easy solution to convert them in Word 2013. Even I had posted my question on OpenXMLDeveloper.org.

    Below is the work around which works fine for me.

    Steps are : 

    1. SaveData - To save WordProcessingML XML string to XML document first.

    2. FlatToOpc - To save XML document to Word

     string bodyContent = UnicodeEncoding.UTF8.GetString(binary);
                                SaveData(filepath + templatename + ".xml", binary);
                                XDocument doc;
                                doc = XDocument.Load(filepath + templatename + ".xml");
                                FlatToOpc(doc, filepath + templatename + ".docx");


    Here, bodyContent is my WordProcessingML string.

    SaveData function :

      #region Save data from binary to xml file
            /// <summary>
            /// Save data from binary to xml file
            /// </summary>
            /// <param name="FileName"></param>
            /// <param name="Data"></param>
            /// <returns></returns>
            private static bool SaveData(string FileName, byte[] Data)
            {
                BinaryWriter Writer = null;
                try
                {
                    // Create a new stream to write to the file
                    Writer = new BinaryWriter(System.IO.File.OpenWrite(FileName));
    
                    // Writer raw data                
                    Writer.Write(Data);
                    Writer.Flush();
                    Writer.Close();
                }
                catch
                {
                    //...
                    return false;
                }
    
                return true;
            }
            #endregion


    FlatToOpc function :

        #region Convert xml file to word doc
            /// <summary>
            /// Convert flat xml to opc
            /// </summary>
            /// <param name="doc"></param>
            /// <param name="docxPath"></param>
            public static void FlatToOpc(XDocument doc, string docxPath)
            {
                XNamespace pkg =
                    "http://schemas.microsoft.com/office/2006/xmlPackage";
                XNamespace rel =
                    "http://schemas.openxmlformats.org/package/2006/relationships";
    
                using (Package package = Package.Open(docxPath, FileMode.Create))
                {
                    // add all parts (but not relationships)
                    foreach (var xmlPart in doc.Root
                        .Elements()
                        .Where(p =>
                            (string)p.Attribute(pkg + "contentType") !=
                            "application/vnd.openxmlformats-package.relationships+xml"))
                    {
                        string name = (string)xmlPart.Attribute(pkg + "name");
                        string contentType = (string)xmlPart.Attribute(pkg + "contentType");
                        if (contentType.EndsWith("xml"))
                        {
                            Uri u = new Uri(name, UriKind.Relative);
                            PackagePart part = package.CreatePart(u, contentType,
                                CompressionOption.SuperFast);
                            using (Stream str = part.GetStream(FileMode.Create))
                            using (XmlWriter xmlWriter = XmlWriter.Create(str))
                                xmlPart.Element(pkg + "xmlData")
                                    .Elements()
                                    .First()
                                    .WriteTo(xmlWriter);
                        }
                        else
                        {
                            Uri u = new Uri(name, UriKind.Relative);
                            PackagePart part = package.CreatePart(u, contentType,
                                CompressionOption.SuperFast);
                            using (Stream str = part.GetStream(FileMode.Create))
                            using (BinaryWriter binaryWriter = new BinaryWriter(str))
                            {
                                string base64StringInChunks =
                                    (string)xmlPart.Element(pkg + "binaryData");
                                char[] base64CharArray = base64StringInChunks
                                    .Where(c => c != '\r' && c != '\n').ToArray();
                                byte[] byteArray =
                                    System.Convert.FromBase64CharArray(base64CharArray,
                                    0, base64CharArray.Length);
                                binaryWriter.Write(byteArray);
                            }
                        }
                    }
    
                    foreach (var xmlPart in doc.Root.Elements())
                    {
                        string name = (string)xmlPart.Attribute(pkg + "name");
                        string contentType = (string)xmlPart.Attribute(pkg + "contentType");
                        if (contentType ==
                            "application/vnd.openxmlformats-package.relationships+xml")
                        {
                            // add the package level relationships
                            if (name == "/_rels/.rels")
                            {
                                foreach (XElement xmlRel in
                                    xmlPart.Descendants(rel + "Relationship"))
                                {
                                    string id = (string)xmlRel.Attribute("Id");
                                    string type = (string)xmlRel.Attribute("Type");
                                    string target = (string)xmlRel.Attribute("Target");
                                    string targetMode =
                                        (string)xmlRel.Attribute("TargetMode");
                                    if (targetMode == "External")
                                        package.CreateRelationship(
                                            new Uri(target, UriKind.Absolute),
                                            TargetMode.External, type, id);
                                    else
                                        package.CreateRelationship(
                                            new Uri(target, UriKind.Relative),
                                            TargetMode.Internal, type, id);
                                }
                            }
                            else
                            // add part level relationships
                            {
                                string directory = name.Substring(0, name.IndexOf("/_rels"));
                                string relsFilename = name.Substring(name.LastIndexOf('/'));
                                string filename =
                                    relsFilename.Substring(0, relsFilename.IndexOf(".rels"));
                                PackagePart fromPart = package.GetPart(
                                    new Uri(directory + filename, UriKind.Relative));
                                foreach (XElement xmlRel in
                                    xmlPart.Descendants(rel + "Relationship"))
                                {
                                    string id = (string)xmlRel.Attribute("Id");
                                    string type = (string)xmlRel.Attribute("Type");
                                    string target = (string)xmlRel.Attribute("Target");
                                    string targetMode =
                                        (string)xmlRel.Attribute("TargetMode");
                                    if (targetMode == "External")
                                        fromPart.CreateRelationship(
                                            new Uri(target, UriKind.Absolute),
                                            TargetMode.External, type, id);
                                    else
                                        fromPart.CreateRelationship(
                                            new Uri(target, UriKind.Relative),
                                            TargetMode.Internal, type, id);
                                }
                            }
                        }
                    }
                    package.Flush();
                    package.Close();
                }
            }
            #endregion

    You will get Word 2013 document saved under destination location using this.

    Please let me know if this resolves your issue.

    Thank you,


    Tuesday, July 21, 2015 4:20 AM
  • Hmm.. thanks.

    But my source document is in 2003 WordProcessingML format .

    And FlatToOpc method is not suitable to handle this format as I understand ..

    Tuesday, July 21, 2015 6:56 AM