Replacing text by a document (Aspose.Words JAVA) (2)

Seems like I messed up a bit since this post was supposed to be an answer to this topic.

Replacing text by a document (Aspose.Words JAVA)

Hi,

Thank you for your answer. We are now to do the replacement. The problem we have is that we have a continuous section break that is actually like a next page section break and we really don’t want that. I attached a generated document for you to see. The text we replaced is the one that is between two section breaks.

And here is the code we use. It’s almost the one you gave us:

private void insertClause(Node node, Document doc)
{
    Document dstDoc = node.getDocument();
    int index;
    Node cell = node.getAncestor(Cell.class);
    Node shape = node.getAncestor(Shape.class);
    if (cell != null || shape != null) {
    // Insertion point is tracked to Cell or Shape level:
    // insert appended document on node by node basis.
        index = node.getParentNode().getChildNodes().indexOf(node);
        Sections sections = doc.getSections();
        for (int s=0; s<sections.getCount(); s++) {
            Section section = (Section)sections.get(s);
            Section insertedSection = null;
            try {
                insertedSection = (Section) dstDoc.importNode(section, true, ImportFormatMode.KEEP\_SOURCE\_FORMATTING);
                for (int n=0; n<insertedSection.getBody().getChildNodes().getCount(); n++) {
                    Node npt = insertedSection.getBody().getChildNodes().get(n);
                    // Only Paragraph or Table nodes can be inserted into Cell or Shape
                    if (node instanceof Paragraph ||
                            node instanceof Table) {
                        node.getParentNode().getChildNodes().insert(index++, npt);
                    }
                }
            } catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else
    {
        // Insertion point is tracked to Section.Body level:
        // - insert appended document on section by section basis.
        Section dstSection = null;
        Body body = (Body)node.getAncestor(Body.class);
        if (body.getLastChild() != node)
        {
            try
            {
                DocumentBuilder builder = new DocumentBuilder(dstDoc);
                builder.moveTo(node);
                builder.insertBreak(BreakType.SECTION\_BREAK\_CONTINUOUS);
                dstSection = builder.getCurrentParagraph().getParentSection();
                index = dstDoc.getSections().indexOf(dstSection);
                int index0 = index;
                Sections sections = doc.getSections();
                for (int s = 0; s < sections.getCount(); s++)
                {
                    Section section = (Section)sections.get(s);
                    Section insertedSection = null;
                    insertedSection = (Section)dstDoc.importNode(section, true, ImportFormatMode.KEEP\_SOURCE\_FORMATTING);
                    dstDoc.getSections().insert(index++, insertedSection);
                }
                dstDoc.getSections().get(index0).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else
        {
            try
            {
                dstSection = (Section)node.getAncestor(Section.class);
                index = dstDoc.getSections().indexOf(dstSection);
                int index0 = index;
                Sections sections = doc.getSections();
                for (int s = 0; s < sections.getCount(); s++)
                {
                    Section section = (Section)sections.get(s);
                    Section insertedSection = null;
                    insertedSection = (Section)dstDoc.importNode(section, true, ImportFormatMode.KEEP\_SOURCE\_FORMATTING);
                    dstDoc.getSections().insert(index++, insertedSection);
                }
                dstDoc.getSections().get(index0).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
            } catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

Thank you for your help.

Continuous section break behaves as a next page section break in MS Word due to differing PageSetup properties of the inserted section. You need either set similar PageSetup options for template documents using MS Word or set PageSetup properties for inserted section the same as in the destination document one by one.

Hi,

thank you very much. We actually had the 2 problems, the one with the header/footer and the one with the page setup.

Now we have another problem: when we use the mail merge fonction, we noticed that the merged text keeps the text formatting of the merge fiels expect for the font size.

generated document and source document with its data attached.

You see, all fields in word documents actually consist of two parts: field code and field value. In MS Word you can switch between code and value view by pressing ALT-F9. Field codes look like { MERGEFIELD VAR1 } and field values look like «VAR1». When Aspose.Words inserts data into merge field it takes formatting from field codes, not from field values.

So to set correct the desired formatting for inserted data you need to switch to code view in MS Word by pressing ALT-F9 and apply formatting to the field code.

Best regards,

Hi,

Thank you for your answer. We now face another problem since the merged pages with Aspose.Words don’t always look the same as the ones merged with MS Word. I attached the 2 documents for you to see and highlighted the paragraphs that look different. It seems that the problem comes from Aspose not removing the IF fields and the nested IF fields in particular in the generated document. That’s why I think we need to find a way to do that but didn’t find a way to do so.

Yes, Aspose.Words concatenates documents as is, retaining all fields and formatting from the original documents. And we don’t support evaluation, manipulation or removal of IF fields yet. So, it seems there is no solution for the issue at the moment.

Best regards,