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

Free Support Forum - aspose.com

How to use Aspose.Words for Java with ColdFusion

I recently purchased Aspose.Words for Java which I’m using with ColdFusion.

However, I’m struggling to reproduce the find and replace example 3 (using a custom evaluator) from https://docs.aspose.com/words/java/find-and-replace/
in a ColdFusion environment.

Could someone please point me in the right direction with some ColdFusion sample code?

Many thanks in advance

Hi Malcolm,

Thanks for your inquiry. Please read following documentation link for your kind reference.
https://docs.aspose.com/words/java/coldfusion-and-aspose-words-for-java/

First, create a Java class that implement IReplacingCallback interface and perform custom evaluator. Compile it, and place in a jar file, and place it in the “ColdFusion8\wwwroot\WEB-INF\lib” folder. See the Java code example at following documentation link. The last code example in this documentation link use the same approach.
https://docs.aspose.com/words/java/find-and-replace/

Hope this helps you. Pleas let us know if you have any more queries.

Hi Tahir,

Thank you for your suggestions so far.

I will freely admit to you that I don’t know too much about programming in java. I use Coldfusion for our application development as I said before I wanted to purchase Aspose Words to make it easier for me to manipulate Microsoft Word documents from within Coldfusion. I did not expect that I would need to write java code to achieve certain replacement of special characters etc. So I am heavily dependent on following any examples that you give or offer through hyperlinks to other threads etc.

I have successfully been able to create simple java classes and integrate these into Coldfusion making them available for me to make calls to them. So I understand the basics of integrating with Coldfusion and making calls to java classes from Coldfusion templates.

Please refer to this example from Alexey Noskov:
https://forum.aspose.com/t/116374

I am struggling with two bits of the puzzle as follows:

(1) I am not finding it easy to convert what I am presuming is C++ code shown in this example into java code. I am presuming that it is C++ rather than java because if I cut and paste the example code and try to compile it with java I receive many syntax errors.

(2) I am also not finding it easy to translate the top section of this sample code that makes the calls to the helper class into Coldfusion equivalent syntax, specifically this bit:

// Create helper and perform replace operation.
ReplaceHelper helper = new ReplaceHelper(doc);
helper.Replace("mytext", "This text can contain any special charactes\nparagraph break\fpage breaks, etc…");

Hi Malcolm,

Thanks for your inquiry.

You can achieve your requirement by implementing IReplacingCallback interface. Please use the same approach shared at following documentation link to achieve your requirements.
https://docs.aspose.com/words/java/find-and-replace/

First, create a Java class for ReplaceEvaluatorTest and FindandRepalce. Compile it, and place in a jar file, and place it in the “ColdFusion8\wwwroot\WEB-INF\lib” folder.

public class ReplaceEvaluatorTest implements IReplacingCallback
{
    public String newText;
    public int replacing(ReplacingArgs e) throws Exception
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.getMatchNode();
        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.getMatchOffset() > 0)
            currentNode = splitRun((Run)currentNode, e.getMatchOffset());
        // This array is used to store all nodes of the match.
        ArrayList runs = new ArrayList();
        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().group().length();
        while (
                (remainingLength > 0) &&
                        (currentNode != null) &&
                        (currentNode.getText().length() <= remainingLength))
        {
            runs.add(currentNode);
            remainingLength = remainingLength - currentNode.getText().length();
            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.getNextSibling();
            }
            while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
        }
        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
        {
            splitRun((Run)currentNode, remainingLength);
            runs.add(currentNode);
        }
        DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
        builder.moveTo((Run)runs.get(0));
        builder.write(newText);
        // Remove runs
        for (Run run : (Iterable) runs)
        {
            run.remove();
        }
        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.SKIP;
    }
    /**
        * Splits text of the specified run into two runs.
        * Inserts the new run just after the specified run.
        */
    private Run splitRun(Run run, int position) throws Exception
    {
        Run afterRun = (Run)run.deepClone(true);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }
}
public class FindandRepalce
{
    public void FindandReplace(Document doc, String regexString, String newText) throws Exception
    {
        ReplaceEvaluatorTest obj = new ReplaceEvaluatorTest();
        obj.newText = newText;
        Pattern regex = Pattern.compile(regexString, Pattern.CASE_INSENSITIVE);
        doc.getRange().replace(regex, obj, false);
    }
}

Please check following Java code. You need to create FindandRepalce object and pass Document, Regex and the text value which you want to FindandReplace method. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
// Craete object to FindandRepalce class
FindandRepalce object = new FindandRepalce();
// Second parameter is Regex
object.FindandReplace(doc, "myText", "new text");
doc.save(MyDir + "Out.docx");

https://docs.aspose.com/words/java/find-and-replace/

Hi Tahir,

Thank you for your suggestions.

I’m still having problems with this sample java code you have supplied, when I follow your instructions

"First, create a Java class for ReplaceEvaluatorTest and FindandRepalce. Compile it"

when trying to compile ReplaceEvaluatorTest.java I receive the following compilation errors:

javac ReplaceEvaluatorTest.java

ReplaceEvaluatorTest.java:1: error: cannot find symbol
public class ReplaceEvaluatorTest implements IReplacingCallback
^
symbol: class IReplacingCallback
ReplaceEvaluatorTest.java:9: error: cannot find symbol
public int replacing(ReplacingArgs e) throws Exception
^
symbol: class ReplacingArgs
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:123: error: cannot find symbol
private Run splitRun(Run run, int position) throws Exception
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:123: error: cannot find symbol
private Run splitRun(Run run, int position) throws Exception
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:15: error: cannot find symbol
Node currentNode = e.getMatchNode();
^
symbol: class Node
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:25: error: cannot find symbol
currentNode = splitRun((Run)currentNode, e.getMatchOffset());
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:31: error: cannot find symbol
ArrayList runs = new ArrayList();
^
symbol: class ArrayList
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:31: error: cannot find symbol
ArrayList runs = new ArrayList();
^
symbol: class ArrayList
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:67: error: cannot find symbol
while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
^
symbol: variable NodeType
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:79: error: cannot find symbol
splitRun((Run)currentNode, remainingLength);
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:87: error: cannot find symbol
DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
^
symbol: class DocumentBuilder
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:87: error: cannot find symbol
DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
^
symbol: class DocumentBuilder
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:87: error: cannot find symbol
DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
^
symbol: class Document
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:89: error: cannot find symbol
builder.moveTo((Run)runs.get(0));
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:99: error: cannot find symbol
for (Run run : (Iterable) runs)
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:99: error: cannot find symbol
for (Run run : (Iterable) runs)
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:109: error: cannot find symbol
return ReplaceAction.SKIP;
^
symbol: variable ReplaceAction
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:127: error: cannot find symbol
Run afterRun = (Run)run.deepClone(true);
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:127: error: cannot find symbol
Run afterRun = (Run)run.deepClone(true);
^
symbol: class Run
location: class ReplaceEvaluatorTest
19 errors

Please tell me what I am doing wrong?
Thank you

Hi Malcolm,

Thanks for your inquiry. I would assume if the class was not visible then you
would get a compile error. Please ensure that the Aspose.Words jare file is in your classpath. Please read following web links about setting classpath for your kind reference.
http://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html
https://coderanch.com/wiki/660138/Set-Classpath

Hi Tahir,

Thank you for all your help so far. As I already told you I’m a total novice java programmer so this classpath thing is new to me.

My Aspose Words jar file is located here:

C:\ColdFusion10\cfusion\wwwroot\WEB-INF\lib

JAVA_HOME environment variable on my server box points to the JRE version I am using here:

C:\Program Files\Java\jdk1.7.0_15

I have therefore set my CLASSPATH environment variable to be:

.;C:\ColdFusion10\cfusion\wwwroot\WEB-INF\lib;C:\ColdFusion10\cfusion\wwwroot\WEB-INF\bin;C:\ColdFusion10\cfusion\wwwroot\WEB-INF\jre\lib;C:\ColdFusion10\cfusion\wwwroot\WEB-INF\jre\bin*;***

My .java files containing the code examples you gave me above in post #552736 are located here:

C:\inetpub\wwwroot\Workflow\JavaLibTest\JavaLib

When positioned in a command prompt at this JavaLib location if I execute the following command:

javac ReplaceEvaluatorTest.java (containing your sample code)

I receive the following compilation errors again:

ReplaceEvaluatorTest.java:1: error: cannot find symbol
public class ReplaceEvaluatorTest implements IReplacingCallback
^
symbol: class IReplacingCallback
ReplaceEvaluatorTest.java:9: error: cannot find symbol
public int replacing(ReplacingArgs e) throws Exception
^
symbol: class ReplacingArgs
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:123: error: cannot find symbol
private Run splitRun(Run run, int position) throws Exception
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:123: error: cannot find symbol
private Run splitRun(Run run, int position) throws Exception
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:15: error: cannot find symbol
Node currentNode = e.getMatchNode();
^
symbol: class Node
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:25: error: cannot find symbol
currentNode = splitRun((Run)currentNode, e.getMatchOffset());
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:31: error: cannot find symbol
ArrayList runs = new ArrayList();
^
symbol: class ArrayList
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:31: error: cannot find symbol
ArrayList runs = new ArrayList();
^
symbol: class ArrayList
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:67: error: cannot find symbol
while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
^
symbol: variable NodeType
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:79: error: cannot find symbol
splitRun((Run)currentNode, remainingLength);
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:87: error: cannot find symbol
DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
^
symbol: class DocumentBuilder
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:87: error: cannot find symbol
DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
^
symbol: class DocumentBuilder
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:87: error: cannot find symbol
DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());
^
symbol: class Document
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:89: error: cannot find symbol
builder.moveTo((Run)runs.get(0));
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:99: error: cannot find symbol
for (Run run : (Iterable) runs)
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:99: error: cannot find symbol
for (Run run : (Iterable) runs)
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:109: error: cannot find symbol
return ReplaceAction.SKIP;
^
symbol: variable ReplaceAction
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:127: error: cannot find symbol
Run afterRun = (Run)run.deepClone(true);
^
symbol: class Run
location: class ReplaceEvaluatorTest
ReplaceEvaluatorTest.java:127: error: cannot find symbol
Run afterRun = (Run)run.deepClone(true);
^
symbol: class Run
location: class ReplaceEvaluatorTest
19 errors

Do you have any more suggestions please for what I might be doing wrong?
Thank you

Hi Malcolm,

Thanks for your inquiry. Please set the classpath using following command.

SET CLASSPATH=.;%CLASSPATH%;C:\aspose-words-14.5.0-jdk16.jar

You need to create Jar file using Java code shared here and place it in the “ColdFusion8\wwwroot\WEB-INF\lib” folder.

Instead of executing the Java commands at command prompt, I suggest you please use any IDE for Java development e.g Eclipse, NetBeans. See the following web links to learn how to create new Java project.
https://www.wikihow.com/Create-a-New-Java-Project-in-Eclipse
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-3.htm

Please read about creating the Jar file using Eclipse from here:
https://www.cs.utexas.edu/~scottm/cs307/handouts/Eclipse%20Help/jarInEclipse.htm
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm

If you do not want to use IDE, please read following web link about classpath.
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

How to set class path
https://coderanch.com/wiki/660138/Set-Classpath

After setting the class path compile your Java program.

How to create Jar file through command prompt.
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

A post was split to a new topic: Find and replace compile time error