diff --git a/apache-poi-2/pom.xml b/apache-poi-2/pom.xml index af959292fa..9a01a76d73 100644 --- a/apache-poi-2/pom.xml +++ b/apache-poi-2/pom.xml @@ -1,7 +1,7 @@ - + 4.0.0 apache-poi-2 0.0.1-SNAPSHOT @@ -19,10 +19,15 @@ poi-ooxml ${poi.version} + + org.apache.poi + poi-scratchpad + ${poi.version} + - 5.2.0 + 5.2.3 \ No newline at end of file diff --git a/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocTextReplacer.java b/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocTextReplacer.java new file mode 100644 index 0000000000..f661551ce9 --- /dev/null +++ b/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocTextReplacer.java @@ -0,0 +1,38 @@ +package com.baeldung.poi.replacevariables; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.hwpf.usermodel.Range; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; + +public class DocTextReplacer { + + public void replaceText() throws IOException { + String filePath = getClass().getClassLoader() + .getResource("baeldung.doc") + .getPath(); + try (InputStream inputStream = new FileInputStream(filePath); POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream)) { + HWPFDocument doc = new HWPFDocument(fileSystem); + doc = replaceText(doc, "Baeldung", "Hello"); + saveFile(filePath, doc); + doc.close(); + } + } + + private HWPFDocument replaceText(HWPFDocument doc, String originalText, String updatedText) { + Range range = doc.getRange(); + range.replaceText(originalText, updatedText); + return doc; + } + + private void saveFile(String filePath, HWPFDocument doc) throws IOException { + try (FileOutputStream out = new FileOutputStream(filePath)) { + doc.write(out); + } + } + +} diff --git a/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocxNaiveTextReplacer.java b/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocxNaiveTextReplacer.java new file mode 100644 index 0000000000..34c2bc43e5 --- /dev/null +++ b/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocxNaiveTextReplacer.java @@ -0,0 +1,63 @@ +package com.baeldung.poi.replacevariables; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.poi.xwpf.usermodel.XWPFRun; +import org.apache.poi.xwpf.usermodel.XWPFTable; +import org.apache.poi.xwpf.usermodel.XWPFTableCell; +import org.apache.poi.xwpf.usermodel.XWPFTableRow; + +public class DocxNaiveTextReplacer { + + public void replaceText() throws IOException { + String filePath = getClass().getClassLoader() + .getResource("baeldung-copy.docx") + .getPath(); + try (InputStream inputStream = new FileInputStream(filePath)) { + XWPFDocument doc = new XWPFDocument(inputStream); + doc = replaceText(doc, "Baeldung", "Hello"); + saveFile(filePath, doc); + doc.close(); + } + } + + private XWPFDocument replaceText(XWPFDocument doc, String originalText, String updatedText) { + replaceTextInParagraphs(doc.getParagraphs(), originalText, updatedText); + for (XWPFTable tbl : doc.getTables()) { + for (XWPFTableRow row : tbl.getRows()) { + for (XWPFTableCell cell : row.getTableCells()) { + replaceTextInParagraphs(cell.getParagraphs(), originalText, updatedText); + } + } + } + return doc; + } + + private void replaceTextInParagraphs(List paragraphs, String originalText, String updatedText) { + paragraphs.forEach(paragraph -> replaceTextInParagraph(paragraph, originalText, updatedText)); + } + + private void replaceTextInParagraph(XWPFParagraph paragraph, String originalText, String updatedText) { + List runs = paragraph.getRuns(); + for (XWPFRun run : runs) { + String text = run.getText(0); + if (text != null && text.contains(originalText)) { + String updatedRunText = text.replace(originalText, updatedText); + run.setText(updatedRunText, 0); + } + } + } + + private void saveFile(String filePath, XWPFDocument doc) throws IOException { + try (FileOutputStream out = new FileOutputStream(filePath)) { + doc.write(out); + } + } + +} diff --git a/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocxTextReplacer.java b/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocxTextReplacer.java new file mode 100644 index 0000000000..2d08d24a4e --- /dev/null +++ b/apache-poi-2/src/main/java/com/baeldung/poi/replacevariables/DocxTextReplacer.java @@ -0,0 +1,65 @@ +package com.baeldung.poi.replacevariables; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.Objects; + +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.poi.xwpf.usermodel.XWPFRun; +import org.apache.poi.xwpf.usermodel.XWPFTable; +import org.apache.poi.xwpf.usermodel.XWPFTableCell; +import org.apache.poi.xwpf.usermodel.XWPFTableRow; + +public class DocxTextReplacer { + + public void replaceText() throws IOException { + String filePath = getClass().getClassLoader() + .getResource("baeldung.docx") + .getPath(); + try (InputStream inputStream = new FileInputStream(filePath)) { + XWPFDocument doc = new XWPFDocument(inputStream); + doc = replaceText(doc, "Baeldung", "Hello"); + saveFile(filePath, doc); + doc.close(); + } + } + + private XWPFDocument replaceText(XWPFDocument doc, String originalText, String updatedText) { + replaceTextInParagraphs(doc.getParagraphs(), originalText, updatedText); + for (XWPFTable tbl : doc.getTables()) { + for (XWPFTableRow row : tbl.getRows()) { + for (XWPFTableCell cell : row.getTableCells()) { + replaceTextInParagraphs(cell.getParagraphs(), originalText, updatedText); + } + } + } + return doc; + } + + private void replaceTextInParagraphs(List paragraphs, String originalText, String updatedText) { + paragraphs.forEach(paragraph -> replaceTextInParagraph(paragraph, originalText, updatedText)); + } + + private void replaceTextInParagraph(XWPFParagraph paragraph, String originalText, String updatedText) { + String paragraphText = paragraph.getParagraphText(); + if (paragraphText.contains(originalText)) { + String updatedParagraphText = paragraphText.replace(originalText, updatedText); + while (paragraph.getRuns().size() > 0) { + paragraph.removeRun(0); + } + XWPFRun newRun = paragraph.createRun(); + newRun.setText(updatedParagraphText); + } + } + + private void saveFile(String filePath, XWPFDocument doc) throws IOException { + try (FileOutputStream out = new FileOutputStream(filePath)) { + doc.write(out); + } + } + +} diff --git a/apache-poi-2/src/main/resources/baeldung-copy.docx b/apache-poi-2/src/main/resources/baeldung-copy.docx new file mode 100644 index 0000000000..2cb76e8ffd Binary files /dev/null and b/apache-poi-2/src/main/resources/baeldung-copy.docx differ diff --git a/apache-poi-2/src/main/resources/baeldung.doc b/apache-poi-2/src/main/resources/baeldung.doc new file mode 100644 index 0000000000..1b8474d65b Binary files /dev/null and b/apache-poi-2/src/main/resources/baeldung.doc differ diff --git a/apache-poi-2/src/main/resources/baeldung.docx b/apache-poi-2/src/main/resources/baeldung.docx new file mode 100644 index 0000000000..f0de4e057b Binary files /dev/null and b/apache-poi-2/src/main/resources/baeldung.docx differ diff --git a/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocTextReplacerUnitTest.java b/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocTextReplacerUnitTest.java new file mode 100644 index 0000000000..0c3d80a354 --- /dev/null +++ b/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocTextReplacerUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.poi.replacevariables; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; + +import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.hwpf.extractor.WordExtractor; +import org.junit.jupiter.api.Test; + +class DocTextReplacerUnitTest { + + @Test + void whenReplaceText_ThenTextReplaced() throws IOException { + new DocTextReplacer().replaceText(); + + String filePath = getClass().getClassLoader() + .getResource("baeldung.doc") + .getPath(); + try (FileInputStream fis = new FileInputStream(filePath); HWPFDocument document = new HWPFDocument(fis); WordExtractor extractor = new WordExtractor(document)) { + long occurrencesOfHello = Arrays.stream(extractor.getText() + .split("\\s+")) + .filter(s -> s.contains("Hello")) + .count(); + assertEquals(5, occurrencesOfHello); + } + } + +} diff --git a/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocxNaiveTextReplacerUnitTest.java b/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocxNaiveTextReplacerUnitTest.java new file mode 100644 index 0000000000..324e63eb51 --- /dev/null +++ b/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocxNaiveTextReplacerUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.poi.replacevariables; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; + +import org.apache.poi.xwpf.extractor.XWPFWordExtractor; +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.junit.jupiter.api.Test; + +class DocxNaiveTextReplacerUnitTest { + + @Test + void whenReplaceText_ThenTextReplaced() throws IOException { + new DocxNaiveTextReplacer().replaceText(); + + String filePath = getClass().getClassLoader() + .getResource("baeldung-copy.docx") + .getPath(); + try (FileInputStream fis = new FileInputStream(filePath); XWPFDocument document = new XWPFDocument(fis); XWPFWordExtractor extractor = new XWPFWordExtractor(document)) { + long occurrencesOfHello = Arrays.stream(extractor.getText() + .split("\\s+")) + .filter(s -> s.contains("Hello")) + .count(); + assertTrue(occurrencesOfHello < 5); + } + } + +} diff --git a/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocxTestReplacerUnitTest.java b/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocxTestReplacerUnitTest.java new file mode 100644 index 0000000000..d09f6b003d --- /dev/null +++ b/apache-poi-2/src/test/java/com/baeldung/poi/replacevariables/DocxTestReplacerUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.poi.replacevariables; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; + +import org.apache.poi.xwpf.extractor.XWPFWordExtractor; +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.junit.jupiter.api.Test; + +class DocxTestReplacerUnitTest { + + @Test + void whenReplaceText_ThenTextReplaced() throws IOException { + new DocxTextReplacer().replaceText(); + + String filePath = getClass().getClassLoader() + .getResource("baeldung.docx") + .getPath(); + try (FileInputStream fis = new FileInputStream(filePath); XWPFDocument document = new XWPFDocument(fis); XWPFWordExtractor extractor = new XWPFWordExtractor(document)) { + long occurrencesOfHello = Arrays.stream(extractor.getText() + .split("\\s+")) + .filter(s -> s.contains("Hello")) + .count(); + assertEquals(5, occurrencesOfHello); + } + } + +}