Free Support Forum - aspose.com

Delete empty paragraph problem

Hello, I send you a document result.

I would like to delete empty paragraph.
In my example I want to remove : Couche intermédiaire 2 :
here is my code




doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImageField);

doc.MailMerge.ExecuteWithRegions(dataSet);

doc.MailMerge.RemoveEmptyParagraphs=true;


Hi,

Sorry I probably misdirected you in the previous conversation. RemoveEmptyParagraphs does not seem to be applicable here, it will not remove a paragraph that contains some additional text. Thus you can try one of the following workarounds:

1. Use DocumentBuilder to remove the paragraph after mail merge.

2. Use a mail merge region like this: <>:<> (requires data structure change).

3. Add Couche intermédiaire 2 to the data field value inside the merge handler.

I think 1 is the most applicable way.

We realize that these approaches are not very convenient. So we will possibly implement some special handling for this in the future.

Hi,
Thank you,
If proceed like this :

2. Use a mail merge region like this: <>:<> (requires data structure change).

3. Add Couche intermédiaire 2 to the data field value inside the merge handler.

I will have a line feed (the line Couche intermédiaire 2 with a null value ).

How can we remove this line ?


ANOTHER QUESTION :

After applying doc.MailMerge.RemoveEmptyParagraphs=true , I have duplicate static text is my mail merge , how can we avoid this ?

Examples are joined.

Annexe after mail merge

Hi,

First, what regards to deleting a paragraph that contains an empty merge field.

Here is a workaround for you. Note it is applicable if only merge field named “Consommation” could have null value, but you can expand the example to get it working if other fields could be null too.

1. Remove Couche intermédiaire 2 from the template so that only merge field <> resides at that line.

2. Use the following code:


doc.MailMerge.RemoveEmptyParagraphs = true;

doc.MailMerge.MergeField += new MergeFieldEventHandler(MyHandler);

doc.MailMerge.ExecuteWithRegions(table);


private void MyHandler(object sender, MergeFieldEventArgs e)

{

// You can repeat this if some other fields could have null value

// and paragraphs that contain them should be removed

if (e.FieldName == “Consommation” && e.FieldValue != DBNull.Value)

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.FieldName);

builder.Font.Name = “Arial”;

builder.Font.Size = 12;

builder.Font.Bold = true;

builder.Write("Couche intermédiaire 2 : ");

builder.Font.Bold = false;

builder.Write(e.FieldValue.ToString());

}

}



Please report us if it works, we are glad to help you.

Regarding your second question, you should simply put the text being repeated (underlined bold) into a separate paragraph while in your template it is just separated with a line break.

Thank you.

The problem is that

if ((e.FieldName == “Consommation total”) && (e.FieldValue != DBNull.Value))

is always true ( e.FielValue !=DBNull.Value always true even if the field “Consommation” is empty.

Here is what I obtain :

Couche d’Impression : Application d’une couche IMPRIDERME nf (consommation 0,150 à 0,200 l/m2)

Couche intermédiaire 1: 1 couche KUBIK égalisateur

Couche intermédiaire 2 :

Couche de finition :
Application d’une couche CREPITEX STANDARD 1,5 à 3,5 kgm2

Regards

If the field value is an empty string (not null), please change

e.FieldValue != DBNull.Value

to

e.FieldValue.ToString() != String.Empty.

thank you,
this works but we have a line break at the old position of Couche Intermédiaire 2.
Is it possible to "delete " the empty line ?

Don't forget to use

doc.MailMerge.RemoveEmptyParagraphs = true;

before starting mail merge execution as I have specified in the example above. It should remove the empty paragraph and thus the line.

Thank you,
We 've used doc.MailMerge.RemoveEmptyParagraphs = true;
but the empty paragraph is not deleted.







doc.MailMerge.RemoveEmptyParagraphs = true;

//----- utilisable pour seigneurie------------------------------------------

doc.MailMerge.MergeField += new MergeFieldEventHandler(MyHandler);

//-----fin utilisable pour seigneurie--------------------------------------

//----- utilisable pour gauthier--------------------------------------------

// doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeImageField);

//-----fin utilisable pour gauthier-----------------------------------------

doc.MailMerge.ExecuteWithRegions(dataSet);

//doc.MailMerge.RemoveEmptyParagraphs=true;

doc.MailMerge.DeleteFields();



Strange, I’ve tested the code on your template (only Couche intermédiaire 2 has been removed) and it works.

Please check the following. Press Button image button on the Standard toolbar. The line in the template should look like

?Consommation? Paragraph mark

The point is that it shouldn’t contain any other symbols, only the merge field and the paragraph character. Remake the line if needed.

Which characters remain in the line after mail merge?

hi,
Any characters remain in the line after mail merge.

I think I’ve followed your steps.

See attached template and attached report.

Even the Title text RESPONSABILITE – GARANTIES – ASSURANCES :
(see page 3 and 4 of report ) appear several times.

Here is report.
Regards.

Thanks for the attachments.

The point is that the workaround suggested above is not related to this:

2. Use a mail merge region like this: <>:<> (requires data structure change).

This is just another approach. So if the workaround suits you, please do not use the construction

<>:<>,

as you have done in the attached template.

Just keep your template as it was in original, but change only one line:

Couche intermédiaire 2 : ?Consommation?
to
?Consommation?.
<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /><o:p>That’s it. I’ve attached a working project that contains your template with the “Systeme” region as it should look like; other places remain unchanged. I hope it will help; if it still doesn’t, feel free to post your issues as we are happy to help you.

Concerning the title that repeats several times: do not use line break (Shift-Enter) after it, use paragraph break (Enter) instead.</o:p>

Thank you very very very much !!!

The first problem is now resolved.

Concerning the title that repeats several times, if we do not use line break (Shift-Enter) after it, but use paragraph break (Enter) instead, The Title RESPONSABILITE – GARANTIES – ASSURANCES will be in a paragraph different of that who contents merge fields of Table Garantie.

So, even if Table Garantie returns any line, the title we’ll be displayed.

Is it possible to Delete the title if there are no text after ?

Sorry, there is no any special method to perform this because this case is pretty custom. You should implement your own code to control the title’s presence. Perhaps it could be done similar to the workaround used for adding Couche intermédiaire 2: inside the merge handler, check if any of the fields from the Garantie region is being populated; if so, insert the title using DocumentBuilder; do it only once.