Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Feb 17, 2023 9:28 am

Hello,

I am using Spire.Doc. with Java.

So, I would need to check if three TextBoxes on the same row are empty, and if they are, all three are deleted, and then the empty row is removed.

The problem is that I have more than 3 TextBoxes in the document, and how do I find the ones I am looking for?

mmarkozz
 
Posts: 8
Joined: Tue Jan 10, 2023 2:05 pm

Mon Feb 20, 2023 7:03 am

Hello,

Thanks for your inquiry.
According to your message, I can’t quite understand your issue, to help us work out a solution for you, please provide your original document and the expected result document, you can attach here or send it to us via email (support@e-iceblue.com). Thanks for your assistance in advance.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 951
Joined: Tue Mar 08, 2022 2:02 am

Mon Feb 20, 2023 9:16 am

Hello,

so I'll attach the screen.

What I need, is to delete the textbox number 3-4-5 if the 4-5 are empty, but without going to touch the remaining ones.

I look forward to your feedback, thank you


Abel.He wrote:Hello,

Thanks for your inquiry.
According to your message, I can’t quite understand your issue, to help us work out a solution for you, please provide your original document and the expected result document, you can attach here or send it to us via email (support@e-iceblue.com). Thanks for your assistance in advance.

Sincerely
Abel
E-iceblue support team

mmarkozz
 
Posts: 8
Joined: Tue Jan 10, 2023 2:05 pm

Mon Feb 20, 2023 10:21 am

Hello,

Thanks for your feedback.
I'll do further investigation and give you feedback asap.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 951
Joined: Tue Mar 08, 2022 2:02 am

Tue Feb 21, 2023 2:34 pm

Awesome, I will wait for your feedback.


Abel.He wrote:Hello,

Thanks for your feedback.
I'll do further investigation and give you feedback asap.

Sincerely
Abel
E-iceblue support team

mmarkozz
 
Posts: 8
Joined: Tue Jan 10, 2023 2:05 pm

Wed Feb 22, 2023 10:36 am

Hello,

Thanks for your patiently waiting.
For your issue, I created a document with an absolutely positioned textbox of the same length and width, as shown in the screenshot below, and then used the code below to implement your requirements.

Code: Select all
   //Load the document
    Document doc = new Document();
    doc.loadFromFile("data/textbox.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,Double> textBoxPositionMap = new HashMap<TextBox,Double>();
    for (int i = 0; i < textBoxCollection.getCount(); i++) {
        int textBoxIndex = i;
        double horizontalPosition =  textBoxCollection.get(i).getBottom();
        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<Double, List<TextBox>> posistionSortMap = new HashMap<Double, List <TextBox>>();
    for (TextBox key : textBoxPositionMap.keySet()) {
        Double 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) {
            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() == "") {
                            j++;
                        }
                    }
                }
            }

            //Delete the textbox
            if (j >= 2) {
                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
User avatar

Abel.He
 
Posts: 951
Joined: Tue Mar 08, 2022 2:02 am

Tue Feb 28, 2023 7:47 am

Hi, thanks for the code.

I tried the code you provided, but it still doesn`t work for me, can I send you my template?

mmarkozz
 
Posts: 8
Joined: Tue Jan 10, 2023 2:05 pm

Tue Feb 28, 2023 9:45 am

Hello,

Thanks for your feedback.
Yes, please offer your template to help us do further investigation, you can attach here or send it to us via email (support@e-iceblue.com). Thanks for your assistance in advance.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 951
Joined: Tue Mar 08, 2022 2:02 am

Fri Mar 03, 2023 9:54 am

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
User avatar

Abel.He
 
Posts: 951
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc