We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace String Text in Word Document with Block or Inline Level Content Controls (StructuredDocumentTag) using Java

Hi, we are trying to replace a word in word document with StructuredDocumentTag by looping all paras in the document. we are able to insert StructuredDocumentTag of INLINE but we need to insert a StructuredDocumentTag of BLOCK level. when we change the element to BLOCK levele we are getting an error “Cannot insert a node of this type at this location.”.

Can we insert a StructuredDocumentTag element of BLOCK level.

Code snippet:

  document = new com.aspose.words.Document( encorrDocStream );
  final NodeCollection<?> paraCollections = document.getChildNodes( NodeType.PARAGRAPH, true );
  for ( final Object paraObj : paraCollections )
    final Paragraph para = (Paragraph)paraObj;
    final RunCollection paraRun = para.getRuns();
    for ( int paraPos = 0; paraPos < paraRun.getCount(); paraPos++ )
      if ( paraRun.get( paraPos ).getText().equals("some word")  )
              paraRun.removeAt( paraPos );
              paraRun.insert( paraPos, getContentStructuredDocTag( letterHeadContent, 
                   document ) );
private StructuredDocumentTag getContentStructuredDocTag( final Content targetContent,
                                                        final com.aspose.words.Document document ) throws 
    final StructuredDocumentTag tag = new StructuredDocumentTag( document,
                                                             ( ( targetContent.getContentType()
                                                                              .equalsIgnoreCase( CCUConstants.IMAGE_CONTENTS ) ) ? SdtType.PICTURE
                                                                                                                                 : SdtType.RICH_TEXT ),
                                                             MarkupLevel.INLINE );
     tag.setTitle( targetContent.getElement().getContentType() + ":" + targetContent.getName() );
     tag.setTag( "content:{\"name\":\"" + targetContent.getName() + "\",\"uuid\":\"" + targetContent.getUuid() + "\"}" );
     final FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
     findReplaceOptions.setFindWholeWordsOnly( true );
     findReplaceOptions.setMatchCase( true );
     tag.getRange().replace( "Click here to enter text", targetContent.getName(), findReplaceOptions );
     return tag;


Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for Java 20.1 generated output document showing the undesired behavior (if any)
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word and attach it here for our reference

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