Different result. byte array vs HttpResponse

Dear Aspose team. thanks for all your support.

I created a Word document in .NET and added some paragraphs, page headers, tables and etc.
Everything works as expected when I’m storing the result as byte[] on the disk. But the output of the downloaded file is different in terms of page headers.

To elaborate on that the headers of event pages have been replaced by the primary headers. Having said that when I tried to keep only the even page headers and commented on all the primary headers sections the output result contains no header at all.

Here is how I pass it to HttpResponseMessage:

var reportFormat = "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,";
var str = System.Convert.ToBase64String(bytes, 0, bytes.Length);
str = reportFormat + str;
var content = new StringContent(str);

var response = new HttpResponseMessage();
response.Content = content;

Here is my configuration for headers:

builder.PageSetup.HeaderDistance = 20;

builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Header;

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
var lineOddPages = CreateLine(doc);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.Font.Bidi = true;
builder.PageSetup.Bidi = true;
builder.InsertField("PAGE", null);
builder.Write(oddPagesHeader);
builder.InsertNode(lineOddPages);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
var lineEvenPages = CreateLine(doc);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Font.Bidi = true;
builder.PageSetup.Bidi = true;
builder.Write(evenPagesHeader);
builder.InsertField("PAGE", null);
builder.InsertNode(lineEvenPages);

@mohammad19i Could you please attach the documents saved directly to file and sent to HttpResponse for our reference? There should not be any difference since in both case Aspose.Words writes the same document into the stream. Could you please try saving the document to file and then read the file and sent it to HttpResponse? Does the problem occur in such scenario?

Thanks for your reply. I attached the two mentioned files.

You can obviously see that page headers on even pages are different in those files. and SavedByHttpResponseMessage.docx show the same header for all pages.

Here is how I saved the byte directly to the disk:

...
MemoryStream ms = new MemoryStream();
doc.Save(ms, SaveFormat.Docx);
var bt = ms.ToArray();
File.WriteAllBytes("D:/DirectlySavedAsByteArray", bt);

Yesterday I add a table of contents and I noticed that there is a big difference in the result of the two files, that you can vividly see it.

And here is how I made the table of contents:

public void SetTableOfContents(Document doc, string title, int levels, bool hasLink)
{
    builder.RowFormat.AllowBreakAcrossPages = false;
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Title;
    builder.Font.Bold = true;
    builder.Write(title);

    builder.Write(ControlChar.LineBreak);

    doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.Bidi = true;
    doc.Styles[StyleIdentifier.Toc1].Font.Name = "B Nazanin";
    doc.Styles[StyleIdentifier.Toc1].Font.Bidi = true;
    doc.Styles[StyleIdentifier.Toc2].ParagraphFormat.Bidi = true;
    doc.Styles[StyleIdentifier.Toc2].Font.Name = "B Nazanin";
    doc.Styles[StyleIdentifier.Toc2].Font.Bidi = true;

    var link = hasLink ? "h" : "";

    builder.InsertTableOfContents($"\\o \"1-{levels}\" \\{link} \\z \\u");

    builder.Write(ControlChar.PageBreak);
}

DirectlySavedAsByteArray.docx (12.5 KB)
SavedByHttpResponseMessage.docx (12.7 KB)

@mohammad19i Unfortunately, I cannot reproduce the problem on my side. Could you please try saving the document to file and then read the file and sent it to HttpResponse? Are the resulting documents the same in this case?
Also, as I can see, you are using a very old 16.6 version of Aspose.Words. Please try using 23.6 version of Aspose.Words.

1 Like

I did your request and I got the same result. In this case, the file download using HttpResponseMessage was exactly the same as the file I saved directly on the disk and everything were OK.

Do you think the problem is related to the version of Aspose I’m using?

@mohammad19i Do you use the same code to save the document to byte array before sanding it to HttpResponse?

MemoryStream ms = new MemoryStream();
doc.Save(ms, SaveFormat.Docx);
var bt = ms.ToArray();

I have inspected the documents you have attached and I see that structure of the files are different. This cannot be caused by simple saving to the different target. It looks like the document was pre-processed before sending it to HttpResponse.

No, I do not think the problem is related to the version of Aspose.Words. I think, the problem is somewhere in the code before sanding the document to HttpResponse.

Actually, I mentioned it in the first message. You are right there are changes before sending to HttpResponseMessage and here is all of it:

 var reportFormat = "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,";
 var str = System.Convert.ToBase64String(bytes, 0, bytes.Length);
 str = reportFormat + str;
 var content = new StringContent(str);

And finally, I pass the content to the content property of HttpResponse. Because in this way the end-user can download the file.

@mohammad19i The code does not change the byte array, it simply converts it to base64 string. So nothing should be changed in the output document. If possible, could you please create a simple application that will allow us to reproduce the problem? We will check it on our side and provide you more information.

1 Like

I exactly found out what is wrong with my code. But I couldn’t solve the problem. The problem is related to the merging process of several documents. For example, I created a document using Aspose Words and attach another document to it that has been built using MS Word. And here is the code that I used for this issue:

public static byte[] MergeWord(List<byte[]> words)
{
    var finalDoc = new Aspose.Words.Document();
    finalDoc.RemoveAllChildren();

    foreach (var word in words)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            ms.Write(word, 0, word.Length);
            Aspose.Words.Document doc = new Aspose.Words.Document(ms);
            finalDoc.AppendDocument(doc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);
        }
    }

    MemoryStream outStream = new MemoryStream();
    finalDoc.Save(outStream, Aspose.Words.SaveFormat.Docx);
    byte[] docBytes = outStream.ToArray();
    return docBytes;
}

I also tried merging using DocumentFormat.OpenXml and completely removed my headers.

What’s wrong with my merging code?

@mohammad19i The problem might be caused by the different compatibility options used in your source and target documents. Please try modifying your code like this:

public static byte[] MergeWord(List<byte[]> words)
{
    Aspose.Words.Document finalDoc = null;

    foreach (var word in words)
    {
        using (MemoryStream ms = new MemoryStream(word))
        {
            Aspose.Words.Document doc = new Aspose.Words.Document(ms);
            if (finalDoc == null)
                finalDoc = doc;
            else
                finalDoc.AppendDocument(doc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);
        }
    }

    MemoryStream outStream = new MemoryStream();
    finalDoc.Save(outStream, Aspose.Words.SaveFormat.Docx);
    byte[] docBytes = outStream.ToArray();
    return docBytes;
}

In such case the final document is not created from scratch and uses compatibility options specified in the first document in the list.

1 Like