Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Word Document Element Manipulation #3

Closed
sabir-aspose opened this issue Oct 28, 2023 · 2 comments · Fixed by #17
Closed

Enhanced Word Document Element Manipulation #3

sabir-aspose opened this issue Oct 28, 2023 · 2 comments · Fixed by #17
Assignees
Labels
enhancement New feature or request

Comments

@sabir-aspose
Copy link
Collaborator

We're offering the following element manipulation functions:

  1. InsertBefore: Add a new element before an existing one.
  2. InsertAfter: Add a new element after an existing one.
  3. Append: Attach a new element at the end.
  4. RemoveBefore: Delete an element before an existing one.
  5. RemoveAfter: Delete an element after an existing one.
  6. Remove: Erase an existing element.
  7. Update: Modify an existing element.
@sabir-aspose
Copy link
Collaborator Author

sabir-aspose commented Nov 13, 2023

Resolved in 23.10.0. Public methods InsertBefore, InsertAfter, RemoveBefore, RemoveAfter, Remove and Update methods with overloads are part of Document class in Openize.Words namespace.

Below is an example of inserting a new paragraph after every paragraph containing text "Heading" in an existing word document:

using System;
using System.IO;
using System.Collections.Generic;
using Openize.Words;
using Openize.Words.IElements;
namespace Openize.Words.Examples
{
    class Program
    {
        static void Main(string[] args)
        {  
            Document doc = new Document("WordDocumentWithParas.docx");
            Paragraph paragraphToInsert;
            List<Paragraph> paragraphsToFind = new List<Paragraph>();
            List<Paragraph> paragraphsToInsert = new List<Paragraph>();
            int num=0;
            foreach (IElement element in doc.GetElements())
            {
               if (element is Paragraph para && para.Text.Contains("Heading"))
               {           
                   paragraphsToFind.Add(para);
                   paragraphToInsert = new Paragraph();
                   paragraphToInsert.Runs.Add(new Run { Text = $"Normal Paragraph {num + 1} inserted by FileFormat.Words" });
                   paragraphsToInsert.Add(paragraphToInsert);
                   num++;
              }
         }
         num = 0;
         foreach (Paragraph para in paragraphsToInsert)
         {
             doc.InsertAfter(para, paragraphsToFind[num]);
             num++;
         }
         doc.Save("WordDocumentWithParasInsertedAfter.docx");
      }
   }
}

The issue will be closed after adding docs and examples.

@sabir-aspose sabir-aspose self-assigned this Nov 13, 2023
@sabir-aspose sabir-aspose added the enhancement New feature or request label Nov 13, 2023
@openize-words openize-words linked a pull request Jun 12, 2024 that will close this issue
@openize-words
Copy link
Owner

Already resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants