Free Support Forum -

Some Tricky Problems


There are a couple of things we’ve been trying to do which we either can’t implement or are looking for ‘better’ solutions to. They are certainly at the top end of the complexity scale but they would be of huge benefit to us, and hopefully others:

1) I have a document with the following line in it:
Label : {value}
And if value is empty there are two possible scenarios
a) I want to completely remove the whole line.
b) I want to remove all the text but not actually delete the line as I might need it to maintain the layout further down the document.

Bear in mind that a single document may have instances of both scenarios. The ‘obvious’ solution is to put the label into the mergefield but that means we’re mixing presentation with data and reduces the ‘user friendliness’ of the template definition (i.e. the user needs to know that that field contains a label plus data and if we want to use the data gain elsewhere it’ll have to appear a second time in the data source).

2) We have a field that needs to appear on the last page of a document whose page count we don’t know. Any ideas how to do this? Put it on all pages and somehow hide/delete it from all but the last page?

A natural extension of this second issue is to handle fields that only appear on the first page of a document. This leads on to other ‘extensions’ but let’s not go there right now :wink:

As I say I realise these are stretching the functionality but I would appreciate it if you could give them some thought.

Thanks in advance,

Hi Phil,

1. Removing text from the document can be resolved when we implement Find and Replace funcionality. You will be able to remove whole paragraph if you include the paragraph mark in the find expression. I cannot give firm estimates for this feature yet, can be sometime in July or August.

2. I’m not sure about the problem with the last page. While in Aspose.Word you never deal with pages because only MS Word will paginate the document. So if you just have your field at the end of the document it will appear on the last page. Do you need to be able to insert a field using DocumentBuilder or what?

Hi Roman,

Ah, yes I see I simplified my problem in 2) to the point where I actually removed the problem statement!

Specifically what we’re doing is creating an invoice. This invoice can run to any number of pages (often it will be only one) depending on how many items are in it. The two key points are:
1) Each page contains a table of the SAME SIZE to hold the list of bill items. This way a bill with 2 items on a page and a bill with 8 items on a page will have the same structure and layout.
2) The last page needs the total printing at the bottom.

So here’s what we’ve done:
a) Used Word drawing objects to create the outline of the table
b) To ensure this appears on every page we’ve put it in the footer (although it actually extends two-thirds of the way up the page).
c) On the main page itself we’ve inserted a table object with invisible borders containing the repeating fields and set the page margins so that the table object remains inside the drawing object.

This way the table object expands as needed and if it goes onto an additional page we instantly get a full size table appearing. The problem is how do we get the total to appear in the correct position (at the bottom of the table) and only on the last page?


P.S. There are alternative solutions e.g. put the total in the second row of the expanding table in which case it will appear directly beneath the repeating elements rather than at the bottom of the page) so this isn’t a showstopper but I’d love to know if there is a way of implementing exactly this requirement.

Yes, it is a bit complicated to make sure the totals are positioned at the bottom of the last page. But I think the complication comes more from MS Word rather than from Aspose.Word in this case.

There is no easier way that I know of to do what you want.

Just one additional idea to your solution. If your invoice is of fixed layout and you know how many rows can appear on every page you can calculate how many extra empty rows or just paragraph breaks to insert after the main table before the totals to make sure the totals are neatly tucked to the bottom.

To do that you can either just add the required number of empty rows to your data source or navigate to the beginning of the totals table using DocumentBuilder.MoveToBookmark method and insert several paragraphs using DocumentBuilder.InsertParagraph.

Thanks for your thoughts on this.
You are absolutely right about the problem being with Word rather than Aspose.

Aspose.Word 2.0.2 has a new Range.Replace method that allows for simple find and replace. For more info please see:

romank wrote:

1. Removing text from the document can be resolved when we implement Find and Replace funcionality. You will be able to remove whole paragraph if you include the paragraph mark in the find expression. I cannot give firm estimates for this feature yet, can be sometime in July or August.


I have a similar problem to this where in my template documents I have lines such as {{Address_3}}. Note that this is just text - not a field of any type - as these template documents are created through a HTML word-style editor. Ideally if the datasource used for constructing the destination documents contains no such column or if the value of the data is blank, I would like to remove the line (behaviour identical to MailMerge.RemoveEmptyParagraphs = true).

Are you saying this is possible using Range.Replace()? If so, how? Although I'm not sure what you mean by "paragraph mark", wouldn't using Range.Replace() with the intention of deleting an empty line remove ALL empty lines (my template documents consist of just one section) or is there a way to specify which empty lines to remove?

Many thanks,

You can try to use Range.Replace to replace your “fields” with new text, but this is far from what MailMerge does for you with real merge fields.

Range.Replace is limited to replacing text withing a single run (sequence of characters with same formatting) and within a single paragraph at the moment. You can increase chances of Range.Replacing working better by disabling fast saves in MS Word Tools/Options.

Sorry if I did not provide a useful answer, I did not fully understand your question probably.