Free Support Forum - aspose.com

How to delete text in a document?

I’m using the document fill function.
​I want to achieve one of these functions.

​barcode:
«barcodeTag»

Then if there are no value to fill «barcodeTag», we want to delete ”barcode: “, that is a fixed text on my document.

​I use Aspose.words for Java 18.9 version,thanks.

@huawei

Thanks for your inquiry. It seems that you are using mail merge feature of Aspose.Words. If this is the case, we suggest you following solution.

  1. Implement IFieldMergingCallback interface.
  2. In IFieldMergingCallback.FieldMerging, please check the value of <<barcodeTag>> field using FieldMergingArgs.FieldValue property.
  3. If the value is null or empty, get the paragraph of mail merge field using FieldMergingArgsBase.Field.Start.ParentParagraph property.
  4. Remove the paragraph node using Paragraph.Remove method.

Please check the following code. Hope this helps you.

class MailMergeCallback implements IFieldMergingCallback {

    public void fieldMerging(FieldMergingArgs e) throws Exception {
        if(e.getFieldName() == "barcodeTag" && e.getFieldValue() == null)
        {
            e.getField().getStart().getParentParagraph().remove();
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
    }
}

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

Hi, tahir.manzoor.
When mergeFiled and text are not in the same line, they will not work.

tag3:«tag3#delete» is work.

tag1:
«tag1#delete» is not work.

@huawei

Thanks for your inquiry. In this case, the tag1: is in previous paragraph of mail merge field. You can get the previous sibling of paragraph using Node.PreviousSibling property. Once you have this node, please use Node.Remove method to remove it.

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

		if(field.getField().getStart().getParentParagraph().getText().startsWith("\u0013 MERGEFIELD")){
            DocumentBuilder builder = new DocumentBuilder(field.getDocument());
            builder.moveToMergeField(field.getFieldName());
            builder.getCurrentNode().getPreviousSibling().remove();
        }else {
            field.getField().getStart().getParentParagraph().remove();
        }

I tried with your method,but builder.getCurrentNode() return is null.

field.getField().getStart().getParentParagraph().getPreviousSibling().remove(); is not work.

How can I get the previous sibling of paragraph?

@huawei

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

I set a new template document , it is take effect to use this method.
Maybe I had some problems with the previous document label settings.
Thank you very much for solving my problem.
have a nice day :slight_smile:

@huawei

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.