Hello,
Thanks for your patiently waiting.
For your requirement, please refer to the following code:
The logic of the code is the following:
1) Getting the collection of textbox
2) Put the textbox object and the vertical position of textbox to Map
3) Classify the textbox according to its location, then put it to Map object, take the location as the key of the Map, and put the textbox with the same location into the list as the value of the Map
4) Get a list of 3 or 4 elements, then walk through the list and get the text of each textbox object, and delete all Textboxes in the list when the requirements you mentioned are met.
- Code: Select all
import com.spire.doc.*;
import com.spire.doc.collections.TextBoxCollection;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class deleteTableFromTextBox {
public static void main(String[] args) {
//Load the document
Document doc = new Document();
doc.loadFromFile("data/Product_Datasheet_TW.docx");
//Get the collection of textbox in document
TextBoxCollection textBoxCollection = doc.getTextBoxes();
//Place the TextBox as the key and the location of textbox t as the value
Map<TextBox,Integer> textBoxPositionMap = new HashMap<TextBox,Integer>();
for (int i = 0; i < textBoxCollection.getCount(); i++) {
int textBoxIndex = i;
//Get the vertical position
int horizontalPosition = (int)textBoxCollection.get(i).getVerticalPosition();
textBoxPositionMap.put(textBoxCollection.get(i),horizontalPosition);
}
//Place the location information as the key and the textbox with the same location in the list as the value
Map<Integer, List<TextBox>> posistionSortMap = new HashMap<Integer, List <TextBox>>();
for (TextBox key : textBoxPositionMap.keySet()) {
int value = textBoxPositionMap.get(key);
if (posistionSortMap.containsKey(value)) {
List<TextBox> list = posistionSortMap.get(value);
list.add(key);
}else{
List<TextBox> list = new ArrayList<TextBox>();
list.add(key);
posistionSortMap.put(value,list);
}
}
//Gets the case where there are three textboxes in a row
for (List<TextBox> list : posistionSortMap.values()) {
if (list.size() == 3||list.size() == 4) {
int j = 0;
for (int i = 0; i < list.size(); i++) {
TextBox tBox = list.get(i);
for (int k = 0; k < tBox.getChildObjects().getCount(); k++) {
DocumentObject objt = tBox.getChildObjects().get(k);
//get text from paragraph in TextBox.
if (objt.getDocumentObjectType() == DocumentObjectType.Paragraph) {
if (((Paragraph) objt).getText().trim().contains("{{img") ) {
j++;
}
}
}
}
//Delete the textbox
if ((list.size() == 3&&j >= 2)||list.size() == 4&&j>=3) {
System.out.println(list.size());
for (int m = 0; m < list.size(); m++) {
//System.out.println(m);
doc.getChildObjects().remove(list.get(m));
}
}
}
}
//Save the document
String output = "output/deleteTableFromTextBox.docx";
doc.saveToFile(output, FileFormat.Docx_2013);
}
}
Sincerely
Abel
E-iceblue support team