Код: Выделить всё
private void replaceTextInParagraph(XWPFParagraph paragraph, Map variableValues) {
List runs = paragraph.getRuns();
StringBuilder paragraphText = new StringBuilder();
// Step 1: Gather all text from the paragraph
for (XWPFRun run : runs) {
String text = run.getText(0);
if (text != null) {
paragraphText.append(text);
}
}
// Step 2: Perform replacements on the complete text
String replacedText = paragraphText.toString();
for (Map.Entry entry : variableValues.entrySet()) {
replacedText = replacedText.replace("", entry.getValue());
}
// Step 3: Clear all existing runs
for (XWPFRun run : runs) {
run.setText("", 0);
}
// Step 4: Add the replaced text back to the paragraph
if (!replacedText.isEmpty()) {
XWPFRun newRun = paragraph.createRun();
newRun.setText(replacedText);
}
}
И это метод, который я использую для просмотра документа.
Код: Выделить всё
public void replaceVariables(XWPFDocument document, Map variableValues) {
for (XWPFParagraph paragraph : document.getParagraphs()) {
replaceTextInParagraph(paragraph, variableValues);
}
for (XWPFHeader header : document.getHeaderList()) {
for (XWPFParagraph paragraph : header.getParagraphs()) {
replaceTextInParagraph(paragraph, variableValues);
}
processTables(header.getTables(), variableValues);
}
for (XWPFFooter footer : document.getFooterList()) {
for (XWPFParagraph paragraph : footer.getParagraphs()) {
replaceTextInParagraph(paragraph, variableValues);
}
processTables(footer.getTables(), variableValues);
}
processTables(document.getTables(), variableValues);
}
private void processTables(List tables, Map variableValues) {
for (XWPFTable table : tables) {
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
for (XWPFParagraph paragraph : cell.getParagraphs()) {
replaceTextInParagraph(paragraph, variableValues);
}
}
}
}
}
Код: Выделить всё
private void replaceTextInParagraph(XWPFParagraph paragraph, Map variableValues) {
List runs = paragraph.getRuns();
// List newRuns = new ArrayList();
StringBuilder paragraphText = new StringBuilder();
for (XWPFRun run : runs) {
// XWPFRun newRun = run;
// newRun.setText("", 0);
// newRuns.add(newRun);
String text = run.getText(0);
if (text != null) {
paragraphText.append(text);
}
}
String replacedText = paragraphText.toString();
for (Map.Entry entry : variableValues.entrySet()) {
replacedText = replacedText.replace("", entry.getValue());
}
// while (!paragraph.getRuns().isEmpty()) {
// paragraph.removeRun(0);
// }
String [] textParts = replacedText.split(" ");
for (int i = 0; i < runs.size(); i++) {
int j = 0;
runs.get(i).setText(textParts[j], 0);
paragraph.addRun(runs.get(i));
}
//
// XWPFRun newRun = paragraph.createRun();
// newRun.setText(replacedText);
}
private void replaceTextInParagraph(XWPFParagraph paragraph, Map variableValues) {
StringBuilder paragraphText = new StringBuilder();
List runs = paragraph.getRuns();
// First, get the entire text of the paragraph
for (XWPFRun run : runs) {
String text = run.getText(0);
if (text != null) {
paragraphText.append(text);
}
}
// Perform the replacement on the entire paragraph text
String replacedText = paragraphText.toString();
for (Map.Entry entry : variableValues.entrySet()) {
replacedText = replacedText.replace("", entry.getValue());
}
// Split the replaced text back into runs
int currentIndex = 0;
for (XWPFRun run : runs) {
String text = run.getText(0);
if (text != null) {
int remainingLength = replacedText.length() - currentIndex; // Calculate remaining length in the replaced text
String subText;
if (text.length()
Подробнее здесь: [url]https://stackoverflow.com/questions/78982146/how-to-replace-variables-in-docx-in-apache-poi-java[/url]
Мобильная версия