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

Free Support Forum - aspose.com

Issues with new aspose.words version (18.2)

Hi,
we have rolled new version of aspose words and we have noticed 2 of our unit tests started to fails:

  1. public void Should_MergeDateWithFormatSetInMergeField_When_InputDateIsProvidedWithoutSlashesOrBreaks()
    {
    var primaryRecordSet = new PrimaryRecordSet(“Main”, new Dictionary<string, RecordSetElement>() { { “Date”, new RecordSetElement(“20160113”, false, false) } });
    var secondaryRecordSets = new List();
    var mergeData = new MergeData(primaryRecordSet, secondaryRecordSets);
    A.CallTo( () => _mergeDataBuilder.BuildMergeData( _xmlDataFile ) ).Returns( mergeData );
    var document = new DocBuilder( _fieldParser.Parse( “{MERGEFIELD Date \@“dd MMMM yyyy”}” ) ).Build();
    _objectUnderTest.MailMerge( document, _xmlDataFile, false );
    Assert.AreEqual( “13 January 2016”, document.Range.Text.TrimAsposeCrap() );
    }

It is now failing with "Expected string length 15 but was 10. Strings differ at index 0.
Expected: “13 January 2016”
But was: “2016/01/13"”

  1. public void Should_ThrowFileCorrupted_When_IncorrectFormatOfInputFile()
    {
    _requestCallerFacade.SelfServiceTemplateFilePath = “InputFiles/Broken.dotx”;
    Assert.Throws( () => _requestCallerFacade.CallRenderer( _objectUnderTest ) );
    }

this one changed the type of exception being thrown on line
var doc = new Document(stream);
attached is our testing template.

It is now throwing UnsupportedFileFormatException

Could you please advice?

Thanks
Aga

Broken.zip (6.1 KB)

Sorry i just realized one more test is failing (and used not to):

public void Should_InsertBookmarkStart_When_PassedAsArgument()
{
var b = new DocBuilder( new Paragraph( new BookmarkStart( “EXT_Test” ), new Run( “text inside bookmark” ) ) );
var doc = b.Build();

        var para = doc.FirstSection.Body.ChildNodes.IgnoreEmptyNodes().Single() as Aspose.Words.Paragraph;
        Assert.IsNotNull( doc.Range.Bookmarks[ "EXT_Test" ] );
        Assert.AreEqual( Aspose.Words.NodeType.BookmarkStart, para.ChildNodes[ 0 ].NodeType );
    }

@acturisaspose,

Thanks for your inquiry. The Broken.dotx does not open in MS Word as well. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application (source code without compilation errors) that helps us to 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 and upload them.

AsposeWordsNewVersionIssues.zip (7.0 KB)

Hi,

I have attached a simple code to show what our issues are.

  1. If you take a look at TEST1_IncorrectDateFormat()
    what we are doing here is to add incorrectly formatted date (ie. without slashes in this case) and the using FieldMergingCallback functionality to correct it (add slashes) so it could then be resolved correctly during mailMerge step.
    Before we rolled 18.2 version of Aspose Words TEST1_IncorrectDateFormat() code used to produce document with the correct format date [ie. 13 January 2016 because of the docBuilder.InsertField(“MERGEFIELD Date \@“dd MMMM yyyy””); code ]
    After the version update the field shows up as 2016/01/13 (so the formatting from the mergefield isn’t applied) however if we don’t use FieldMergingCallback and instead provide a merge data with correct date format (2016/01/13) - then mergefield formatting would be applied correctly.

  2. Regarding the Broken.dotx issue - yes we are aware that this file is corrupted and it can’t be opened in Word. The issue here is that the type of error thrown by Aspose on opening this file has changed for this case from ‘FileCorruptedException’ to ‘UnsupportedFileFormatException’ and we can’t see why (and would prefer if it didn’t as this file does not have unsopported file format extention - it is corrupted.

  3. If you take a look at TEST2_BookmarkAddedEvenIfNoBookmarkEnd()
    What we are checking here is if we add to the document a bookmarkStart field and some text, but don’t add bookmark end - will we still have bookmark added to Document.Range.Bookmarks.
    In the previous version of Aspose Words this was the case (so adding only bookmark start with some text has resulted in inserting bookmark node into the document object) but after rolling 18.2 version of Aspose Words if we don’t close the bookmark with BookmarkEnd field the bookmark won’t get added to the document.
    Can you please confirm why this was changed and if this was intended?

Please let me know if you have any queries

Thanks
Aga

@acturisaspose,

Thanks for sharing the detail.

We have logged this problem in our issue tracking system as WORDSNET-16557. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

We have tested the scenario using latest version of Aspose.Words for .NET 18.3 and have not found the shared issue. Please use Aspose.Words for .NET 18.3.

You have implemented IFieldMergingCallback interface. We suggest you please format the date according to your requirement in IFieldMergingCallback.FieldMerging method.

Following code example generates the correct output.

var document = new Document();
var docBuilder = new DocumentBuilder(document);
docBuilder.InsertField("MERGEFIELD Date \\@\"dd MMMM yyyy\"");
document.MailMerge.Execute(new string[] { "Date" }, new object[] { DateTime.Now });

document.Save(MyDir + "output.docx");

You have implemented IFieldMergingCallback interface. We suggest you please format the date according to your requirement in IFieldMergingCallback.FieldMerging method.

We can’t really implement date formatting in the callback method as you have suggested because we don’t know what it will be. Attached code was just an example, but in fact we will be using .dot/.dotx template input with fields and separate mergeData files so at the time we are doing callback we don’t really know what time/date format the user have set up in their template.

additionally please note that it used to work correctly and it only changed after rolling 18.2 version of code.

Thanks
aga

@acturisaspose,

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

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the output Word file that shows the desired behavior.
  • Please attach the Aspose.Words version that works fine for your scenario.
  • Please create a standalone console application (source code without compilation errors) that helps us to 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 and upload them.

Hi,

I have already provided you with an example code and explained the issue: we need to be able to use Callback option to correct incorrectly formatted date from merge data file (ie. if we got 20160114). This was possible in older version of Aspose Words code but has stopped working when we have rolled 18.2.
Your advice was to use correctly formatted date in merge data which isn’t exactly helpful (as i explained we can’t do it)
I can’t see why provided materials are not enough but please let me know if something from my example is unclear and i will try to explain.

Thanks
Aga

@acturisaspose,

Thanks for your inquiry. Please note that when you implement IFieldMergingCallback interface you need to write the code in IFieldMergingCallback.FieldMerging to format the date value according to your requirement. Following code example shows how to format the date during mail merge process.

private class FormatDateField : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldName == "Date")
        {
            DateTime date = Convert.ToDateTime(e.FieldValue.ToString());
                    
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.DocumentFieldName);
            builder.Write(date.ToString("dd MMMM yyyy"));
            e.Text = "";
        }
    }
 
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    { 
    }
}

var document = new Document();
var docBuilder = new DocumentBuilder(document);
docBuilder.InsertField("MERGEFIELD Date ");
document.MailMerge.FieldMergingCallback = new FormatDateField();
document.MailMerge.Execute(new string[] { "Date" }, new object[] { DateTime.Now });

document.Save(MyDir + "output.docx");

Following class shared in AsposeWordsNewVersionIssues.zip. This code does not work according to your shared requirement. You need create a Date object and format it as shown in above code example.

class ActurisDateFormatMergeCallback : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        args.Text = args.FieldValue.ToString().Insert(6, "/").Insert(4, "/");
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        throw new System.NotImplementedException();
    }
}

If you still face problem, please share the code example, documents and old Aspose.Words’ version that works fine at your end. We will then investigate the issue and log it in our issue tracking system.

AsposeDateFormattingSmaller.zip (11.6 KB)
Hi,

I have prepared console app (attached).

In the InputFiles there is example xml - this is the format we usually get mergeData in - we have no control over the way data in this file is produced/formatted etc - we get it and need to work with that :slight_smile:

the second input we usually get is word file in XXX.dot (or .dotx) format with some text and Fields inside.
In the console App i am creating a simple example of such file (with one mergefield only where date formatting was applied - “dd MMMM yyyy”). Please note that normally we would get this template from the user and we have no control over the template content.

our old library is on version 17.3 and new one is version 18.2. I have tried to include it in the zip file but they are to big and won’t fit in the attachement - please let me know if there is any other way i can send them to you if you require.

if you reference old library and run the app - you should see that the date output is formatted as requested by user in the Mergefield however if you reference to the new library - the output will be formatted as yyyy/MM/dd

Could you please advice?

Thanks
Aga

@acturisaspose,

Thanks for sharing the detail. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16579. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

Please use the following code example to get the desired output. Hope this helps you.

public void FieldMerging(FieldMergingArgs args)
{
    //args.Text = args.FieldValue.ToString().Insert(6, "/").Insert(4, "/");
    String strDate = args.FieldValue.ToString().Insert(6, "/").Insert(4, "/"); 
    DateTime date = Convert.ToDateTime(strDate);

    DocumentBuilder builder = new DocumentBuilder(args.Document);
    builder.MoveToMergeField(args.DocumentFieldName);
    builder.Write(date.ToString("dd MMMM yyyy"));
    args.Text = "";
}

@acturisaspose,

Thanks for your patience. It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-16579) as ‘Not a Bug’.

Please note that FieldMergingArgs.Text property gets or sets the text that will be inserted into the document for the current merge field. If you set Text to any string (including empty), the string will be inserted into the document in place of the merge field.

This was a bug in older versions of Aspose.Words and fixed in Aspose.Words 17.7. Please use the code example shared in previous post to get the desired output.

The issues you have found earlier (filed as WORDSNET-16557) have been fixed in this Aspose.Words for .NET 18.4 update and this Aspose.Words for Java 18.4 update.

Hi,
I can confirm that the incorrect exception type issue is now fixed, however i have tested issue we had with bookmark not being added when only bookmarkStart was inserted (see bellow) and it is still not working.

@acturisaspose,

Thanks for your inquiry. You are facing the expected behavior of Aspose.Words. If you do not add BookmarkStart and BookmarkEnd nodes for a bookmark into document, you will not get the bookmark. If you are getting bookmark by inserting only BookmarkStart node using old version of Aspose.Words, it is a bug. The latest version of Aspose.Words for .NET 18.4 shows expected behavior.

hi,

sorry we just run into another issue with this solution - what with the cases where there is a formatting switch on the field? eg.:

{ MERGEFIELD Date @ “d” * Ordinal}

thanks
Aga

@acturisaspose,

Thanks for your inquiry. Aspose.Words does support the ordinal date formats e.g. { DATE @ “d” * Ordinal }.

Unfortunately, your question is not clear enough therefore we request you to please elaborate your inquiry further. Also, please supply input/output documents and your current code for testing. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

oh, i am really sorry, i thought i have included a reference to the above note.
The issue i have is with the above code example you have suggested we should use for our FieldMerging callback method. it works ok with simple cases, but when the Field we are merging contains formatting switch, they are not being taken into account.
I can (and did) add DateTimeFormat , but not sure how to handle GeneralFormat (eg. Ordinal), or NumberFormat (and i am refering to properties names eg. Field.Format.DateTimeFormat)

Could you please advise?
Thanks
Aga

@acturisaspose,

Thanks for your inquiry. The issue WORDSNET-16579 was resolved in Aspose.Words 18.4. You do not need to use shared workaround i.e. format the date using C# code.

Please make sure that you are using the format switches in the field correctly and field’s value is also correct. If you still face problem, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

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

PS: To attach these resources, please zip and upload them.

Sorry, i am confused, in the following note (quote bellow) you have stated that this was not a bug and won’t be fixed?.. could you please confirm?