Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request - return TableCell #1

Open
svvparakala opened this issue May 12, 2020 · 1 comment
Open

Request - return TableCell #1

svvparakala opened this issue May 12, 2020 · 1 comment

Comments

@svvparakala
Copy link

Is there some way to have the method replaceWordsinTables not just replace the word, but also return the location of the table cell in which the replaced word was found? I would like to do some custom formatting on it, and instead of having to search for that specific table cell again just use the return value to edit it.

@Naqued
Copy link

Naqued commented Jan 8, 2021

Hey @svvparakala, i have made a solution to get a Pointer (XmlCursor) were the bookmark has been found

i have Wrapped the wrapper :
TextReplacer and WordReplacer

Code is below :

`public class TextReplacerWrapper extends TextReplacer {
private XmlCursor cursor;

@Override
public void onWordFoundInRun(XWPFRun run) {
	super.onWordFoundInRun(run);
	this.cursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor();    	    	
}

@Override
public void onWordFoundInPreviousCurrentNextRun(List<XWPFRun> runs, int currentRun) {
    super.onWordFoundInPreviousCurrentNextRun(runs, currentRun);
	this.cursor = ((XWPFParagraph) runs.get(0).getParent()).getCTP().newCursor();    	    	

}
public void replaceInText(XWPFDocument document, String bookmark, String replacement) {
	super.replaceInText(document, bookmark, replacement);
}
public void replaceInTable(XWPFDocument document, String bookmark, String replacement) {
	super.replaceInTable(document, bookmark, replacement);
}

public XmlCursor getCursor()
{
	return this.cursor;
}

}`

and

`public class WordReplacerWrapper extends WordReplacer {
private TextReplacerWrapper replacer;
private XWPFDocument document;
private XmlCursor cursor;

public WordReplacerWrapper(XWPFDocument xwpfDoc) {
	super(xwpfDoc);
    this.init(xwpfDoc);

}

public WordReplacerWrapper(File docxFile) throws IOException {
    super(docxFile);
    this.init(this.document);
}

private void init(XWPFDocument xwpfDoc) {
    if (xwpfDoc == null) throw new NullPointerException();
    this.document = xwpfDoc;
    this.replacer = new TextReplacerWrapper();
}

/**
 * Replaces all occurrences of a bookmark only in the text of the file with a replacement string.
 * @param bookmark word to replace.
 * @param replacement word of replacement.
 */
public void replaceWordsInText(String bookmark, String replacement) {
    replacer.replaceInText(document, bookmark, replacement);
}


/**
 * Replaces all occurrences of a bookmark only in tables of the file with a replacement string.
 * @param bookmark word to replace.
 * @param replacement word of replacement.
 */
public void replaceWordsInTables(String bookmark, String replacement) {
    replacer.replaceInTable(document, bookmark, replacement);

}


public XmlCursor getCursor()
{
	this.cursor = this.replacer.getCursor();
	return this.cursor;
}

}`

This allow you to :

 `
 WordReplacerWrapper wReplacer =  new WordReplacerWrapper(document);
 wReplacer.replaceWordsInText(bookMark, valueToReplace);
 
XmlCursor tableCursor = wReplacer.getCursor();

`

then you do what you want with the cursor : 
exemple 

`XWPFDocument doc.insertNewTbl(tableCursor);`

This is a temporary solution untill i have some time and PR the code to @deividasstr 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants