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

Free Support Forum - aspose.com

Table AutoFit(AutoFitBehavior.AutoFitToContents) incorrect when it is nested in another table

Hi,


I am using Aspose .NET to generate a Mail Merge report.

Everything works fine, except when I place the “MyHtml” merge field in a table cell in the template document.

If “MyHtml” contains a HTML table, the HTML table is compressed horizontally even though I explicitly set the tables to Auto Fit to Contents.

The following is the code:

public override void GenerateReport()
{
string TemplatePath = Path.Combine(TemplateFolder, ReportTemplate.FileName);

// Open the template document.
var Doc = new Document(TemplatePath);

// Add a handler for the MergeField event.
HandleMergeFieldCustomLogic customLogicHandler = new HandleMergeFieldCustomLogic();
Doc.MailMerge.FieldMergingCallback = customLogicHandler;
Doc.NodeChangingCallback = customLogicHandler;

// Execute the nested mail merge with regions
DataSet ds = GetDataSet();
Doc.MailMerge.ExecuteWithRegions(ds);

foreach (Aspose.Words.Tables.Table table in customLogicHandler.InsertedHtmlTables)
{
table.AllowAutoFit = false;
table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.AutoFitToContents);
}
}


private class HandleMergeFieldCustomLogic : IFieldMergingCallback, INodeChangingCallback
{
private DocumentBuilder mBuilder;
private bool IsHtmlTable = false;
public readonly List InsertedHtmlTables = new List();

#region IFieldMergingCallback
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
if (mBuilder == null)
mBuilder = new DocumentBuilder(e.Document);

// This way we catch the beginning of a new row.
if (e.FieldName.Equals(“MyHtml”))
{
string FieldValue = e.FieldValue as string;
if (!string.IsNullOrWhiteSpace(FieldValue))
{
// Move to Field
mBuilder.MoveToField(e.Field, true);

#region Insert HTML

//Set this flag to true, so that we get only Inserted Html tables after the InsertHtml call
IsHtmlTable = true;
mBuilder.InsertHtml(FieldValue);

//Set this flag to false, so that we ignore all other tables after the InsertHtml call
IsHtmlTable = false;

#endregion Insert HTML

// Remove the string representation of HTML
e.Text = string.Empty;
}
}
}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing.
}

#endregion IFieldMergingCallback

#region INodeChangingCallback

void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
{
if (args.Node.NodeType == NodeType.Table && IsHtmlTable)
{
InsertedHtmlTables.Add(args.Node);
}
}

void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
{
// Do Nothing
}

void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
{
// Do Nothing
}

void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
{
// Do Nothing
}

#endregion INodeChangingCallback
}

Regards,
Joseph Wee

Hi Joseph,


Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output document showing the undesired behavior.
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.
  • 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,

Hi,


I have attached the 7z file containing the following:
  1. Sample Template.docx - Template File used
  2. Output.docx - Output Word document
  3. Desired.docx - The desired result
  4. Program.cs - the console program used to reproduce the problem
Thanks.

Regards,
Joseph Wee

Hi Joseph,


The archive “AsposeWordsTableAutoFit.7z” you attached seems corrupted. Please ZIP these resources and reattach them here for testing.

Best regards,

Hi,


I have re-uploaded the files requested.

Regards,
Joseph Wee

Hi Joseph,


Thanks for your inquiry. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-14583. Our product team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi Joseph,


Thanks for being patient. Regarding WORDSNET-14583, our product team has completed the work on your issue and has come to a conclusion that this issue and the undesired behavior you’re observing is actually not a bug in Aspose.Words for .NET. So, we will close this issue as ‘Not a Bug’. Please see the following details:

You have to “AutoFit” the parent table too. Please try using the following code:

...

Doc.MailMerge.ExecuteWithRegions(ds);

foreach (Aspose.Words.Tables.Table table in customLogicHandler.InsertedHtmlTables)

{

table.AllowAutoFit = false;

table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.AutoFitToContents);

}

Table tab1 = Doc.FirstSection.Body.Tables[0];

tab1.AllowAutoFit = false;

tab1.AutoFit(AutoFitBehavior.AutoFitToContents);

Doc.Save(MyDir + "17.5.docx");


Hope, this helps.

Best regards,