Free Support Forum - aspose.com

Delete blank lines after removing table

hi Support,

I have using below statement for removing table.

Table table = (Table) document.getChild(NodeType.TABLE, 1, true);
table.remove();

Once this statement is removed, couple of blank lines are remained.

Please help removing these empty lines after the table is removed using Java.

Thanks,

@rgadipalli

Thanks for your inquiry. Please ZIP and attach your input and expected output Word documents here for our reference. We will then provide you with more information about your query along with code.

Testing.zip (18.8 KB)

Hi Tahir,

Please find my test case attached.

Thank You

@rgadipalli

Thanks for sharing the detail. Please use the following modified code to get the desired output. Hope this helps you.

private static void removeTableByIndex(Document document, int tableindex) throws Exception {

    Table table = (Table) document.getChild(NodeType.TABLE, tableindex, true);

    if(table != null && table.hasChildNodes()) {
        Node para = table.getNextSibling();
        table.remove();
        if(para.toString(SaveFormat.TEXT).trim().length() == 0)
            para.remove();
    }
}
1 Like

Thank you Tahir, this works for me.

I have another question, regrading locking the Tables programmatically or Should i create another thread or can i post in this thread itself?

Please let me know.

@rgadipalli

Thanks for your inquiry. EditableRange class represents a single editable range. EditableRange is a “facade” object that encapsulates two nodes EditableRangeStart and EditableRangeEnd in a document tree and allows to work with an editable range as a single object.

You need to add EditableRangeStart and EditableRangeEnd nodes into document that you want to edit and protect the document using Document.Protect method. Please do not enclose the table between these nodes.

If you still face problem, please ZIP and attach your expected output document here for our reference. We will then provide you more information about your query.

hi Tahir,

Can you please provide few samples where we run through a document and make few tables, paragraphs un-editable and keep rest of the document editable?

Thanks,

@rgadipalli

Thanks for your inquiry. Following code example shows how to make the document as read only except the second paragraph.

Document doc = new Document(MyDir + "in.docx");
doc.protect(ProtectionType.READ_ONLY);

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToParagraph(1, 0);
builder.startEditableRange();
builder.moveToParagraph(1, -1);
builder.endEditableRange();

doc.save(MyDir + "Out.docx");

Following code example shows how to makeread-onlyt table of document as read only.

Document doc = new Document(MyDir + "in.docx");
doc.protect(ProtectionType.READ_ONLY);
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentStart();
builder.startEditableRange();
builder.moveTo(table);
builder.endEditableRange();

builder.moveTo(table.getNextSibling());
builder.startEditableRange();
builder.moveToDocumentEnd();
builder.endEditableRange();
doc.save(MyDir + "Out.docx");

hi Tahir,
Testing.zip (18.5 KB)

I ran the sample code on document which contains list of tables. And i have couple of questions.

  1. Why the first cell in the table is still editable? and rest of the cells in table remain un-editable!
  2. And all editable ranges are with background yellow. Is this is the behavior of editable range class?

Attaching the input and output in ZIP.

Thank You

@rgadipalli

Thanks for your inquiry.

Please do not add EditableRangeStart and EditableRangeEnd nodes into the first cell of table. Please check the following code example.

Document doc = new Document(MyDir + "TestTableDelete.docx");
doc.protect(ProtectionType.READ_ONLY);
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveTo(table.getNextSibling());
builder.startEditableRange();
builder.moveToDocumentEnd();
builder.endEditableRange();

doc.save(MyDir + "18.8.docx");

This is expected behavior. Please note that Aspose.Words mimics the behavior of MS Word. If you perform the same scenario using MS Word, you will get the same output.