Free Support Forum - aspose.com

Merged Text Prints in Different Font During Mail Merge

Hello

I am upgrading aspose.word for java to the latest version.

I get a problem about font.

I have a lot of existing word template. And after the templates are merged, doc files are generated, I find that the first symble of a lot of words looks smaller than the symber behind it.
For example, for '<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>ZH-CN</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:SpaceForUL/>
<w:BalanceSingleByteDoubleByteWidth/>
<w:DoNotLeaveBackslashAlone/>
<w:ULTrailSpace/>
<w:DoNotExpandShiftReturn/>
<w:AdjustLineHeightInTable/>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
<w:UseFELayout/>
</w:Compatibility>
<w:DoNotOptimizeForBrowser/>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;}

<![endif]–><span style=“font-size:8.0pt;font-family:
“Arial”,“sans-serif”;mso-fareast-font-family:宋体;mso-ansi-language:EN-US;
mso-fareast-language:EN-US;mso-bidi-language:AR-SA;mso-no-proof:yes” lang=“EN-US”>06<span style=“font-size:12.0pt;font-family:“Times New Roman”,“serif”;
mso-fareast-font-family:宋体;mso-ansi-language:EN-US;mso-fareast-language:EN-US;
mso-bidi-language:AR-SA” lang=“EN-US”>/01/2011’ which is a merged value, font size of ‘06’ looks smaller than ‘<span style=“font-size:8.0pt;font-family:
“Arial”,“sans-serif”;mso-fareast-font-family:宋体;mso-ansi-language:EN-US;
mso-fareast-language:EN-US;mso-bidi-language:AR-SA;mso-no-proof:yes” lang=“EN-US”><span style=“font-size:12.0pt;font-family:“Times New Roman”,“serif”;
mso-fareast-font-family:宋体;mso-ansi-language:EN-US;mso-fareast-language:EN-US;
mso-bidi-language:AR-SA” lang=“EN-US”>/01/2011’. Font size of ‘06’ is 8, font style is ‘Arial’, but font size of ‘<span style=“font-size:8.0pt;font-family:
“Arial”,“sans-serif”;mso-fareast-font-family:宋体;mso-ansi-language:EN-US;
mso-fareast-language:EN-US;mso-bidi-language:AR-SA;mso-no-proof:yes” lang=“EN-US”><span style=“font-size:12.0pt;font-family:“Times New Roman”,“serif”;
mso-fareast-font-family:宋体;mso-ansi-language:EN-US;mso-fareast-language:EN-US;
mso-bidi-language:AR-SA” lang=“EN-US”>/01/2011’ is bigger and font style is ‘Times New Roman’.

I search this problem in internet. An articl in support center of Microsoft describes this problem. This is the link: http://support.microsoft.com/kb/212349

This article gives three workarounds.
Method 1: Add the Charformat Switch. Edit the merge field and add the Charformat switch.
Method 2: Change the Normal Style. You can modify the built-in Normal style to match the font that you want.
Method 3: Merge to a New Document and Make Formatting Changes

These workarounds only for word. I have a lot of templates to handle. It’s impossible for me to do this mannually. Could you please tell me to change the normal style in runtime? I prefer method 2. Or you have other way to solve this problems?
Maybe you can read this article in support center of Microsoft.

Thank you very much

Hi Shaohui,


Thanks for your inquiry.

To be able to apply custom formatting during Mail Merge, you should handle IFieldMergingCallback.FieldMerging event. For more information, please see the following link:
http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-apply-custom-formatting-during-mail-merge.html

Moreover, to change merge field style and formatting, please see the following sample implementation of IFieldMergingCallback.FieldMerging event:

private class
HandleMergeFieldAlternatingRows : IFieldMergingCallback<o:p></o:p>

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

if (mBuilder == null)

mBuilder = new DocumentBuilder(e.Document);

if (e.FieldName.Equals("yourDateFieldName"))

{

mBuilder.MoveToMergeField(e.FieldName);

mBuilder.Font.StyleIdentifier= StyleIdentifier.DefaultParagraphFont;

Run run = new Run(mBuilder.Document);

run.Text = e.FieldValue.ToString();

// You can set font here

Aspose.Words.Font font = run.Font;

font.Bold = true;

font.Color = Color.Red;

mBuilder.InsertNode(run);

}

}

public void ImageFieldMerging(ImageFieldMergingArgs args)

{

throw new NotImplementedException();

}

private DocumentBuilder mBuilder;

}


I hope, this will help.

Best Regards,

Hi,


In addition to my previous reply, please note that the formatting of merge fields, upon Mail Merge execution, should be preserved and that is not happening in your case. Could you please attach your template document here for testing? We will investigate and try to reproduce this issue on on our side and then provide you more information.

Best Regards,

Hi awais.hafeez:

Thanks for your answer. But I think you misunderstood me.
The style is
defined in word file. For the same field, in different file, its style
is different. The style is defined by user, but not set in runtime.



Here is my template file and the result file.



I am looking forward to your response.

Thank you very much!

Hello

Thank you for additional information. Please try using the following code to avoid this behavior:

Document doc = new Document("C:\\Temp\\DocTemplate.Invoice.doc");

DataTable dt = new DataTable("InvoiceItem");

dt.Columns.Add("InvoiceItem.SubscriptionNumber");

dt.Columns.Add("InvoiceItem.RatePlanName");

dt.Columns.Add("InvoiceItem.Name");

dt.Columns.Add("InvoiceItem.Quantity");

dt.Columns.Add("InvoiceItem.UnitPrice");

dt.Columns.Add("InvoiceItem.ServicePeriod");

dt.Columns.Add("InvoiceItem.ExtendedPrice");

//Add some data

dt.Rows.Add(new object[]

{

"SubscriptionNumber1", "RatePlanName1", "Name1", "Quantity1", "UnitPrice1",

"07/01/2011-07/31/2011", "ExtendedPrice1"

});

dt.Rows.Add(new object[]

{

"SubscriptionNumber2", "RatePlanName2", "Name2", "Quantity2", "UnitPrice2",

"07/01/2011-07/31/2011", "ExtendedPrice2"

});

doc.MailMerge.FieldMergingCallback = new HandleMergeFieldAlternatingRows();

doc.MailMerge.ExecuteWithRegions(dt);

doc.Save("C:\\Temp\\out.doc");

private class HandleMergeFieldAlternatingRows : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

if (mBuilder == null)

mBuilder = new DocumentBuilder(e.Document);

if (e.FieldName.Equals("InvoiceItem.ServicePeriod"))

{

mBuilder.MoveToMergeField(e.FieldName);

Run run = new Run(mBuilder.Document);

run.Font.Name = mBuilder.Font.Name;

run.Font.Size = mBuilder.Font.Size;

run.Text = e.FieldValue.ToString();

mBuilder.InsertNode(run);

}

}

public void ImageFieldMerging(ImageFieldMergingArgs args)

{

throw new NotImplementedException();

}

private DocumentBuilder mBuilder;

}

Best regards,

Thanks you, AndreyN!

Your code works very well.

I have one question for you.
What the meanning of <span style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:Calibri;
mso-fareast-theme-font:minor-latin;mso-ansi-language:RU;mso-fareast-language:
EN-US;mso-no-proof:yes”>‘mBuilder.Font.Name’?
Does it stand for the global default font style?

If the global style is different field style, how it works?

Thanks again!

Hello

Thanks for your inquiry. 'mBuilder.Font.Name' it is font of FieldName. Please press Alt-F9 to see the field code. As you can see FieldName->Arial, FieldCode and FieldSeparator->TimesNewRoman. Actually it is the reason of the problem.

Best regards,

Hi,

I run into this issue again when I am working on the same project of evaluating the upgrading Aspose word for Java from 2.4.1 to 11.5.0 that Shaohui worked on before.

I have two questions:

Q1: Does this is a regression? For the same template, it can be generated correct word in version 2.4.1 but this issue will happen in v11.5.0.

Q2: Is there some global setting to control this behavior instead of using FieldMergeCallback?

In our template, there are hundreds merge field and the leading words in those merge fields are smaller then the following words just as the previous description Shaohui wrote. So it is hard to use the if-else statement to adjust all merge field`s Font.

I am concerning about this approach`s performance. If the output word has more then 50 pages, the performance may have serious degradation with this field by field font adjustment.

I am looking forward to your reply since this is very important for me to decide when and how to upgrade our Aspose component to latest version.

Any feedback?


The problem is part of the merged field using the font of “FieldName”
and part of the merged field using the font of Normal style.



For example:

"United States" is the value of Country with font Arial 9. You can see the "United" is rendered correctly but the "States" is wrong, the font is "Times New Roman 12".

Hope anyone from Aspose can check if this is regression defect.

Hi Cosmo,


Thanks for your inquiry and sorry for the delayed response. I am working over your query and will get back to you as soon as possible.

Best Regards,

Hi Cosmo,


Thanks for your patience.

While using the latest version of Aspose.Words i.e. 11.5.0, I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system as a regression. The issue ID is WORDSNET-6674. Your request has been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best Regards,

Thanks for your reply. I am glad to see your guys take action to fix it. Hope it can be released with 11.6.0 so that I have enough time to retest it.

Hi Cosmo,


Thanks for your inquiry. Unfortunately, your issue is not resolved yet. Our development team is currently doing analysis of this issue. We will inform you as soon as your issue is resolved.

Best Regards,

The issues you have found earlier (filed as WORDSNET-6674) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Could you confirm that this issue has been fixed and no regression on 13.2.0?

From our testing result, we found that this issue still exist in Aspose.Word 13.2.0
And I know new Run set Font can resolve this issue, but it will cost a lot of memory from our observation since we have large number of records during mailmerge. And moveToMergeField and new Font will cost a lot of memory consumption.

Hi Guilin,

Thanks for your inquiry. Yes, the fix to WORDSNET-6674 was integrated in Aspose.Words 11.7.0. However, to ensure a timely and accurate response please supply us with the following information, if you cannot supply us with this information we will not be able to investigate your issue and raise a ticket.

  • What version of Aspose.Words for Java are you using?
  • What environment are you running on?
    • OS (Windows Version or Linux Version)
    • Architecture (32 / 64 bit)
    • MS Office Version
    • Any Third Party Libraries / SDK in addition to Aspose Products
  • Please supply us with the code from your application that is causing the issue
  • Please supply us with the input document that is causing the issue
  • Please supply us with the output document showing the undesired behavior

As soon as you get these pieces of information to us we'll start our investigation into your issue.

Many thanks,