KeepSourceFormatting mode works incorrectly

Hi,

There is a bug in Aspose.Words if you run the code below:

	var documentWithStyleRefDate = new Document("Document with StyleRef Date.docx");
	var emptyDocumentWithDateStyle = new Document("Empty document with Date style.docx");
	emptyDocumentWithDateStyle.AppendDocument(documentWithStyleRefDate, ImportFormatMode.KeepSourceFormatting);
	emptyDocumentWithDateStyle.Range.Fields[0].Update();
	emptyDocumentWithDateStyle.Save("Resulting document.docx");

In the resulting document, I expect to see:

{STYLEREF Date_0 * MERGEFORMAT}

But see:

{STYLEREF Date * MERGEFORMAT}

Thus, the field can not be updated correctly (see Resulting document.docx in attached archive (127.7 KB)

I use Aspose.Words 17.9.0.0

Many thanks,
Alex Shloma

@licenses,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you append the content of one document into another using MS Word, you will get the same output.

We suggest you please use ImportFormatMode.UseDestinationStyles in Document.AppendDocument method.

emptyDocumentWithDateStyle.AppendDocument(documentWithStyleRefDate, ImportFormatMode.UseDestinationStyles);

We suggest you please read the following article.
Differences between ImportFormat Modes

I know how import format modes are different when adding the content of one document into another.
I need to use ImportFormatMode.KeepSourceFormatting mode because after the adding I want to get 2 different styles: Date and Date_0 in document.

The problem lies elsewhere. Please, see my first post.

@licenses,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you merge the content of documents using MS Word, you will get the same output.

In your case, you need to update the style name of field as shown below.

var documentWithStyleRefDate = new Document(MyDir + "Document with StyleRef Date.docx");
var emptyDocumentWithDateStyle = new Document(MyDir + "Empty document with Date style.docx");
emptyDocumentWithDateStyle.AppendDocument(documentWithStyleRefDate, ImportFormatMode.KeepSourceFormatting);

FieldStyleRef field = (FieldStyleRef)emptyDocumentWithDateStyle.Range.Fields[0];
field.StyleName = "Date_0";
field.Update();

foreach (Style style in emptyDocumentWithDateStyle.Styles)
{
    if (style.Name.StartsWith("Date"))
        Console.WriteLine(style.Name);
}
emptyDocumentWithDateStyle.Save(MyDir + "output.docx");

Hi Tahir,

Thanks for your response.

Best regards,
Alex Shloma