Aspose Words .Net - Removing Embedded Text

Hi,

Is there a way to remove embedded texts without specifying the text, or prevent being inserted at the first place before or after builder.Inser…(…)?

@johnAxe

To remove embedded texts in Aspose.Words without specifying the text, you can utilize the document manipulation capabilities provided by the library. While there isn’t a direct method to prevent embedded texts from being inserted, you can manage the content after it has been added.

Here’s a general approach to remove specific types of content, such as page breaks or section breaks, which might be considered as embedded content:

  1. Removing Page Breaks: You can iterate through the paragraphs in the document and check for page breaks. If found, you can remove them.

  2. Removing Section Breaks: Similar to page breaks, you can loop through the sections and combine them if necessary, effectively removing the section breaks.

Here’s a code example demonstrating how to remove page breaks and section breaks:

using System;
using Aspose.Words;
using Aspose.Words.Fields;

namespace DocumentManagement
{
    class RemoveContent
    {
        public void RemoveEmbeddedContent(Document doc)
        {
            RemovePageBreaks(doc);
            RemoveSectionBreaks(doc);
        }

        private void RemovePageBreaks(Document doc)
        {
            NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
            foreach (Paragraph para in paragraphs)
            {
                if (para.ParagraphFormat.PageBreakBefore)
                    para.ParagraphFormat.PageBreakBefore = false;

                foreach (Run run in para.Runs)
                {
                    if (run.Text.Contains(ControlChar.PageBreak))
                        run.Text = run.Text.Replace(ControlChar.PageBreak, string.Empty);
                }
            }
        }

        private void RemoveSectionBreaks(Document doc)
        {
            for (int i = doc.Sections.Count - 2; i >= 0; i--)
            {
                doc.LastSection.PrependContent(doc.Sections[i]);
                doc.Sections[i].Remove();
            }
        }
    }
}

In this example, the RemoveEmbeddedContent method is responsible for calling the methods that handle the removal of page breaks and section breaks. You can adapt this approach to target other types of embedded content as needed.

For preventing embedded texts from being inserted in the first place, you would need to manage the logic in your document creation process to avoid adding unwanted content.

If you have specific types of embedded texts in mind, please provide more details for tailored guidance.

This generic answer doesn’t provide solution.

@johnAxe Unfortunately, you requirements are not clear enough. If possible, could you please attach your input, output and expected output documents along with your sample code? We will check the issue and provide you more information.