Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:11:05+00:00 2026-06-09T03:11:05+00:00

I want to split a Word document in two by a specific text programatically

  • 0

I want to split a Word document in two by a specific text programatically using C# and the Open XML SDK.
What I’ve done for the first part is removing all paragraphs up until the paragraph containing the desired text. this worked fine.
Then on the copy of the original document I did the same only this time removing all paragraphs starting from the one containing the desired text.
For some reason the second part turned out to be an invalid document, that can’t be opened using word.
Opening the corrupted document with “Open XML SDK 2.0 Productivity Tool” and validating it, doesn’t detect any problems with the document.

This is the code removing the part before the desired text (works fine):

public static void DeleteFirstPart(string docName)
    {
        using (WordprocessingDocument document = WordprocessingDocument.Open(docName, true))
        {
            DocumentFormat.OpenXml.Wordprocessing.Document doc = document.MainDocumentPart.Document;

            List<Text> textparts = document.MainDocumentPart.Document.Body.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().ToList();
            foreach (Text textfield in textparts)
            {
                if (!textfield.Text.Contains("split here"))
                {
                    RemoveItem1(textfield);
                }
                else
                {
                    break;
                }
            }
        }
    }

I Tried two different remove item methods, both with same result:

private static void RemoveItem1(Text item)
    {
        // Need to go up at least two levels to get to the run.
        if ((item.Parent != null) &&
          (item.Parent.Parent != null) &&
          (item.Parent.Parent.Parent != null))
        {
            var topNode = item.Parent.Parent;
            var topParentNode = item.Parent.Parent.Parent;
            if (topParentNode != null)
            {
                topNode.Remove();
                // No more children? Remove the parent node, as well.
                if (!topParentNode.HasChildren)
                {
                    topParentNode.Remove();
                }
            }
        }
    }


private static void RemoveItem2(Text textfield)
    {
        if (textfield.Parent != null)
        {
            if (textfield.Parent.Parent != null)
            {
                if (textfield.Parent.Parent.Parent != null)
                {
                    textfield.Parent.Parent.Remove();
                }
                else
                {
                    textfield.Parent.Remove();
                }
            }
            else
            {
                textfield.Remove();
            }
        }   
    }

This is the code removing the part starting from the desired text (corrupts the document):

public static void DeleteSecondPart(string docName)
    {
        using (WordprocessingDocument document = WordprocessingDocument.Open(docName, true))
        {
            DocumentFormat.OpenXml.Wordprocessing.Document doc = document.MainDocumentPart.Document;

            List<Text> textparts = document.MainDocumentPart.Document.Body.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().ToList();
            bool remove = false;
            foreach (Text textfield in textparts)
            {
                if (textfield.Text.Contains("split here"))
                {
                    remove = true;
                }

                if(remove)
                {
                    RemoveItem1(textfield);
                    //Using this commented code line, instead of the one above, removes only the text field itself, it works fine, the document is valid, but it leaves empty paragraphs that could be pages long.
                    //textfield.Remove();

                }
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:11:08+00:00Added an answer on June 9, 2026 at 3:11 am

    A rewrite of the RemoveItem method did the trick:

     private static void RemoveItem3(Text textfield)
        {
            OpenXmlElement element = textfield;
            while (!(element.Parent is DocumentFormat.OpenXml.Wordprocessing.Body) && element.Parent != null)
            {
                element = element.Parent;
            }
    
            if (element.Parent != null)
            {
                element.Remove();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split a string into two variables, the first word and the
I have a Microsoft Word Document (docx) and I use Open XML SDK 2.0
My objective is to read an XML text file and split each word and
I have code like that 010200345 i want split every first three digit please
I want to split a string in Javascript using split function into 2 parts.
I want to split a string like this: abc//def//ghi into a part before and
I want to split a file containg HTTP response into two files: one containing
I have the following string 3/4Ton. I want to split it as --> word[1]
This is what I want to do: Split a word into separate charachters. The
I'm going to make a word wrap algorithm in PHP. I want to split

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.