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.

Tue Jan 10, 2023 2:35 pm

Hello,

I have a problem removing empty lines, it removes the empty lines correctly, but it also removes my TextBoxes that should not be removed.

I attached the zip with inside 3 docx files:

- DocWithSpaces.docx (Is the file where I should delete the spaces)
- DocWithTextBoxDeleted.docx (Is the result file when I'm trying to remove the empty lines)
- DocIShouldGet.docx (Is the correct result that I should get).

Can anyone help me to solve this problem?

Thank you!

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

Wed Jan 11, 2023 6:45 am

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

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

Wed Jan 11, 2023 7:52 am

Thank you, I'm actually working in Java.

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

Wed Jan 11, 2023 8:46 am

Hello,

Thanks for your feedback.
For Java code, please refer to the following code:
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.
        for (Object sectionObj : document.getSections()) {
            Section section=(Section)sectionObj;
            for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
                if ((section.getBody().getChildObjects().get(i).getDocumentObjectType().equals(DocumentObjectType.Paragraph) )) {
                   String s= ((Paragraph)(section.getBody().getChildObjects().get(i))).getText().trim();
                    if (s.isEmpty()) {
                        Paragraph para = (Paragraph)section.getBody().getChildObjects().get(i);
                        //Traverse child objects of paragraph
                        for (int j = 0; j < para.getChildObjects().getCount(); j++) {
                            //If child objects of paragraph is "TextBox", clone the "TextBox" to the the first paragraph
                            if (para.getChildObjects().get(j).getDocumentObjectType() == DocumentObjectType.Text_Box) {
                                section.getParagraphs().get(0).getChildObjects().add(para.getChildObjects().get(j).deepClone());
                            }
                        }
                        section.getBody().getChildObjects().remove(section.getBody().getChildObjects().get(i));
                        i--;
                    }
                }
            }
        }
        String result = "output/output.docx";

        //Save to file.
        document.saveToFile(result, FileFormat.Docx_2013);


If you have any issue, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

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

Wed Jan 11, 2023 9:36 am

Still doesn't work, same result as previous one.

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

Wed Jan 11, 2023 9:56 am

Hello,

I created console project with Jdk1.8 and used the latest version of Spire.Doc 10.12.4 to execute the code I provide, I can get the expected result document. I attached the result document for your reference.
In addition, please offer the following message to help us do further investigation. Thanks for your assistance.
1) The JDK version, such as Oracle Jdk 1.8.
2) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).

Sincerely
Abel
E-iceblue support team
User avatar

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

Wed Jan 11, 2023 10:37 am

Okay, I did something wrong, now it's perfect, thank you!

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

Thu Jan 12, 2023 1:30 am

Hello,

Thanks for your feedback.
I'm glad to hear that your issue has been solved.
If you have any issue in the future, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

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

Return to Spire.Doc