Hello,
Thanks for your inquiry.
Are you currently using Spire.Doc for Net and the code in the screenshot below to achieve your requirement? If so, the textbox will be deleted in result word document due to the logic of the code is that: Removing directly the paragraph that doesn’t contain text, if textbox in these paragraph, it also will be deleted.
To avoid this issue, you can copy textbox to the paragraph that contain text before deleting the paragraph that doesn’t contain text. I put the complete code below for your reference.
If you have any issue, just feel free to contact us.
- Code: Select all
//Create Word document.
Document document = new Document();
//Load the file from disk.
document.LoadFromFile(@"..\..\data\DocWithSpace.docx");
//Traverse every section on the word document and remove the null and empty paragraphs.
foreach (Section section in document.Sections)
{
for (int i = 0; i < section.Body.ChildObjects.Count; i++)
{
if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
{
if (String.IsNullOrEmpty((section.Body.ChildObjects[i] as Paragraph).Text.Trim()))
{
Paragraph para = (Paragraph)section.Body.ChildObjects[i];
//Traverse child objects of paragraph
foreach (DocumentObject paraObj in para.ChildObjects)
{
//If child objects of paragraph is "TextBox", clone the "TextBox" to the the first paragraph
if (paraObj.DocumentObjectType == DocumentObjectType.TextBox)
{
section.Paragraphs[0].ChildObjects.Add(paraObj.Clone());
}
}
//Delete the paragrph including blank line
section.Body.ChildObjects.Remove(section.Body.ChildObjects[i]);
i--;
}
}
}
}
//Save to file.
String result = @"../../output/CopyTextBoxBeforeDelete.docx";
document.SaveToFile(result, FileFormat.Docx2013);
Sincerely
Abel
E-iceblue support team
Login to view the files attached to this post.