wordpress hit counter
Creating Documents from Template - Industry Solutions - Open XML Scenarios - OpenXML Developer

Creating Documents from Template

Open XML Scenarios

Discussions about how to use the Open XML Formats

Creating Documents from Template

  • rated by 0 users
  • This post has 3 Replies |
  • 3 Followers
  • I am trying to create Documents(.docx) in Open XML from Templates(.dotx).

    Is there a better way to generate Dotx in to docx.

    But problem is ContentType is readonly

    Uri partUriDocument = PackUriHelper.CreatePartUri(new Uri("word//document.xml", UriKind.Relative));

                        PackagePart DocPart = DocumentPackage.GetPart(partUriDocument);

                        DocPart.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";

     

    DocPart.ContentType is readonly.

     

    For template its value is application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml

     

    For documents it should be  application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml

     

     I had tried removing a Part and adding a new Part but this was causing some other error during opening the document from word and also the package was increasing in file size.


  • Hi,

    This sample code will convert dotx file to docx file.

     const string documentRelationshipType =

    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
                const string wordDOCXContentType =

    "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
                string docName = @"c:\test9\1nameis.dotx";
                Package wdPackage = Package.Open(docName, FileMode.Open, FileAccess.ReadWrite);
                PackagePart documentPart = null;
                Uri documentUri = null;

                //  Get the main document part (document.xml).
                foreach (System.IO.Packaging.PackageRelationship relationship in

    wdPackage.GetRelationshipsByType(documentRelationshipType))
                {
                    documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative),

    relationship.TargetUri);
                    documentPart = wdPackage.GetPart(documentUri);
                    //  There is only one document.
                    break;
                }

                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(documentPart.GetStream());

                //  Delete the document part and its relationship part.
                wdPackage.DeletePart(documentPart.Uri);

                //  Create new document and relationship parts using the correct content types.
             
                documentPart = wdPackage.CreatePart(documentUri, wordDOCXContentType);
                xdoc.Save(documentPart.GetStream(FileMode.Create, FileAccess.Write));
                wdPackage.Close();

                string newFileName = Path.GetDirectoryName(docName) + @"\" +

    Path.GetFileNameWithoutExtension(docName) + ".docx";
                //  If the new file exists, delete it. You might
                //  want to make this code less destructive.
                if (File.Exists(newFileName))
                {
                    File.Delete(newFileName);
                }
                File.Move(docName, newFileName);

    Sheela

  • This code is giving the same error in opening the file, do you have any refined code?
  • How receive all PackagePart and insert to new document(docx)? I have table in my template (.dotx). When I open new created document I do not see TABLE, border is not visible.
Page 1 of 1 (4 items)