Open XML SDK, how to delete the pages when I find a certain line in the doc file RRS feed

  • Question

  • Hi,

    I am using  Open XML SDK in my C#.NET project and here is what I want to do. First, I need to find a line where is for example written this: "REMOVE THE PAGES BEHIND". When I find that page I need to literally delete pages behind that line. Here is the example:


    Page 1



    Page 2



    Page 3




    Page 4


    So, I need to delete first two pages and leave the rest. I found article which explains how to delete the file, but is there some way to find that line?

    Thank you in advance.

    Wednesday, November 13, 2013 3:52 PM


  • Hi Ferid

    Mmmm. Yes, you should be able to find the line of text, although it's not as straight-forward as it might appear because there's no guarantee that it's all stored together in one <w:t> element. Formatting and change tracking could break it up into multiple "runs".

    The trickier part when working with the Open XML SDK is identifying pages. If there's a manual page break at the end of every page, then there's no problem. If the pages rely on how Word lays out the document, then it's not possible to be sure of identifying them in a file not opened inside the Word application.

    If, however, you can say "delete everything before this point", then it shouldn't be a problem...

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 14, 2013 5:47 PM