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

Free Support Forum - aspose.com

Remove certain paragraphs and table from existing document

Hi

We’ve got a template-system here based on word-templates which are merged using aspose.words. This works just great.

Now we have a special problem. We have a part in a document (template) that contains a paragraph (without merge-fields) followed by a table (only merge-fields). During mail-merge we have to check in the database-record for a flag. If this flag is set, then the paragraph and the table have to be deleted from the resulting file. (Please note: before and after this section with the paragraph and the table there are and might be many other paragraphs, tables, sections, etc.)

How can I do this?

I know I can work with sections, but as far as I know I don’t have a possibility to name sections. So I would have to get the section by index. But what if the template-admin (just a regular MS-Word-User) makes some changes to the template (e.g. inserting new sections), then this approach leads to problems.

Any help would be highly appriciated.

Regards,
Andy

Hi Andy,

Please use the following code snippet to achieve your requirements. Please find sample doc file and HandleMergeField code in attachment.

// Load the template document.
Document doc = new Document(MyDir + "Template.doc");
// Setup mail merge event handler to do the custom work.
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
// This is the data for mail merge.
String[] fieldNames = new String[] {"RecipientName", "SenderName", "FaxNumber", "PhoneNumber", "Subject", "Body", "Urgent", "ForReview", "PleaseComment"};
Object[] fieldValues = new Object[] {"Josh", "Jenny", "123456789", "", "Hello", "Test message 1", true, false, true};
// Execute the mail merge.
doc.MailMerge.Execute(fieldNames, fieldValues);
// Save the finished document.
doc.Save(MyDir + "Template Out.doc");

Hi Tahir

Thanks for your quick reply.
I’ll test the callback-method for deleting the table.

Unfortunately we have the problem, that before this table we have a paragraph containing some text explaining the content of the following table (in fact, the table can contain several records, so we use TableStart/TableEnd). This paragraph sould be deleted as well.

To make things even more complicated, we cannot be sure whether the paragraph with the explanation comes before or after the table and we don’t know how many sentences/paragraphs the explanation contains.

So, is there any way to mark some regions in a word-file in a way we can access with aspose.words so that we can delete it from the dom?

Regards,
Andreas

Hi Andreas,

Thanks for your inquiry.

I think you can use bookmarks to mark these parts of your document so they can be easily removed during mail merge. Please see the following article for details: https://docs.aspose.com/words/java/working-with-bookmarks/

If I can help you with anything else, please feel free to ask.

Thanks,

Hi Adam

Thanks for your input.
For testing I marked the table and the paragraph as bookmark (named “TEST”) and then used the following code:

Dim doc As New Document(Server.MapPath("test.doc"))
Dim bookmark As Bookmark = doc.Range.Bookmarks("TEST")
bookmark.Text.Remove(0, bookmark.Text.Length)
doc.Save(Server.MapPath("test.pdf"), SaveFormat.Pdf)

But in the resulting PDF the text (paragraph and table) is still there…

Can you tell me what I did wrong?

Regards,
Andy

Hi

Sorry bothering you, please ignore my last post except you find that the following solution would not solve my problem in every possible scenario.

I found the solution for deleting bookmarks with its content in the followin thread: How to remove a bookmark and its content

I tried this and found, that “RemoveBookmarkWithContent” from Emanual just did what I wanted it to do.

So, the solution with bookmarks is a valid way for regular MS Word-Users to mark and name sections in a file. The option of deleting the whole content of a bookmark does the rest. My problem should be solved.

Thanks for your support.

Regards,
Andy

Hi Andy,

It is nice to hear form you that you have solve the problem. Let us know, If you have any more queries.