Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
A "Run" defines a region of text within a docx document with a common set of properties. Word processors are
* relatively free in splitting a paragraph of text into multiple runs, so there is no strict rule to say over how many
* runs a word or a string of words is spread.
*
This class aggregates multiple runs so they can be treated as a single text, no matter how many runs the text
* spans.
*
* @author Joseph Verron
* @author Tom Hombergs
* @version ${version}
* @since 1.0.8
*/
public class PowerpointParagraph
implements Paragraph {
private final List runs = new ArrayList<>();
private final CTTextParagraph paragraph;
private int currentPosition = 0;
/**
* Constructs a new ParagraphWrapper for the given paragraph.
*
* @param paragraph the paragraph to wrap.
*/
public PowerpointParagraph(CTTextParagraph paragraph) {
this.paragraph = paragraph;
recalculateRuns();
}
/**
* Recalculates the runs of the paragraph. This method is called automatically by the constructor, but can also be
* called manually to recalculate the runs after a modification to the paragraph was done.
*/
private void recalculateRuns() {
currentPosition = 0;
this.runs.clear();
int index = 0;
for (Object contentElement : paragraph.getEGTextRun()) {
if (contentElement instanceof CTRegularTextRun r && !r.getT()
.isEmpty()) {
this.addRun(r, index);
}
index++;
}
}
/**
* Adds a run to the aggregation.
*
* @param run the run to add.
*/
private void addRun(CTRegularTextRun run, int index) {
int startIndex = currentPosition;
int endIndex = currentPosition + run.getT()
.length() - 1;
runs.add(new PowerpointRun(startIndex, endIndex, index, run));
currentPosition = endIndex + 1;
}
/**
* Replaces the given expression with the replacement object within
* the paragraph.
* The replacement object must be a valid DOCX4J Object.
*
* @param placeholder the expression to be replaced.
* @param replacement the object to replace the expression.
*/
@Override
public void replace(Placeholder placeholder, Object replacement) {
if (!(replacement instanceof CTRegularTextRun replacementRun))
throw new AssertionError("replacement is not a CTRegularTextRun");
String text = asString();
String full = placeholder.expression();
int matchStartIndex = text.indexOf(full);
if (matchStartIndex == -1) {
// nothing to replace
return;
}
int matchEndIndex = matchStartIndex + full.length() - 1;
List affectedRuns = getAffectedRuns(matchStartIndex,
matchEndIndex);
boolean singleRun = affectedRuns.size() == 1;
List