Free Support Forum - aspose.com

CleanupOptions to fully remove empty paragraphs

We have encountered problems when trying to use a MailMerge’s CleanupOptions to fully remove empty paragraphs. In most cases this works, but we found that if multiple MergeFields are embedded within a single If Field, any blank ones are NOT removed from the generated document. I suspect the Options don’t recognise any whitespace that falls outside a MergeField as ‘empty’.<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

If you examine the following code, we made 3 attempts to fully remove all empty paragraphs. #1, simply using the RemoveEmptyParagraphs setting, #2 by combining RemoveEmptyParagraphs, RemoveUnusedRegions, RemoveUnusedFields and RemoveContainingFields and #3 by implementing an IFieldMergingCallback. The only solution we got to work was #3 and then, only when the callback’s RemoveEmptyParagraphs method is invoked between MailMerge.Execute(…) and UpdateFields(); if it is invoked after UpdateFields() it doesn’t have any effect, even though I can see the code attempting to remove empty Paragraphs.

Note, this solution only attempts to remove Paragraphs that surround one or more MergeFields, that are left empty after any empty MergeFields have themselves been removed. Therefore, it has no effect on any empty Paragraphs that don’t have any MergeFields within them and is, therefore, relatively ‘safe’.

this.mailMergedDocument = new Document(letterFileName);

// #1 this.mailMergedDocument.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;

/* #2 this.mailMergedDocument.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs

| MailMergeCleanupOptions.RemoveUnusedRegions

| MailMergeCleanupOptions.RemoveUnusedFields

| MailMergeCleanupOptions.RemoveContainingFields;*/

// #3 this.mailMergedDocument.MailMerge.FieldMergingCallback = new HandleMergeField(this.mailMergedDocument);

HandleMergeField hmf = new HandleMergeField(this.mailMergedDocument);

this.mailMergedDocument.MailMerge.FieldMergingCallback = hmf;

this.mailMergedDocument.MailMerge.Execute(dataTableDataSource);

/* Only works with this call in this position, between MailMerge.Execute(...) and UpdateFields().

* The CleanupOptions setting seems to be irrelevant; the RemoveEmptyParagraphs() works with or

* without it. */

hmf.RemoveEmptyParagraphs();

/// <erge in of fixes: Converts MailMerge fields to static text.

this.mailMergedDocument.UpdateFields();

FieldsHelper.ConvertFieldsToStaticText(this.mailMergedDocument, Aspose.Words.Fields.FieldType.FieldIf);

///

/// HandleMergeField class

///

public class HandleMergeField : IFieldMergingCallback

{

///

/// DocumentBuilder object

///

private readonly DocumentBuilder docBuilder;

///

/// Initializes a new instance of the class.

///

/// The document

internal HandleMergeField(Document doc)

{

this.docBuilder = new DocumentBuilder(doc);

}

///

/// Called for every merge field encountered in the document.

/// This simply captures the owning Paragraphs.

///

/// Field Merging Arguments

public void FieldMerging(FieldMergingArgs args)

{

if (args != null)

{

// Capture parent Paragraph.

Paragraph p = args.Field.Start.ParentParagraph;

if (!mPara.Contains(p)) {

mPara.Add(p);

}

}

}

///

/// Image field merging.

///

/// Image Field Merging Arguments

public void ImageFieldMerging(ImageFieldMergingArgs args)

{

// Do Nothing for the moment used it when we use images like signature

}

public void RemoveEmptyParagraphs() {

foreach (Paragraph p in mPara) {

// Remove all empty MergeFields

foreach(Field f in p.Range.Fields) {

if (string.IsNullOrEmpty(f.Result))

{

f.Remove();

}

}

// If the resulting Paragraph is left empty, remove it.

if (string.IsNullOrEmpty(p.Range.Text.Trim()) && p.ParentNode != null) {

p.Remove();

}

}

mPara.Clear();

}

private ArrayList mPara = new ArrayList();

}

Any comments are most welcome.

Regards,v

Hi Allal,


Thanks for your inquiry. Please upgrade to latest version of Aspose.Words from the following link. I hope, this helps:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

In case the problem still remains, please attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output document which shows the undesired behavior
  • Your expected document which shows the correct output. Please create this document using Microsoft Word application.
  • Please create a standalone Console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,