Appending multiple documents!

Hi.
Im getting a bit of headache while trying to append several documents from a byte[][] to one big nice document and then back to a single byte[].

Code:

private byte[] mergeDocs(byte[][] tempNotification)
{
    Document DoucumentOut = new Document();
    for (int i = 1; i < tempNotification.Length; i++)
    {
        if (tempNotification[i] != null)
        {
            System.IO.MemoryStream msIn = new System.IO.MemoryStream(tempNotification[i]);
            Document document = new Document(msIn);

            DoucumentOut.Sections.Add(document.Sections[0].Clone());
        }
    }

    System.IO.MemoryStream msOut = new System.IO.MemoryStream();
    DoucumentOut.Save(msOut, SaveFormat.Doc);

    return msOut.GetBuffer();
}

This renders a Exception that i can’t solve at this line:
“DoucumentOut.Sections.Add(document.Sections[0]);”

The exeption:
“The newChild was created from a different document than the one that created this node.”

I have searched the forum and thought that i found a solution a couple of times but they all render this Exception.

Can someone please explain in detail what this exception message means?
And please help me get past this problem.

Regards
/Joel

Hi
Thanks for your request. You should use ImportNode method. Please try using the following code:

private byte[] mergeDocs(byte[][] tempNotification)
{
    Document DoucumentOut = new Document();
    for (int i = 1; i < tempNotification.Length; i++)
    {
        if (tempNotification[i] != null)
        {
            System.IO.MemoryStream msIn = new System.IO.MemoryStream(tempNotification[i]);
            Document document = new Document(msIn);
            foreach (Section srcSection in document.Sections)
            {
                Section dstSection = (Section)DoucumentOut.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
                DoucumentOut.Sections.Add(dstSection);
            }
        }
    }
    System.IO.MemoryStream msOut = new System.IO.MemoryStream();
    DoucumentOut.Save(msOut, SaveFormat.Doc);
    return msOut.GetBuffer();
}

I hope this could help you.
Best regards.

That worked perfect.

Thank you for very a quick response.

Hi Alexey,

your solution has a single flaw. If you has a lot of documents, i.e. > 1000 pieces, or few larger ones, the document consumes more and more memory. If you have max 128MB memory, it is hardly possible to append more than 20 documents with 4 pages each to one single document.

Is there any solution to append two documents without loading them completely into the memory for Java?

Thanks a lot! Kind regards,

Jens Böckel
---------------------------------
ABACUS Research AG
B2B Software
Switzerland

Hi
Thanks for your inquiry. No there is no way to load document into memory partially. The document needs to be held wholly in memory. Therefore, memory and CPU usage are dependent on document size and document complexity.
Anyway, it is better to use few small documents instead one huge document.
Best regards.

Hi Alexey,

thanks for your support. This is a must to have one single document at the end of the process.

Is there any chance that the memory usage with docx format is smaller?

Jens Boeckel
-------------------------
ABACUS Research AG
Switzerland

Hi
Thanks for your inquiry. No, there is no difference what format you will use. Memory usage will be the same. But, you are free to check this on your side.
Best regards.