This is a migrated thread and some comments may be shown as answers.

Access to OpenXML in docx.

4 Answers 417 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 06 Sep 2018, 03:17 PM
Is it possible using Telerik's tools to get at the OpenXML file in a Word document?

4 Answers, 1 is accepted

Sort by
0
Tom
Top achievements
Rank 1
answered on 10 Sep 2018, 10:02 AM

In case it's of any use:  We use both OpenXML and Telerik on the same document in some of our code.  The way we work that is:

  1. load the file to a byte array (DocxFormatProvider.Import(byte[])
  2. load that byte array into the Telerik objects
  3. Do what we need to do,
  4. Export it back to a byte array (DocxFormatProvider Export(RadFlowDocument))
  5. Load it into the OpenXML objects
  6. Do our OpenXML actions

 

0
Petya
Telerik team
answered on 11 Sep 2018, 11:13 AM
Hi Brian, Tom,

WordsProcessing is a library intended to import, modify, generate and export various flow document formats, including DOCX. A file is basically a collection of XML files in accordance with the Office Open XML specification, so I'm not entirely sure what you are trying to achieve, Brian.

If you're looking to edit a document, then the approach Tom outlined is the way to go. You could import a document with the DocxFormatProvider, edit its contents with a RadFlowDocumentEditor and export it back with the format provider.

Alternatively, if for some reason you are looking for access to the underlying XML files, our ZipLibrary can help achieve this task.

I hope this helps, but if I misunderstood your question, please let me know.

Regards,
Petya
Progress Telerik

0
Brian
Top achievements
Rank 1
answered on 11 Sep 2018, 12:38 PM

I am working on some legacy code that parses (read only) the XML document for various reasons. I am trying to replace a lot of that old code with similar functionality from the Telerik tools.  As I do this, I am prioritizing by the amount of time each piece will take me to finish.  In this case, it will take a while, so it is going on the back burner.  At the same time, if I can swap out some small part of the code with Telerik's equivalent utilities, it will make things easier when I come back to it.  So what I am looking for is direct access to read the XML document in the DOCX file. 

You mentioned the Zip Library.  I looked at that, but didn't see examples dealing with OpenXML documents.  Do you know of any such examples?

0
Petya
Telerik team
answered on 13 Sep 2018, 03:21 PM
Hi Brian,

Thanks for elaborating. 

The ZipLibrary can help you gain access to the underlying XML files in a DOCX archive, for example:
using (Stream stream = File.Open("test.docx", FileMode.Open))
{
    using (ZipArchive archive = new ZipArchive(stream))
    {
        foreach (var archiveEntry in archive.Entries)
        {
            //process the individual files in the DOCX archive
        }
    }
}

In this scenario, you can then read the XML files with File.Open, however, the ZipLibrary doesn't have the capabilities to parse those XML files. This would be something WordsProcessing can handle (to provide some additional context - by using the same ZipLibrary internally) with the approach outlined in the previous posts. 

Let me know if I can help with anything else.

Regards,
Petya
Progress Telerik

Kamran
Top achievements
Rank 3
Iron
Iron
Veteran
commented on 06 Apr 2022, 02:19 PM

Can you please help us by example.  I have exported byte[] from DocxFormatProvider.

Now I want to get Open Office XML from byte[].  My goal is to convert byte[] to xml document.

Svilen
Telerik team
commented on 11 Apr 2022, 07:08 AM

Hey, Kamran,

If you wish to export the byte array to a DOCX file, please use this line of code:

File.WriteAllBytes("document.docx",byteArray); //The second argument is your byte array

If you wish to programmatically obtain the XML files from a DOCX file created with WordsProcessing, you can first export the RadFlowDocument to a file and then use the example code in the article Extract the contents of a zip file to a directory to extract it with ZipLibrary.

Please let me know if this helps or if you have a different goal.

Tags
WordsProcessing
Asked by
Brian
Top achievements
Rank 1
Answers by
Tom
Top achievements
Rank 1
Petya
Telerik team
Brian
Top achievements
Rank 1
Share this question
or