From 4f1ac2e7760bdfc4403a5e90739101585b93b73b Mon Sep 17 00:00:00 2001 From: nguyennamthai Date: Fri, 23 Dec 2016 23:38:23 +0700 Subject: [PATCH] Apache POI Support for MS Word (#908) * Initial commit for Apache POI support for MS Word * Relocate the POI Word project * Add Apache POI to the repo --- apache-poi/.gitignore | 1 + apache-poi/pom.xml | 41 ++++++++ .../com/baeldung/poi/word/WordDocument.java | 95 ++++++++++++++++++ apache-poi/src/main/resources/logo-leaf.png | Bin 0 -> 782 bytes .../src/main/resources/poi-word-para1.txt | 1 + .../src/main/resources/poi-word-para2.txt | 2 + .../src/main/resources/poi-word-para3.txt | 3 + .../java/com/baeldung/poi/word/WordTest.java | 47 +++++++++ 8 files changed, 190 insertions(+) create mode 100644 apache-poi/.gitignore create mode 100644 apache-poi/pom.xml create mode 100644 apache-poi/src/main/java/com/baeldung/poi/word/WordDocument.java create mode 100644 apache-poi/src/main/resources/logo-leaf.png create mode 100644 apache-poi/src/main/resources/poi-word-para1.txt create mode 100644 apache-poi/src/main/resources/poi-word-para2.txt create mode 100644 apache-poi/src/main/resources/poi-word-para3.txt create mode 100644 apache-poi/src/test/java/com/baeldung/poi/word/WordTest.java 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 0000000000000000000000000000000000000000..395a2ccf796b1813d766d4766a7cf9bbb71d377f GIT binary patch literal 782 zcmV+p1M&QcP)*JtmV#+AnZ0eu0AyGckMu^9YgCCAsnik4ad*k*6(V zwxk(SFVQRX60KsgBt>$rWHW`FDG5)8i4t!kpO_@cHWrf8ByHOxWyU2fnaW-~^n`;z z09x7-sb##_CP|tt9HA;AVK_)cFSc;D#5;~-(Z#}k%Amw(94kMwK_)mVSe(AAXdN-| zK!C_f1tn}FwYXEm1~ZVs8#NfbQ6+qTE zdanz}`S!*~M#Nv_$u6W86(j&ir*GVnbQJ=?Q62Y{-MGsbmWS zvAFAk4%mT5RHvNbq3A;bU19;2`jBMy;cgrD@{qW@aJLXP`AR{iwA+QaM>MYD@Bh;- zNTSNo_{4-+vI#AyLcmcZNvXI)K;u9iJS@r%F%umm6b>(ZWLT0qawI@Pa>!+CAkyvd ziaP>@q-;G$iwGGf)VbJxGGt<(BmLItJqo@3318%}=^u#V2S=#XM?_3a(;K)vE=j`7 z5f>uQ_>#MaXB(Bc*8Qw((TOx1l5ke(U~`8m7aZ!Sr+U~bg~LK7*n0e$!r>s2&GB1a z<0wtME3G}Nx8t|KAw^PHhERh-&+?1o31v~yIohKp`o6!wK2(4y)<5F$J6ucHR+v|B=cq4W}%HH`D}yR$lI= zuMEgluQ=vRU6+g}w2n35LHK1Yr-r5Bq4;ED 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()); + } +}