none
Looking for various tutorials, samples - help me RRS feed

  • Question

  • Hi,

    I am getting into a kind of a big project where I have to generate docx files which have to include lots of different things (drawings, header, footer, diagrams, pictures). I am totally new to OpenXML and so far I haven't found good tutorials that covered all the areas mentioned earlier. I visited openxmldeveloper.org where there are several presentations and sample code but that is not enough. I downloaded an e-book called OpenXML Explained, it is good but not detailing the programming part. So now by programming I can do the following: adding text, modifying its appearance (size, justification) but I do not know how to add a picture (which namespaces are required, how to add the to the package, how to reference to them, where to put the phsysical image), how to draw.
    Does anyone know if there are such tutorials? Or where to look for them? In msdn I looked the SDK help but I didn't get more confident. I really need a push, where to turn if I want to generate docx files with such rich content.
    Thursday, October 11, 2007 10:26 AM

Answers

All replies

  • Perhaps it should good for you to use the Microsoft tool of choice (Word, Excel, Powerpoint) to build a template (or many) and modify them instead of start from scratch.

    Then you can do what you well know (change the text, the images, etc.) in a little part of the time and take some coffee .

    If there is possible think in terms of models this the way to do it.

     

    Thursday, October 11, 2007 2:20 PM
  • Well, it sounds like a good start but I face the problem again. I'd like to use a template:  how can I do it? Should I add a namespace reference to the dotx file and embed the file into the package? Or adding some new tag is enough? I need to have a guide of how to do it, I can't figure it out on my own. Dissecting the package that Word creates is very time consuming (and at this time I hardly can understand it).
    Thursday, October 11, 2007 6:20 PM
  • Well, so far I think, OpenXML SDK is a good way to go on. I found some examples in MSDN but it's still preliminary, I am still struggling with how to add things to my document in a correct way. Isn't there a hidden stash somewhere of good SDK tutorials maybe?
    Friday, October 12, 2007 5:56 PM
  • Well, I think I am a little confused. I tried to use the SDK for creating a docx. I found a snippet in msdn and I used it:

    Code Block

    using (WordprocessingDocument wordDoc=
                    WordprocessingDocument.Create("test2.docx",WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mdp = wordDoc.AddMainDocumentPart();
                    SetMainDocumentContent(mdp);
                }

            private void SetMainDocumentContent(MainDocumentPart part)
            {
                const string docXml =
    @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
    <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
        <w:body><w:p><w:r><w:t>Hello world!</w:t></w:r></w:p></w:body>
    </w:document>";

                using (Stream stream = part.GetStream())
                {
                    byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
                    stream.Write(buf, 0, buf.Length);
                }
            }



    So far so good.

    Here is the code of adding a new part:

    Code Block

    public static void AddNewPart(string document, string fileName)
    {
       using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
       {
          MainDocumentPart mainPart = wordDoc.MainDocumentPart;
                   
          CustomXmlPart myXmlPart = mainPart.AddNewPart<CustomXmlPart>();

          using (FileStream stream = new FileStream(fileName, FileMode.Open))
          {
             myXmlPart.FeedData(stream);
          }
        }
    }



    It seems easy to do but I couldn't add the header part to my document just by replacing the CustomXmlPart with HeaderPart. It's kinda annoying to keep fighting with this, I think I really stucked here. I need some big wind to blow me out of this hole.
    Friday, October 12, 2007 9:08 PM
  •      Try Werner van Vugt free PDF book "Open XML explained", available as a link from his new blog site http://blogs.code-counsel.net/wouter/default.aspx .  The link is http://openxmldeveloper.org/archive/2007/08/13/1970.aspx .  There are many more resources at openxmldeveloper.org , starting with http://openxmldeveloper.org/LearnMore.aspx .

         Doug Mahugh blog:  http://blogs.msdn.com/dmahugh/

         Brian Jones blog: http://blogs.msdn.com/brian_jones/

         Erika Ehrli blog: http://blogs.msdn.com/erikaehrli/

     

    Friday, November 9, 2007 9:22 PM
  • I found that book at the beginning of my tries with OpenXML, it's a good base, and I also visited the openxmldeveloper.org.
    I think one has to wait for some time to find good tutorials as this technology is rather new and there is a lack of tutorials yet. Personally I got some experience while I worked with OXML, and it is a little easier now than it was at the first try.

    Monday, November 26, 2007 11:58 PM
  • Hi there,

    For those of you in the lookout for code samples, articles, and tutorials, I strongly recommend the following resources:


    Erika Ehrli
    Wednesday, January 28, 2009 9:53 PM
    Moderator