We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replacing text by a document (Aspose.Words JAVA)

Hi,

I would like to know if it’s possible to replace a text by a document like when using the Word API?

Thx.

Dav

Yes, it is possible with the current API, although it requires some non-trivial coding.

I have made an example for you on how to evaluate some home made document inclusion statements, having the following format: [Include Document: filename ]. In this example all occurences of the text having the above format are substituted with the content of the document specified in filename string.

Here is a complete code:

///
/// Executes all "[Include Document: filename ]" statements in the document.
///
private void IncludeDocumentExecute(Document doc)
{
    string patternIncludeDocument = @"\[Include Document:\s\*(?.\*?)\s\*\]";
    // Execute replacement using IncludeDocumentEvaluator.
    doc.Range.Replace(new Regex(patternIncludeDocument), new ReplaceEvaluator(IncludeDocumentEvaluator), false);
}
private ReplaceAction IncludeDocumentEvaluator(object sender, ReplaceEvaluatorArgs e)
{
    InsertDocument(e.MatchNode, new Document(e.Match.Groups[1].Value));
    // Eraze "[Include Document: filename ]" text.
    e.Replacement = "";
    // commit replacement
    return ReplaceAction.Replace;
}

public void InsertDocument(Node node, Document doc)
{
    Document dstDoc = node.Document;
    Section insertedSection;
    int index;
    if (node.GetAncestor(typeof(Cell)) != null || node.GetAncestor(typeof(Shape)) != null)
    {
        // Insertion point is tracked to Cell or Shape level:
        // - insert appended document on node by node basis.
        index = node.ParentNode.ChildNodes.IndexOf(node);
        foreach (Section section in doc.Sections)
        {
            insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
            foreach (Node insertedNode in insertedSection.Body.ChildNodes)
            {
                // Only Paragraph or Table nodes can be inserted into Cell or Shape
                if (node is Paragraph || node is Table)
                {
                    node.ParentNode.ChildNodes.Insert(index++, insertedNode);
                }
            }
        }
    }
    else
    {
        // Insertion point is tracked to Section.Body level:
        // - insert appended document on section by section basis.
        Section dstSection;
        Body body = (Body)node.GetAncestor(typeof(Body));
        if (body.LastChild != node)
        {
            DocumentBuilder builder = new DocumentBuilder(dstDoc);
            builder.MoveTo(node);
            builder.InsertBreak(BreakType.SectionBreakContinuous);
            dstSection = builder.CurrentParagraph.ParentSection;
        }
        else
        {
            dstSection = (Section)node.GetAncestor(typeof(Section));
        }
        index = dstDoc.Sections.IndexOf(dstSection);
        int index0 = index;
        foreach (Section section in doc.Sections)
        {
            insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
            dstDoc.Sections.Insert(index++, insertedSection);
        }
        dstDoc.Sections[index0].PageSetup.SectionStart = SectionStart.Continuous;
    }
}
///
/// Executes all "[Include Document: filename ]" statements in the document.
///
private void IncludeDocumentExecute(Document doc)
{
    string patternIncludeDocument = @"\[Include Document:\s\*(?.\*?)\s\*\]";
    // Execute replacement using IncludeDocumentEvaluator.
    doc.Range.Replace(new Regex(patternIncludeDocument), new ReplaceEvaluator(IncludeDocumentEvaluator), false);
}

private ReplaceAction IncludeDocumentEvaluator(object sender, ReplaceEvaluatorArgs e)
{
    InsertDocument(e.MatchNode, new Document(e.Match.Groups[1].Value));
    // Eraze "[Include Document: filename ]" text.
    e.Replacement = "";
    // commit replacement
    return ReplaceAction.Replace;
}

public void InsertDocument(Node node, Document doc)
{
    Document dstDoc = node.Document;
    Section insertedSection;
    int index;
    if (node.GetAncestor(typeof(Cell)) != null || node.GetAncestor(typeof(Shape)) != null)
    {
        // Insertion point is tracked to Cell or Shape level:
        // - insert appended document on node by node basis.
        index = node.ParentNode.ChildNodes.IndexOf(node);
        foreach (Section section in doc.Sections)
        {
            insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
            foreach (Node insertedNode in insertedSection.Body.ChildNodes)
            {
                // Only Paragraph or Table nodes can be inserted into Cell or Shape
                if (node is Paragraph || node is Table)
                {
                    node.ParentNode.ChildNodes.Insert(index++, insertedNode);
                }
            }
        }
    }
    else
    {
        // Insertion point is tracked to Section.Body level:
        // - insert appended document on section by section basis.
        Section dstSection;
        Body body = (Body)node.GetAncestor(typeof(Body));
        if (body.LastChild != node)
        {
            DocumentBuilder builder = new DocumentBuilder(dstDoc);
            builder.MoveTo(node);
            builder.InsertBreak(BreakType.SectionBreakContinuous);
            dstSection = builder.CurrentParagraph.ParentSection;
        }
        else
        {
            dstSection = (Section)node.GetAncestor(typeof(Section));
        }
        index = dstDoc.Sections.IndexOf(dstSection);
        int index0 = index;
        foreach (Section section in doc.Sections)
        {
            insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
            dstDoc.Sections.Insert(index++, insertedSection);
        }
        dstDoc.Sections[index0].PageSetup.SectionStart = SectionStart.Continuous;
    }
}

Thank you for your answer. It gave me an idea on how to do it. Too bad it was written in C# but that’s my mistake since I forgot to mention on my first post that we are working on the JAVA version.

Actually, what we are trying to do is replace a run from one document by paragraphs from others.

We manage to locate the run in the document but fail to replace it.

Could you give us some JAVA sample codes for us to understand how to do this?

Thanks.

Dav

Hi, Dav!

This is previous C# answer ported to java:

import org.testng.annotations.Test;
import java.util.regex.Pattern;

/**
 \* The main trick is to implement replace() method from com.aspose.words.ReplaceEvaluator interface.
 */
public class OSTENDITest implements ReplaceEvaluator
{
    @Test
    public void TestOSTENDI() throws Exception
    {
        Document doc = new Document("C:/data/test.doc");
        includeDocumentExecute(doc);
        doc.save("C:/data/test_out.doc");
    }

    /**
     \* Executes all "[Include Document: filename ]" statements in the document.
     */
    private void includeDocumentExecute(Document doc) throws Exception
    {
        String patternIncludeDocument = "\\[Include Document:\\s*(.*)\\s*\\]";
        //Range.replace() calls this.replace() method for each match found during a replace operation.
        doc.getRange().replace(Pattern.compile(patternIncludeDocument), this, false);
    }

    /**
     \* A user implemented ReplaceEvaluator.replace() method is called for each
     \* match found during a replace operation.
     \*
     \* @return An enumerated value that specifies the action to be taken for the current match.
     \*/
    public int replace(Object sender, ReplaceEvaluatorArgs e) throws Exception
    {
        InsertDocument(e.getMatchNode(), new Document(e.matcher().group(1)));
        // Eraze "[Include Document: filename ]" text.
        e.setReplacement("");
        // commit replacement
        return ReplaceAction.REPLACE;
    }

    public void InsertDocument(Node node, Document doc) throws Exception
    {
        Document dstDoc = node.getDocument();
        Section insertedSection;
        int index;
        if (node.getAncestor(Cell.class) != null || node.getAncestor(Shape.class) != null)
        {
            // Insertion point is tracked to Cell or Shape level:
            // - insert appended document on node by node basis.
            index = node.getParentNode().getChildNodes().indexOf(node);
            for (Node section : doc.getSections())
            {
                insertedSection = (Section) dstDoc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
                for (Node insertedNode : insertedSection.getBody().getChildNodes())
                {
                    // Only Paragraph or Table nodes can be inserted into Cell or Shape
                    if (node instanceof Paragraph || node instanceof Table)
                    {
                        node.getParentNode().getChildNodes().insert(index++, insertedNode);
                    }
                }
            }
        }
        else
        {
            // Insertion point is tracked to Section.Body level:
            // - insert appended document on section by section basis.
            Section dstSection;
            Body body = (Body) node.getAncestor(Body.class);
            if (body.getLastChild() != node)
            {
                DocumentBuilder builder = new DocumentBuilder(dstDoc);
                builder.moveTo(node);
                builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
                dstSection = builder.getCurrentParagraph().getParentSection();
            }
            else
            {
                dstSection = (Section) node.getAncestor(Section.class);
            }
            index = dstDoc.getSections().indexOf(dstSection);
            int index0 = index;
            for (Node section : doc.getSections())
            {
                insertedSection = (Section) dstDoc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
                dstDoc.getSections().insert(index++, insertedSection);
            }
            dstDoc.getSections().get(index0).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
        }
    }
}

Regards,

Hi Konstantin,

This is awesome. This is what I was looking for.
Thanks for the help

-Sunil

A post was merged into an existing topic: Question about FindAndReplace feature of Aspose.Words for Java

A post was split to a new topic: Question about FindAndReplace feature of Aspose.Words for Java

A post was split to a new topic: How to replace text in document’s footer