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
Exception
{
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.setLockContentControl(true);
tag.setLockContents(false);
tag.getRange().replace( "Click here to enter text", targetContent.getName(), findReplaceOptions );
return tag;
}