none
trying to create bulleted list RRS feed

  • Question

  • Im trying to make bulleted listin my document.  My method has pretty much been to create a docx the normal way, then open it as a zip file and look at the markup.

    on the line

    Dim stylePart = wordDoc.AddNewPart(Of StyleDefinitionsPart)()

     

    I am getting the following error

    DocumentFormat.OpenXml.Packaging.OpenXmlPackageException was unhandled by user code
      Message="The part cannot be added here."

    Any idea on what im doing wrong? also if im going about making bulleted list in the wrong way let me know, im assuming I just need to have the markup in the document then add a style to styles.xml.

    Code Snippet

    Imports System.IO

    Imports DocumentFormat.OpenXml

    Imports DocumentFormat.OpenXml.Packaging

     

    Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim docXML As String = "<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?> " & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "<wBig Smileocument xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "<w:body>"

    docXML = docXML & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "<wStick out tongue><wStick out tonguepr><w:jc w:val=""center"" /></wStick out tonguepr><w:r><w:t>this is a test</w:t></w:r></wStick out tongue>"

    docXML = docXML & "<wStick out tongue><w:numpr><w:ilvl val=""0"" /><w:numid val=""1"" /></w:numpr><w:r><w:t>testing</w:t></w:r></wStick out tongue>"

    docXML = docXML & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "</w:body>" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "</wBig Smileocument>"

    Dim styleXML As String = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?><wTongue Tiedtyles xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">"

    styleXML = styleXML & "<wTongue Tiedtyle w:type=""paragraph"" wTongue TiedtyleId=""ListParagraph""><w:name w:val=""List Paragraph"" /><w:basedOn w:val=""Normal"" /><w:uiPriority w:val=""34"" /><w:qFormat /><w:rsid w:val=""003F5A06"" /><wStick out tonguePr><w:ind w:left=""720"" /><w:contextualSpacing /></wStick out tonguePr></wTongue Tiedtyle></wTongue Tiedtyles>"

    'Create the Package

    Using wordDoc As WordprocessingDocument = WordprocessingDocument.Create("c:\testing3.docx", WordprocessingDocumentType.Document)

    'Create main document

    Dim mainPart As MainDocumentPart = wordDoc.AddMainDocumentPart

    Dim st As Stream = mainPart.GetStream

    Dim utf As UTF8Encoding = New UTF8Encoding

    Dim buf As Byte() = utf.GetBytes(docXML)

    st.Write(buf, 0, buf.Length)

    'Create Styles

    Dim stylePart = wordDoc.AddNewPart(Of StyleDefinitionsPart)()

    Dim stylestream As Stream = stylePart.GetStream

    utf = New UTF8Encoding

    buf = utf.GetBytes(styleXML)

    stylestream.Write(buf, 0, buf.Length)

     

    End Using

    End Sub

    End Class

     

     

    Apologies for the stupid emoticons in the markup, not sure how to getrid of it
    Tuesday, July 22, 2008 6:35 PM

Answers

  • // Create the Main Document part

    MainDocumentPart _mainPart = _doc.AddMainDocumentPart ();

    // Create a Style Part

    StyleDefinitionsPart _stlePart = _mainPart.AddNewPart<StyleDefinitionsPart>();

     

    Style definition part would allow you to define the styles used in the document but to apply numbering to parts you need to create a part NumberingDefinitionsPart

    In OpenXml all numbering and bullets are contained of a part called Numbering.xml.

     

    Numbering.xml consist of two things, a numId and a abstractnumId which in conjunction allow you to define the numbering to be used in a document. The abstractNumId would contain the styles and the levels that a numbering can go to, the numId on the other hand is the the id that contains a reference of the abstractnumId.

    The client (document.xml) would provide the numId in the appropriate paragraph where numbering needs to be applied.

     

    For a sample just open Word, apply bulleting / numbering to a paragraph. open the package (zip format) you will see a numbering.xml has been created. if you also open the document.xml you will see a reference of the numId in the paragraph properties (numPr element)

     

     

     

     

    Wednesday, July 23, 2008 5:25 AM