Document Merging - Preserving Source Default TabStop Setting

I am creating a ‘master’ document from several ‘source’ documents. Each of these source documents may have a different default tabstop setting. How do I honor the setting for each ‘source’ document as I build the ‘master’?

Thanks.

Steve Waga

Hi
Thanks for your inquiry. Could you please attach your documents for testing?
Best regards.

Here are two sample source documents.

Hi
Thank you for additional information. I used the following code for merging documents and it seems all works fine.

Document docA = new Document("SourceFormA.doc");
Document docB = new Document("SourceFormB.doc");
AppendDoc(docA, docB);
docA.Save("out.doc");

See the following link to learn how to merge documents.
https://docs.aspose.com/words/net/insert-and-append-documents/
Best regards.

Hi Alexey,

When you say it works fine, are you referring to the successful production of the merged document itself or that plus its formatting?

When I do the merge, the document is produced successfully but the formatting for document ‘SourceFormA.doc’ is not correct. This is due to incorrect tab stop settings in the ‘master’ document. The tab stop settings of SourceFormA.doc is .25". The settings in the ‘master.doc’ is the default .50".

In short, the tab stops of the source documents do not seem to be carried over to the ‘master’ document.

Steve

Hi
Thanks for your inquiry. The output document is formatted correctly. Could you please attach your output document? Which version of Aspose.Words are you using?
Best regards.

Attached is my output document.
We are using Aspose.Words for Java.

Hi
Thank you for additional information. I tried to merge your document using Aspose.Words for Java and output document looks fine. Formatting is the same as in the source documents. Which version of Aspose.Words for java are you using? I use the latest (2.5.0) version for testing.
https://releases.aspose.com/words/net
Please try using this version and let me know if this helps.
Best regards.

I too used the 2.5.0 version and still have the formatting issue. Here’s my code to merge the two documents.

private void insertDocument(Document masterDocument, Document srcDoc) throws Exception {
    for (int i = 0; i < srcDoc.getSections().getCount(); i++)
    {
        Section srcSection = srcDoc.getSections().get(i);
        Node dstSection = masterDocument.importNode(srcSection, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
        masterDocument.appendChild(dstSection);
    }
}

Hi
Thank you for additional information. I used the same code:

public static void main(String [ ] args) throws Exception {
    Document dst = new Document("C:\\Temp\\SourceFormA.doc");
    Document src = new Document("C:\\Temp\\SourceFormB.doc");
    AppendDoc(dst, src);
    dst.save("C:\\Temp\\out.doc");
}
///
/// A useful function that you can use to easily append one document to another.
///
/// The destination document where to append to.
/// The source document.
private static void AppendDoc(Document dstDoc, Document srcDoc) throws Exception
{
    // Loop through all sections in the source document.
    // Section nodes are immediate children of the Document node so we can just enumerate the Document.
    for (Section srcSection : srcDoc.getSections())
    {
        // Because we are copying a section from one document to another,
        // it is required to import the Section node into the destination document.
        // This adjusts any document-specific references to styles, lists, etc.
        //
        // Importing a node creates a copy of the original node, but the copy
        // is ready to be inserted into the destination document.
        Node dstSection = dstDoc.importNode(srcSection, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
        // Now the new section node can be appended to the destination document.
        dstDoc.appendChild(dstSection);
    }
}

Best regards.

Thanks. I’ll keep debugging …

Alexey, I see why yours works, try something like:

public static void main(String [ ] args) throws Exception {
    Document master = new Document();
    Document srcA= new Document("C:\\Temp\\SourceFormA.doc");
    AppendDoc(master, srcA);

    Document srcB = new Document("C:\\Temp\\SourceFormB.doc");
    AppendDoc(master, srcB);

    master.save("C:\\Temp\\out.doc");
}

This is more of what I am trying to do.

Steve

Steve, thank you for additional information. You can try using the following code to resolve the problem.

Document master = new Document();
master.getFirstSection().remove();
Document docA = new Document("C:\\Temp\\SourceFormA.doc");
master.setDefaultTabStop(docA.getDefaultTabStop());
AppendDoc(master, docA);
Document docB = new Document("C:\\Temp\\SourceFormB.doc");
AppendDoc(master, docB);
master.save("C:\\Temp\\out.doc");

Best regards.

Also, depending on what document you use as the dst, you will get different results. For example, try your original code but with the source files reversed.

public static void main(String [ ] args) throws Exception {
    Document src= new Document("C:\\Temp\\SourceFormA.doc");
    Document dst= new Document("C:\\Temp\\SourceFormB.doc");
    AppendDoc(dst, src);

    dst.save("C:\\Temp\\out.doc");
}

Steve

Thanks for the reply Alexey, but I do not know the document contents at the time of the merge. I could be merging many documents, any of which may have a unique default tab stop setting.

*Steve, thank you for additional information. You can try using the following code to resolve the problem.

Document master = new Document();
master.getFirstSection().remove();
Document docA = new Document(“C:\Temp\SourceFormA.doc”);
master.setDefaultTabStop(docA.getDefaultTabStop());
AppendDoc(master, docA);
Document docB = new Document(“C:\Temp\SourceFormB.doc”);
AppendDoc(master, docB);
master.save(“C:\Temp\out.doc”);*

Steve, thanks for your request. But there is no way to set default tab stops for each section.
Best regards,

Ok, thanks for postings Alexey.