Source document miss formatting

Hello All,
I’m having an issue while trying to convert a docx file to pdf. it loses all source document formatting (ex. bold text) , I’m using mailmerge feature to dynamically populate the forms. I tried several fixes but none with sucess,

Hi Frederico,

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

  • Your input document
  • Aspose.Words generated output DOCX/PDF files showing the undesired behavior
  • 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 further 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,

Thanks for the response.

I’m sending the doc files used as template and the resulting pdf after mailmerge fields; Notice how the header title in the pdf files is not entirely bolded, only the leading words.

I need to fix it ASAP.

Fred

Hi Fred,

Thanks for your inquiry. Aspose.Words mimics the way the Microsoft Word works. You can verify this by performing mail merge using Microsoft Word 2016 for example. To fix this issue, please select whole merge field and apply bold formatting. Repeat this for all merge fields you’re having problems with. Then re-save and execute mail merge using Aspose.Words. Hope, this helps.

Best regards,

Hi ,
Can you provide some sample code? The problem is that some fields should be bold and others not, but during mailmerge I’m unable to check if the field from the doc file is bold or not.
Thks

Hi Fred,

Please try using the following code to fix those merge fields programmatically.

Document doc = new Document(MyDir + @"1.3_Abertura_Livro_Unidade_Producao (2).docx");
ArrayList mfs = new ArrayList();
// Determine what fields have bold formatting applied to at least one Run
foreach (Field field in doc.Range.Fields)
{
    if (field.Type.Equals(FieldType.FieldMergeField))
    {
        Node currentNode = field.Start;
        bool isContinue = true;
        while (currentNode != null && isContinue)
        {
            if (currentNode.NodeType.Equals(NodeType.FieldEnd))
                isContinue = false;
            if (currentNode.NodeType.Equals(NodeType.Run))
            {
                if (((Run)currentNode).Font.Bold)
                {
                    mfs.Add(field);
                    break;
                }
            }
            Node nextNode = currentNode.NextPreOrder(currentNode.Document);
            currentNode = nextNode;
        }
    }
}
// Apply bold formatting to all Run nodes inside those merge field
foreach (Field field in mfs)
{
    if (field.Type.Equals(FieldType.FieldMergeField))
    {
        Node currentNode = field.Start;
        bool isContinue = true;
        while (currentNode != null && isContinue)
        {
            if (currentNode.NodeType.Equals(NodeType.FieldEnd))
                isContinue = false;
            if (currentNode.NodeType.Equals(NodeType.Run))
            {
                ((Run)currentNode).Font.Bold = true;
            }
            Node nextNode = currentNode.NextPreOrder(currentNode.Document);
            currentNode = nextNode;
        }
    }
}
doc.MailMerge.Execute(new string[] { "GovernoNome",
    "SecretariaNome",
    "OrgaoNome",
    "SetorNome"},
new object[] { "GOVERNO DO ESTADO DO ESPÍRITO SANTO",
    "SECRETARIA DE ESTADO DA AGRICULTURA, ABASTECIMENTO, AQUICULTURA E PESCA",
    "INSTITUTO DE DEFESA AGROPECUÁRIA E FLORESTAL DO ESPÍRITO SANTO",
    "Seção de Defesa Sanitária Vegetal"});
doc.Save(MyDir + @"16.12.0.pdf");

Best regards,

Awais,
It partially solved the problem , now the footer fonts are messed up. As you can see in the attached document the fonts in these fields are not bold but the fonts are different.
thanks

Hi Fred,

Can you please tell what final font formatting should the footer content have?

Font Name: Calibri (Body), Font Size: 11, Bold: true?
or
Font Name: Arial, Font Size: 8, Bold: true?
or something else?

We will investigate the issue further on our end and provide you more information.

Best regards,

Hello,
The problem is that. I have several different templates and the footer fonts are defined in these templates. Just as a side note, this problem only happens with the current version of the library, previously this issue didn’t occur. I tried several different fixes but the problem remains. I really need to fix the sooner. I also have noticed that only the leading words are correctly formatted after that the fonts gets mixed with the default font of the document and not the current field. Is there any other alternatives besides this mailmerge feature perhaps this is teh problem.
thanks.

Hi Fred,

Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 17.1.0, we managed to reproduce this issue on our end. We have logged this issue in our bug tracking system. The ID of this issue is WORDSNET-14800. Your thread has also been linked to the appropriate issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Can you please also tell us Aspose.Words’ version number for which there were no problems on your side previously?

Best regards,

The current working version is 9.7.0 and the current codebase was using version 16.1.0. Weird enough 9.7.0 keeps the correct formatting while the newer version loses all formatting data.

Hi Fred,

Regarding WORDSNET-14800, our product team has completed the work on your issue and has come to a conclusion that this issue is not a bug in latest versions of Aspose.Words. Your issue (WORDSNET-14800) has now been closed with ‘Not a Bug’ resolution.

It is just a situation where very old version of Aspose.Words used to work incorrectly (not like MS Word) and that satisfied your requirement. Perhaps, the 9.7.0 version did not support the MERGEFORMAT switch or supported it partially/incorrectly. To work around the issue, please edit the template or use the Aspose.Words API to alter it. Specifically, you should remove the MERGEFORMAT switch and set the desired formatting to field code runs. Sample code is as follows:

Document doc = new Document(MyDir + @"1.3_Abertura_Livro_Unidade_Producao (2).docx");
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldMergeField)
    {
        FieldMergeField mf = (FieldMergeField)field;
        mf.Format.GeneralFormats.Remove(GeneralFormat.MergeFormat);
    }
}
doc.MailMerge.Execute(new string[] { "OrgaoEndereco",
    "OrgaoMunicipio",
    "OrgaoUF",
    "OrgaoCep"},
new object[] { "GOVERNO DO ESTADO DO ESPÍRITO SANTO",
    "SECRETARIA DE ESTADO DA AGRICULTURA, ABASTECIMENTO, AQUICULTURA E PESCA",
    "INSTITUTO DE DEFESA AGROPECUÁRIA E FLORESTAL DO ESPÍRITO SANTO",
    "Seção de Defesa Sanitária Vegetal"});
doc.Save(MyDir + @"17.1.0.pdf");

Hope, this helps.

Best regards,