wordpress hit counter
How to convert a "dotx" into docx file with c# and open Xml that contains building block? - WordprocessingML - Formats - OpenXML Developer

How to convert a "dotx" into docx file with c# and open Xml that contains building block?

Formats

Discussions about working with different Open XML Formats

How to convert a "dotx" into docx file with c# and open Xml that contains building block?

  • rated by 0 users
  • This post has 8 Replies |
  • 1 Follower
  • Dear all,

     

    I have a big problem I generate docx from document template dotx that contains building block but I have no more the possibility to select my building block.

      here is my code

     

     MemoryStream documentStream;
                    String templatePath = Path.Combine(Environment.CurrentDirectory, @"path of the dotx");
                    using (Stream tplStream = File.OpenRead(templatePath))
                    {

                        documentStream = new MemoryStream((int)tplStream.Length);
                        CopyStream(tplStream, documentStream);
                        documentStream.Position = 0L;
                    }
                    using (WordprocessingDocument template = WordprocessingDocument.Open(documentStream, true))
                    {

                        template.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);

                        MainDocumentPart mainPart = template.MainDocumentPart;

                        mainPart.DocumentSettingsPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",

                           new Uri(templatePath, UriKind.Absolute));



                        mainPart.Document.Save();

                    }



                    string path = @"path of the new doc";

                    File.WriteAllBytes(path, documentStream.ToArray());



                    // Run Word to open the document:

                    System.Diagnostics.Process.Start(path);

     

    do you have any idea how I can mange with the building block

  • Hi,

       To convert from dotx to docx you can refer this post.

  • it's exactly what i do but I have building block and by this method I have no possibility to select it

  • when I compare by doing the operation manually I see that the folder glossary inside the word folder is missing

  • sorry I made a mistake the folder glossary is here , the linked with the template is made in the file app.xml from folder docProps and the path of the template is displayed in settings.xml.rels  of the fodler word\_rels

    but the list of building block is not available

  • Hi,

      Can you please send any screen shots, dotx file & the code which you are trying?

  • pour openxml.zip

    Thanks for your help.

    You can find in attachment a zip file with the template and the document produced.

    If you look at the document produced you can see that it's no more possible to select a building block.

    The template and the docuemnt generated are in the same folder.

    Here is the code

     

      static void Main(string[] args)
            {

                try
                {              
                    MemoryStream documentStream;
                    String templatePath = Path.Combine(Environment.CurrentDirectory, @"path of the dotx");
                    using (Stream tplStream = File.OpenRead(templatePath))
                    {
                        documentStream = new MemoryStream((int)tplStream.Length);
                        CopyStream(tplStream, documentStream);
                        documentStream.Position = 0L;
                    }
                    using (WordprocessingDocument template = WordprocessingDocument.Open(documentStream, true))
                    {

                        template.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
                        MainDocumentPart mainPart = template.MainDocumentPart;
                        mainPart.DocumentSettingsPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",new Uri(templatePath, UriKind.Absolute));
                        mainPart.Document.Save();
                    }
                    string path = @"path of the docx";
                    File.WriteAllBytes(path, documentStream.ToArray());
                    // Run Word to open the document:
                    System.Diagnostics.Process.Start(path);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);

                } Console.WriteLine("Press Return to terminate");
                Console.ReadLine();
            }




            public static void CopyStream(Stream source, Stream target)
            {

                if (source != null)
                {

                    MemoryStream mstream = source as MemoryStream;
                    if (mstream != null) mstream.WriteTo(target);
                    else
                    {
                        byte[] buffer = new byte[2048];
                        int length = buffer.Length, size;
                        while ((size = source.Read(buffer, 0, length)) != 0)
                            target.Write(buffer, 0, size);
                    }
                }
            }

     

    Thanks in advance for your advice, help.

  • I have no problem to fill the customXML , it's the selection of the building block my main problem

  •  

    when I look at the file generated, and go the developer item in the word ruban, if I select the Document Template I see that my template is not attached

     

    but in the file word\_rels\settings.xml.rels  I have the path on my template

     

    <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate" Target="path of the template.dotx" TargetMode="External" Id="R01f8ea2394c24745" />

Page 1 of 1 (9 items)