Hello everyone I have the following method:
Hi,
Document doc = new Document(getMyDir() + “template.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);Shape img = builder.insertImage(getMyDir() + “Aspose.Words.jpg”);
Bookmark bm = doc.getRange().getBookmarks().get(“bm”);
bm.setText("");BookmarkStart start = bm.getBookmarkStart();
start.getParentNode().insertAfter(img, start);doc.save(getMyDir() + “awjava-15.9.0.docx”);
Thank so much! for response, my problem was my template document, you know other ways by insert image, when i want replace a string text into my template document? for example replace the image where found the string text = [image1].I do not want to use Bookmark
Hi,
Document doc = new Document(getMyDir() + “template.docx”);Pattern regex = Pattern.compile("\[image1\]", Pattern.CASE_INSENSITIVE);
doc.getRange().replace(regex, new ReplaceEvaluator(), false);doc.save(getMyDir() + “awjava-15.9.0.docx”);
static class ReplaceEvaluator implements IReplacingCallback
{
/**
* This method is called by the Aspose.Words find and replace engine for each match.
* This method highlights the match string, even if it spans multiple runs.
*/
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();<font color="GREEN"><i>// 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());<font color="GREEN"><i>// This array is used to store all nodes of the match for further highlighting.
ArrayList runs = new ArrayList();
<font color="GREEN"><i>// 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();<font color="GREEN"><i>// 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));
}<font color="GREEN"><i>// 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 <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DocumentBuilder<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Document<font color="BLUE"><b>)</b></font> e<font color="BLUE"><b>.</b></font>getMatchNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getDocument<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> builder<font color="BLUE"><b>.</b></font>moveTo<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Run<font color="BLUE"><b>)</b></font> runs<font color="BLUE"><b>.</b></font>get<font color="BLUE"><b>(</b></font>runs<font color="BLUE"><b>.</b></font>size<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">-</font> <font color="BROWN">1</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> builder<font color="BLUE"><b>.</b></font>insertImage<font color="BLUE"><b>(</b></font><font color="PURPLE">"D:\\Temp\\Aspose.Words.jpg"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> <font color="RED"><b>for</b></font> <font color="BLUE"><b>(</b></font>Run run <font color="BLUE">:</font> <font color="BLUE"><b>(</b></font>Iterable<font color="BLUE"><</font>Run<font color="BLUE">></font><font color="BLUE"><b>)</b></font> runs<font color="BLUE"><b>)</b></font> run<font color="BLUE"><b>.</b></font>remove<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> <font color="GREEN"><i>// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.SKIP;
}<font color="GREEN"><i>/** * Splits text of the specified run into two runs. * Inserts the new run just after the specified run. */</i></font> <font color="RED"><b>private</b></font> Run splitRun<font color="BLUE"><b>(</b></font>Run run<font color="BLUE"><b>,</b></font> <font color="RED"><b>int</b></font> position<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font> Run afterRun <font color="BLUE">=</font> <font color="BLUE"><b>(</b></font>Run<font color="BLUE"><b>)</b></font>run<font color="BLUE"><b>.</b></font>deepClone<font color="BLUE"><b>(</b></font>true<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> afterRun<font color="BLUE"><b>.</b></font>setText<font color="BLUE"><b>(</b></font>run<font color="BLUE"><b>.</b></font>getText<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>substring<font color="BLUE"><b>(</b></font>position<font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> run<font color="BLUE"><b>.</b></font>setText<font color="BLUE"><b>(</b></font>run<font color="BLUE"><b>.</b></font>getText<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>substring<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font><font color="BROWN">0</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>,</b></font> <font color="BLUE"><b>(</b></font><font color="BROWN">0</font><font color="BLUE"><b>)</b></font> <font color="BLUE">+</font> <font color="BLUE"><b>(</b></font>position<font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> run<font color="BLUE"><b>.</b></font>getParentNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>insertAfter<font color="BLUE"><b>(</b></font>afterRun<font color="BLUE"><b>,</b></font> run<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> <font color="RED"><b>return</b></font> afterRun<font color="BLUE"><b>;</b></font> <font color="BLUE"><b>}</b></font>
}
Thanks! by response, but I don’t can resolve the issue.
Hi,