We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Remove Nodes from its Parent Word Document using Node.Remove() Method C# Code

Hi I am not able to remove node from its parent document. even after invoking the .Remove() API nodes still exists in the parent document.

Please find the code snippet i used for removing nodes.

>     var nodes = thisDoc.GetChildNodes(NodeType.Paragraph, true);
for(var i = 0; i < nodes.Count; i++ )
{
	var para = nodes[i];
	var p = collector.GetStartPageIndex(para);
	try
	{

		enumerator.Current = collector.GetEntity(para);
		
		// only retain nodes within the given coords
		if (!fig.rec.IntersectsWith(enumerator.Rectangle))
		{
			list.Add(para);
			
			//para.Remove();

		}
		else
		{
			if (para.NodeType == NodeType.Paragraph)
			{
				if (para.IsComposite)
				{
					CompositeNode curComposite = (CompositeNode)para;
					foreach (var nod in curComposite.GetChildNodes(NodeType.Paragraph, true))
					{
						//nod.Remove();
						list.Add(nod);
					}
				}
				else
				{
					list.Add(para);

				}
			}
		}
	}
	catch (Exception ex)
	{

		throw;
	}
	
	 string outErrorMessage;
	
}

PDF.WordToPdf(thisDoc, Path.Combine(Directory.GetCurrentDirectory(),"PDF", string.Concat(fig.Figid, ".pdf")), out outErrorMessage);

@senthilspi,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Your expected Word document showing the desired output. You can create expected document by using MS Word.
  • Please also provide a screenshot highlighting the desired area/node that you want to remove by using Aspose.Words.
  • Please also create a standalone simple Console Application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

Hi Hafeez,
sampleAsposeTest.zip (4.9 MB)
Please find the attached samples.
I inferred a strange behavior, I am simply trying to split the document by page and in each page delete all the nodes except shapes and save as pdf and word.
during PDF save all the node i have removed are present in the output, where as during DOCX save, all the documents got deleted, including the shapes I have preserved.

Please let me know how to successfully remove only specific nodes and save remaining as PDF.

@senthilspi,

We are working on your query and will get back to you soon.

@senthilspi,

We managed to produce following exception at: enumerator.Current = collector.GetEntity(nod);

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
Parameter name: value
  Source=Aspose.Words
  StackTrace:
   at Aspose.Words.Layout.LayoutEnumerator.set_Current(Object value)

The problem occurs because you are passing a Section node in GetEntity method; but please note that this method works for only Paragraph nodes, as well as indivisible inline nodes, e.g. BookmarkStart or Shape. It doesn’t work for Run, CellRow or Table nodes, and nodes within header/footer. For more details, please refer to: