none
ChangeDocumentType swap BookmarkStart elements order RRS feed

  • Question

  • I have a dotx template created with Word. The xml document file contains this snippet:

             <w:p w:rsidR="00047782" w:rsidRDefault="00047782">
                <w:pPr>
                  <w:jc w:val="center"/>
                </w:pPr>
                <w:bookmarkStart w:id="0" w:name="testata"/>
                <w:bookmarkStart w:id="1" w:name="logo"/>
                <w:bookmarkStart w:id="2" w:name="_GoBack"/>
                <w:bookmarkEnd w:id="2"/>
                <w:r>
                  <w:t>LOGO</w:t>
                </w:r>
                <w:bookmarkEnd w:id="1"/>
              </w:p>

    If I either use ChangeDocumentType from ooxml sdk v2 or create the docx via Word, the xml snippet become the following:

              <w:p w:rsidR="00047782" w:rsidRDefault="00047782">
                <w:pPr>
                  <w:jc w:val="center"/>
                </w:pPr>
                <w:bookmarkStart w:id="0" w:name="logo"/>
                <w:bookmarkStart w:id="1" w:name="testata"/>
                <w:bookmarkStart w:id="2" w:name="_GoBack"/>
                <w:bookmarkEnd w:id="2"/>
                <w:r>
                  <w:t>LOGO</w:t>
                </w:r>
                <w:bookmarkEnd w:id="0"/>
              </w:p> 

    As you can see the BookmarkStart tags have a different order. Now if I replace the content inside a bookmark this difference is huge.

    Can someone explain why the conversion from dotx to docx change so much the document?

    Thanks

     


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    Tuesday, September 7, 2010 10:59 AM

Answers

  • Hey Raffaele,

    Without knowing the details and workflow of how this project is going to work, it would be difficult to say which way would be best.  I will recommend taking a look at the following sample on working with bookmarks in Word documents.  It has an example of deleting bookmarks that might give you some ideas and the source code is included. 

    https://openxmldeveloper.org/articles/719.aspx

    I apologize if you have already seen the article and it wasn't what you were looking for, but if you need anything else let me know.  The other option I can offer would be to open up a case with Microsoft support and they might be able to help you out. 

    Brandon

    Friday, September 10, 2010 2:58 PM

All replies

  • Hey Raffaele,

    I wasn't able to repro the exact re-order you explain above, where "logo" and "testata" are flipped, but I do see that the bookmarks get re-ordered.  From what I can tell, the re-order comes about because we need to insert the GoBack bookmark and depending on where this bookmark is when the document is saved, it will affect the order of the bookmarks in the xml.  If you can provide some specific steps to get the behavior you mention above, that might be helpful to get a better idea what is happening. 

    The only explanation I can think of is based on my sample document.  I had 2 bookmarks ("logo" and "testata") inserted into an empty document and saved the file as a .dotx.  The xml order of this will be (0. logo, 1. testata, 2. _GoBack).  If I create a new document based off this template and then save the file, my order will be (0. logo, 1. _GoBack, 2. testata) and this should be expected as far as I know.  The GoBack bookmark is added to mark off the last cursor position of the document and therefore will change the order accordingly.

    Does that help?  If not, let me know what else I can do to help.

    Also, is there any particular reason you would need to replace content based on the id instead of the name of a bookmark?   

    Brandon

    Wednesday, September 8, 2010 11:37 PM
  • Hi Brandon,
    Thanks for your answer. I already tried to reproduce a smaller document but I could not be able to. I'll keep on trying anyway.

    I don't have problem using names instead of ids. The problem is that I need to replace the content between BookmarkStart and BookmarkEnd but as I convert the template in a document I strip the other BookmarkStart that wasn't there in the template. I need a reliable way to take/replace the bookmarked content in the document.

    I was successfull in moving bookmarks that are one inside another, but the real problem is performance. I waste a small amount of time that becomes huge when i make massive conversions.

    Since my target is to change the content in a document, I am thinking to use <w:sdt/>. What's your opinion about this choice? As <w:sdt/> is only one tag, it's very simple to control its content, while BookmarkStart/BookmarkEnd often are positioned by Word at different xml levels.

    Thanks


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    Thursday, September 9, 2010 6:43 AM
  • Hey Raffaele,

    Without knowing the details and workflow of how this project is going to work, it would be difficult to say which way would be best.  I will recommend taking a look at the following sample on working with bookmarks in Word documents.  It has an example of deleting bookmarks that might give you some ideas and the source code is included. 

    https://openxmldeveloper.org/articles/719.aspx

    I apologize if you have already seen the article and it wasn't what you were looking for, but if you need anything else let me know.  The other option I can offer would be to open up a case with Microsoft support and they might be able to help you out. 

    Brandon

    Friday, September 10, 2010 2:58 PM