diff --git a/apache-poi/.gitignore b/apache-poi/.gitignore new file mode 100644 index 0000000000..e05054868c --- /dev/null +++ b/apache-poi/.gitignore @@ -0,0 +1 @@ +*.docx diff --git a/apache-poi/pom.xml b/apache-poi/pom.xml new file mode 100644 index 0000000000..d94b2e2ad1 --- /dev/null +++ b/apache-poi/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + com.baeldung + apache-poi + 0.0.1-SNAPSHOT + + + 3.6.0 + 4.12 + 3.15 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + + + + junit + junit + ${junit.version} + test + + + org.apache.poi + poi-ooxml + ${poi.version} + + + diff --git a/apache-poi/src/main/java/com/baeldung/poi/word/WordDocument.java b/apache-poi/src/main/java/com/baeldung/poi/word/WordDocument.java new file mode 100644 index 0000000000..c05c323447 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/word/WordDocument.java @@ -0,0 +1,95 @@ +package com.baeldung.poi.word; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.poi.util.Units; +import org.apache.poi.xwpf.usermodel.ParagraphAlignment; +import org.apache.poi.xwpf.usermodel.UnderlinePatterns; +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.poi.xwpf.usermodel.XWPFRun; + +public class WordDocument { + public static String logo = "logo-leaf.png"; + public static String paragraph1 = "poi-word-para1.txt"; + public static String paragraph2 = "poi-word-para2.txt"; + public static String paragraph3 = "poi-word-para3.txt"; + public static String output = "rest-with-spring.docx"; + + public void handleSimpleDoc() throws Exception { + XWPFDocument document = new XWPFDocument(); + + XWPFParagraph title = document.createParagraph(); + title.setAlignment(ParagraphAlignment.CENTER); + XWPFRun titleRun = title.createRun(); + titleRun.setText("Build Your REST API with Spring"); + titleRun.setColor("009933"); + titleRun.setBold(true); + titleRun.setFontFamily("Courier"); + titleRun.setFontSize(20); + + XWPFParagraph subTitle = document.createParagraph(); + subTitle.setAlignment(ParagraphAlignment.CENTER); + XWPFRun subTitleRun = subTitle.createRun(); + subTitleRun.setText("from HTTP fundamentals to API Mastery"); + subTitleRun.setColor("00CC44"); + subTitleRun.setFontFamily("Courier"); + subTitleRun.setFontSize(16); + subTitleRun.setTextPosition(20); + subTitleRun.setUnderline(UnderlinePatterns.DOT_DOT_DASH); + + XWPFParagraph image = document.createParagraph(); + image.setAlignment(ParagraphAlignment.CENTER); + XWPFRun imageRun = image.createRun(); + imageRun.setTextPosition(20); + Path imagePath = Paths.get(ClassLoader.getSystemResource(logo).toURI()); + imageRun.addPicture(Files.newInputStream(imagePath), XWPFDocument.PICTURE_TYPE_PNG, imagePath.getFileName().toString(), Units.toEMU(50), Units.toEMU(50)); + + XWPFParagraph sectionTitle = document.createParagraph(); + XWPFRun sectionTRun = sectionTitle.createRun(); + sectionTRun.setText("What makes a good API?"); + sectionTRun.setColor("00CC44"); + sectionTRun.setBold(true); + sectionTRun.setFontFamily("Courier"); + + XWPFParagraph para1 = document.createParagraph(); + para1.setAlignment(ParagraphAlignment.BOTH); + String string1 = convertTextFileToString(paragraph1); + XWPFRun para1Run = para1.createRun(); + para1Run.setText(string1); + + XWPFParagraph para2 = document.createParagraph(); + para2.setAlignment(ParagraphAlignment.RIGHT); + String string2 = convertTextFileToString(paragraph2); + XWPFRun para2Run = para2.createRun(); + para2Run.setText(string2); + para2Run.setItalic(true); + + XWPFParagraph para3 = document.createParagraph(); + para3.setAlignment(ParagraphAlignment.LEFT); + String string3 = convertTextFileToString(paragraph3); + XWPFRun para3Run = para3.createRun(); + para3Run.setText(string3); + + FileOutputStream out = new FileOutputStream(output); + document.write(out); + out.close(); + document.close(); + } + + public String convertTextFileToString(String fileName) { + try (Stream stream = Files.lines(Paths.get(ClassLoader.getSystemResource(fileName).toURI()))) { + return stream.collect(Collectors.joining(" ")); + } catch (IOException | URISyntaxException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/apache-poi/src/main/resources/logo-leaf.png b/apache-poi/src/main/resources/logo-leaf.png new file mode 100644 index 0000000000..395a2ccf79 Binary files /dev/null and b/apache-poi/src/main/resources/logo-leaf.png differ diff --git a/apache-poi/src/main/resources/poi-word-para1.txt b/apache-poi/src/main/resources/poi-word-para1.txt new file mode 100644 index 0000000000..97ef5fdc9d --- /dev/null +++ b/apache-poi/src/main/resources/poi-word-para1.txt @@ -0,0 +1 @@ +I published the first REST with Spring tutorials over four years ago, and after 300,000 readers and over a thousand personal questions over email, I am finally realizing that there is a huge education gap in the ecosystem. \ No newline at end of file diff --git a/apache-poi/src/main/resources/poi-word-para2.txt b/apache-poi/src/main/resources/poi-word-para2.txt new file mode 100644 index 0000000000..3d37aa58a4 --- /dev/null +++ b/apache-poi/src/main/resources/poi-word-para2.txt @@ -0,0 +1,2 @@ +I had to learn what makes a good API is the hard way, and I couldn’t find anything out there to help me speed things up. +It took me 3 years, more than 10 production grade APIs built from scratch and a decent number of mistakes to get to a mature understanding of how an API should really be done well. \ No newline at end of file diff --git a/apache-poi/src/main/resources/poi-word-para3.txt b/apache-poi/src/main/resources/poi-word-para3.txt new file mode 100644 index 0000000000..1ecf329e68 --- /dev/null +++ b/apache-poi/src/main/resources/poi-word-para3.txt @@ -0,0 +1,3 @@ +Sure, there were tutorials and random code samples out there, but nothing coherent. +Nothing start to finish to act as a guide through the challenges of building a mature API with Spring. +The educational gap between beginner and the API professional remains vast. \ No newline at end of file diff --git a/apache-poi/src/test/java/com/baeldung/poi/word/WordTest.java b/apache-poi/src/test/java/com/baeldung/poi/word/WordTest.java new file mode 100644 index 0000000000..bc1011a03a --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/word/WordTest.java @@ -0,0 +1,47 @@ +package com.baeldung.poi.word; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +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.junit.BeforeClass; +import org.junit.Test; + +public class WordTest { + static WordDocument wordDocument; + + @BeforeClass + public static void generateMSWordFile() throws Exception { + WordTest.wordDocument = new WordDocument(); + wordDocument.handleSimpleDoc(); + } + + @Test + public void whenParsingOutputDocument_thenCorrect() throws Exception { + Path msWordPath = Paths.get(WordDocument.output); + XWPFDocument document = new XWPFDocument(Files.newInputStream(msWordPath)); + List paragraphs = document.getParagraphs(); + document.close(); + + XWPFParagraph title = paragraphs.get(0); + XWPFRun titleRun = title.getRuns().get(0); + assertEquals("Build Your REST API with Spring", title.getText()); + assertEquals("009933", titleRun.getColor()); + assertTrue(titleRun.isBold()); + assertEquals("Courier", titleRun.getFontFamily()); + assertEquals(20, titleRun.getFontSize()); + + assertEquals("from HTTP fundamentals to API Mastery", paragraphs.get(1).getText()); + assertEquals("What makes a good API?", paragraphs.get(3).getText()); + assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph1), paragraphs.get(4).getText()); + assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph2), paragraphs.get(5).getText()); + assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph3), paragraphs.get(6).getText()); + } +}