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.
Mon Apr 27, 2020 5:24 pm
Hi
I'm currently trying to remove empty lines from a word document (where I have removed tables)
I found an example of how this done in C# but having difficulty replicating in Java
Would it be possible to get the equivalent in Java?
Thanks,
Shane
- Code: Select all
{
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
foreach (Section section in doc.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()))
{
section.Body.ChildObjects.Remove(section.Body.ChildObjects[i]);
i--;
}
}
}
}
string result = "result.docx";
doc.SaveToFile(result, FileFormat.Docx2013);
}
Last edited by
shanerussell86 on Mon Apr 27, 2020 5:28 pm, edited 1 time in total.
-
shanerussell86
-
- Posts: 5
- Joined: Wed Mar 11, 2020 4:05 pm
Mon Apr 27, 2020 5:26 pm
Here's my Java equivalent so far
- Code: Select all
for (int s = 0; s < doc.getSections().getCount(); s++) {
Section section = doc.getSections().get(s);
for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
if (section.getBody().getChildObjects().get(i).getDocumentObjectType() == DocumentObjectType.Paragraph) {
if (section.getBody().getChildObjects().get(i).getDocumentObjectType().Paragraph.toString().trim().isEmpty()) {
section.getBody().getChildObjects().remove(section.getBody().getChildObjects().get(0));
i--;
}
}
}
}
-
shanerussell86
-
- Posts: 5
- Joined: Wed Mar 11, 2020 4:05 pm
Tue Apr 28, 2020 1:27 am
Hello,
Thanks for your inquiry.
Below is the code for your reference. Feel free to contact us if you have further questions.
- Code: Select all
Document doc = new Document();
doc.loadFromFile("input.docx");
for (int s = 0; s < doc.getSections().getCount(); s++) {
Section section = doc.getSections().get(s);
for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
if (section.getBody().getChildObjects().get(i).getDocumentObjectType() == DocumentObjectType.Paragraph) {
Paragraph para = (Paragraph) section.getBody().getChildObjects().get(i);
if (para.getText().trim().isEmpty()) {
section.getBody().getChildObjects().remove(para);
i--;
}
}
}
}
doc.saveToFile("out.docx");
Sincerely,
Rachel
E-iceblue support team
-
rachel.lei
-
- Posts: 1571
- Joined: Tue Jul 09, 2019 2:22 am
Tue Apr 28, 2020 8:11 am
That worked perfectly
Thanks Rachel
-
shanerussell86
-
- Posts: 5
- Joined: Wed Mar 11, 2020 4:05 pm
Tue Apr 28, 2020 8:25 am
Hello,
Glad to hear that!
Just feel free to contact us if you need further assistance. Wish you all the best!
Sincerely,
Rachel
E-iceblue support team
-
rachel.lei
-
- Posts: 1571
- Joined: Tue Jul 09, 2019 2:22 am