Contents in table's cell are not rendered correctly after re-saving DOCX using C#

I am trying to use aspose to merge multiple word(docx) files into one. These
have been loaded into memorystreams. When I create a document using these memory streams and save them the format is stuffed. So obviously the merged document is stuffed.
below is the test code I have written now when I have written the memorystream to disk the file looks fine but if used to create an aspose document and saved th formating is stuffed.
Any ideas /help on what I am doing wrong…
All help appreciated thanks Warren
I have attached 2 files one written directly from memory stream and the other saved using aspose…

public int Merge_test(report_info.ReportBatchInformation ReportBatch, int CurrentAccount)
{
    int loop_count = 0;
    string outputfilename = "";
    Aspose.Words.Document merged_doc = new Aspose.Words.Document();

    foreach (report_info.ReportRunInformation* r in ReportBatch.IndividualReports)
    {
        string Output_filename2 = "TaxSummary_A*" + CurrentAccount.ToString() + "_" + _r.Sequence.ToString() + ".Docx";
        FileStream fsb = File.OpenWrite(Output_filename2);
        fsb.Write(_r.report_output.ToArray(), 0, (int)_r.report_output.Length);
        fsb.Close();

        Aspose.Words.Document doc3 = new Aspose.Words.Document(_r.report_output);
        merged_doc.AppendDocument(doc3, ImportFormatMode.KeepSourceFormatting);
        Aspose.Words.Document doc2 = new Aspose.Words.Document(_r.report_output);
        outputfilename = "Test_docfile" + loop_count.ToString() + ".docx";
        doc2.Save(outputfilename, Aspose.Words.SaveFormat.Docx);
        loop_count = loop_count + 1;
    }
    outputfilename = "Mergetest.docx";
    merged_doc.Save(outputfilename, Aspose.Words.SaveFormat.Docx);
    return 0;
}

Hi Warren,

Thanks for your query. Please use the following code snippet for your requirement.

Document doc = new Document();
doc.RemoveAllChildren();
//e.g there are three documet
String[] Files = new String[] { "Doc1.docx", "Doc2.docx", "Doc3.docx" };
foreach (String file in Files)
{
    Document docAppend = new Document(MyDir + file);
    doc.AppendDocument(docAppend, ImportFormatMode.KeepSourceFormatting);
}
doc.Save(MyDir + "AsposeOut.docx");

Please check the code at following forum thread for your kind reference.

https://forum.aspose.com/t/53479

The documentation link for your kind reference.
https://docs.aspose.com/words/net/insert-and-append-documents/

Hope this helps you. Let us know, If you have any more queries.

Sorry but what is the difference between that and the example code I attached?
I have modified the code to use the removeallchildren
But if I am loading the memorystreams in to the document and then saving the format is still different.
So if you use the example I sent … ie load the ‘taxsummary_A…’ file into an aspose.Words.document as a memory stream then save the document.
Each time I do it the format changes irregardless of merging documents or not. This is the main issue the fact that the document looses formatting information .
Regards
Warren

Perhaps I need to explain a bit better.
If I load doc files all works nicely. I have the merge working fine also.
I am generating docx files in an array of memorystreams that I wish to merge. I can get all to merge but the format is wrong ( ie not matching the original)
So I ttested loading the memory streams in to documents seperately and saving seperately as it is trying to merge them. from the attached file test_filedoc0.docx the format does not match the format of taxsummary_a_125567_1.docx .
From the code snippet you can see that the taxsummary file is a direct save of the memory stream whilst the test file is the document saved via aspose. And they are not the same.

Hi Warren,

Thanks for sharing the further information. Please explain the following in more detail.
I can get all to merge but the format is wrong ( ie not matching the original)
If you are facing text formatting issue with your documents, please use ImportFormatMode.UseDestinationStyles in your code and share your findings.

We are really keen to help you but need some more details from your side.

I will try to re-explain the issue I am having .
We generate docx files into memory streams (not to disk)
We want to use aspose to handle these files but during testing I am not getting the resultss I would expect.
I have attached a snippet of code that I have used for testing.
in the routine aspose_test I have used the memory streams in multiple different ways to test the results.

1/ I write the memorystream to disk as file TaxSummary_from_memstream.docx when we open this file in word the result is exactly as we would expect the file to look.

2/ I open the memory stream in an aspose document and save as aspose_memfile.docx. if this is opened in word it does not look like TaxSummary_from_memstream.docx . I don’t understand why this would be ?

3/I read TaxSummary_from_memstream.docx into a memory stream and load that into an aspose document then save it. This result does not look like TaxSummary_from_memstream.docx… why?
4/ I open TaxSummary_from_memstream.docx directly into an aspose document and then save it… again the result does not loook like TaxSummary_from_memstream.docx why?

code

public int Aspose_test(report_info.ReportBatchInformation ReportBatch, int CurrentAccount)
{
    string outputfilename = "";
    foreach (report_info.ReportRunInformation _r in ReportBatch.IndividualReports)
    {
        string Output_filename2 = "TaxSummary_from_memstream.docx";
        FileStream fsb = File.OpenWrite(Output_filename2);
        fsb.Write(_r.report_output.ToArray(), 0, (int)_r.report_output.Length);
        fsb.Close();

        Aspose.Words.Document doc2 = new Aspose.Words.Document(_r.report_output);
        outputfilename = "aspose_memfile.docx";
        doc2.Save(outputfilename);

        FileStream fsb2 = File.OpenRead(Output_filename2);
        Byte[] file_contents = new byte[fsb2.Length + 1];
        fsb2.Read(file_contents, 0, (int)fsb2.Length);
        var file_in_memory = new MemoryStream(file_contents);

        Aspose.Words.Document doc3 = new Aspose.Words.Document(file_in_memory);
        outputfilename = "Test_discfile.docx";
        doc3.Save(outputfilename);

        Aspose.Words.Document doc4 = new Aspose.Words.Document(Output_filename2);
        outputfilename = "Test_discfile2.docx";
        doc4.Save(outputfilename);
    }
    return 0;
}

Hi Warren,

Please accept my apologies for late response.

Thanks for sharing the more information. I have managed to reproduce the same problem at my end. I have logged this issue in our issue tracking system and you will be notified via this forum thread once this issue is resolved.

We really appreciate your patience and apologies for your inconvenience.

Sorry to be a pain … but has there been any further information on this?
I am under a bit of time pressure to have a solution.
regards
Warren

Hi Warren,

I have verified the status of this issue from our issue tracking system and like to share with you that this issue is pending for analysis. You will be updated via this forum thread once this issue is resolved.

We appreciate your patience.

Is this any closer to a resolution? We are hoping to be able to use aspose. As our release of word format reports is due in 4 weeks , if this does not look to be close to a resolution I need to know so I can look for an alternative.
regards
Warren

Hi Warren,

I regret to share with you that this issue is still pending for analysis. However, I have asked for the ETA of this issue from our development team. As soon as any information is shared by them, I will be more than happy to share that with you.

We really appreciate your patience and apologies for your inconvenience.

whilst you are analysing the issue so are Ecrion. They have sent me a couple of files to try , but as my temporary license has expired I can not try these.
Is it possible to get an extension to the temporary license?

Hi Warren,

You can request a free 30-day trial license again from following link. Please let us know if you have any more queries.

https://purchase.aspose.com/temporary-license

Hello Warren,
Thanks for your interest to Aspose.Words. I’m working on the issue you have reported earlier. As I can see the document “TaxSummary_A_125576_1.docx” was created using a third party tool. If you try for example open/save this document in MS Word and then process using Aspose.Words the output document will look much better.
Then if you try comparing (I mean unzip Docx and compare document.xml) “TaxSummary_A_125576_1.docx” before and after open/save with MS Word, you will see your document is really “corrupted” MS Word somehow fixes this, but Aspose.Words does not. It may take some time to fix this problem.
Do you have an ability to open/save this document before process with Aspose.Words, or maybe you can fix the mechanism which you are using to generate this document? Or you can try using Aspose.Words to generate such document from scratch.
Best regards,

Thankyou for that. Unfortunately the third party application is a report generator that produces the file.
Are you able to tell me what is corrupt about the file ? I looked at the file and it lookslike valid XML to me. I will then try to resolve this either with the other party or via a change myself.
regards
Warren

Hello Warren,
Thank you for additional information. XML inside your document is valid but if you try to compare this XML before and after open/save with MS Word you will see the difference. Your document has 4 tables each of them has “tableGrid” element with “gridCol” element. “gridCol” element should have “w:w” attribute with width specified. Please see the attached screenshot. If you try replace all “tableGrid” elements inside your document with correct and then process the document with Aspose.Words the output document will look correct.
Best regards,

Hello Warren,
Have you managed to resolve your problem as it was suggested in my previous reply?
Please let me know in case of any other issues. I will be glad to help you.
Best regards,

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


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