Bookmarks in Table

Hi,

Can you please tell me how to handle the table that is having bookmarks?

I am using bookmarks for conditional hide or display of data.

It works fine everywhere in doc but when I have bookmarks in a table which is implemented using MailMerge feature of Aspose then it is throwing NullPointerException at some places & at some places when the data is iterated for the 2nd time in table, it is not replacing the MergeFields in bookmark.

Can you please help me out or suggest how to handle the bookmarks in table?

Sample doc :

----------------------------------------------------------------------------------------------------

<if «field=‘ADDITIONAL_COVER’ value =‘true’»>

«TableStart:Covers»Cover Type: «field=‘ADDITIONAL_COVER_NAME’»

Cover Amount: «field=‘RIDER_SUM_ASSURED_AMOUNT’» <if «field=‘ADDITIONAL_COVER_NAME’ value=‘HOS’»> daily <if «field=‘ADDITIONAL_COVER_NAME’ value=‘WOP’»> per «field=‘RIDER_PREMIUM_FREQUENCY’»

Currency: «field=‘POLICY_CURRENCY_CODE’»

<if not «field=‘PRODUCT_TYPE’ value=‘2’» and «field=‘ADDITIONAL_COVER_NAME’ value=‘CIC’, ‘TPD’, ‘CAN’»>

Payment Amount: «field=‘MODAL_PREMIUM’»

Currency: «field=‘RIDER_CURRENCY’»

Frequency: «field=‘RIDER_PREMIUM_FREQUENCY’»

Term: Expiry date : «field=‘RIDER_MATURITY_DATE’»

«TableEnd:Covers»

---------------------------------------------------------------------------------------------------------------------

It is throwing NullPointerException where marked red & not replacing mergeFields in 2nd iteration where marked green.

Thanks,

Bhoomica

Hi

Thanks for your request. Please attach your document for testing and provide me code that will allow me to reproduce the problem. I will investigate the issue and provide you more information.

Best regards.

PFA, the template & code.

Can you please tell me how to make a new copy of the existing bookmark so that every time I modify a bookmark it doesn’t affect the original bookmark but the copy of it.

Thanks

Bhoomica

Hi

Thank you for additional information. Word document cannot contain few bookmarks (or formfields) with same name. Therefore, you should rename bookmarks (formfields) during mail merge with regions. See the following forum thread to learn more:

Here is code that should resolve the problem.
// Add a hadler for the MergeField event.document.getMailMerge().addMergeFieldEventHandler(new HandleMergeFieldRenameBookmarks());
document.getMailMerge().executeWithRegions(fundMailMergeDS);
document.getMailMerge().executeWithRegions(coverMailMergeDS);
document.getMailMerge().executeWithRegions(penaltyMailMergeDS);
document.getMailMerge().executeWithRegions(gRateMailMergeDS);

public class HandleMergeFieldRenameBookmarks implements MergeFieldEventHandler
{
//Index of bookmarkprivate int mBookmarkIndex = 0;
public void mergeField(Object sender, MergeFieldEventArgs e) throws Exception
{
if (e.getFieldName().equals(“field=‘ADDITIONAL_COVER_NAME’”))
{
//Get parent node of mergefiledCompositeNode node = (CompositeNode)e.getField().getStart().getAncestor(NodeType.ROW);
//Rename bookamrks
for(int i=0; i<node.getRange().getBookmarks().getCount(); i++)
{
node.getRange().getBookmarks().get(i).setName(node.getRange().getBookmarks().get(i).getName() + mBookmarkIndex);
}
mBookmarkIndex++;
}
}
}

Hope this helps.Best regards

Hi,

Thanks for the help. It is really helpful but I still have an issue in this.

I am attaching the code & template.

My problem is that when the records in the table having bookmarks iterate, at that time at 1 bookmark there is no replacement of the mergefield in & thus the bookmark replacement condition doesn’t satisfy.

Can you please guide me why the MergeField replacement is not happening in 2nd n henceforth iterations in Additional Cover table.

I have marked the line which is not getting replaced in template in red.

Thanks

Bhoomica

Hi

Thanks for your request. I think you can resolve this issue by removing non-merged mergefileds from the document. See the following code for instance:

// Add a handler for the MergeField event.

document.getMailMerge().addMergeFieldEventHandler(

new HandleMergeFieldRenameBookmarks());

document.getMailMerge().executeWithRegions(fundMailMergeDS);

document.getMailMerge().executeWithRegions(coverMailMergeDS);

document.getMailMerge().executeWithRegions(penaltyMailMergeDS);

document.getMailMerge().executeWithRegions(gRateMailMergeDS);

//Remove non-merged mergefields

document.getMailMerge().deleteFields();

Hope this helps.

Best regards.