|
| 1 | +package com.baeldung.poi.word; |
| 2 | + |
| 3 | +import java.io.FileOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.URISyntaxException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.Paths; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import org.apache.poi.util.Units; |
| 13 | +import org.apache.poi.xwpf.usermodel.ParagraphAlignment; |
| 14 | +import org.apache.poi.xwpf.usermodel.UnderlinePatterns; |
| 15 | +import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| 16 | +import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
| 17 | +import org.apache.poi.xwpf.usermodel.XWPFRun; |
| 18 | + |
| 19 | +public class WordDocument { |
| 20 | + public static String logo = "logo-leaf.png"; |
| 21 | + public static String paragraph1 = "poi-word-para1.txt"; |
| 22 | + public static String paragraph2 = "poi-word-para2.txt"; |
| 23 | + public static String paragraph3 = "poi-word-para3.txt"; |
| 24 | + public static String output = "rest-with-spring.docx"; |
| 25 | + |
| 26 | + public void handleSimpleDoc() throws Exception { |
| 27 | + XWPFDocument document = new XWPFDocument(); |
| 28 | + |
| 29 | + XWPFParagraph title = document.createParagraph(); |
| 30 | + title.setAlignment(ParagraphAlignment.CENTER); |
| 31 | + XWPFRun titleRun = title.createRun(); |
| 32 | + titleRun.setText("Build Your REST API with Spring"); |
| 33 | + titleRun.setColor("009933"); |
| 34 | + titleRun.setBold(true); |
| 35 | + titleRun.setFontFamily("Courier"); |
| 36 | + titleRun.setFontSize(20); |
| 37 | + |
| 38 | + XWPFParagraph subTitle = document.createParagraph(); |
| 39 | + subTitle.setAlignment(ParagraphAlignment.CENTER); |
| 40 | + XWPFRun subTitleRun = subTitle.createRun(); |
| 41 | + subTitleRun.setText("from HTTP fundamentals to API Mastery"); |
| 42 | + subTitleRun.setColor("00CC44"); |
| 43 | + subTitleRun.setFontFamily("Courier"); |
| 44 | + subTitleRun.setFontSize(16); |
| 45 | + subTitleRun.setTextPosition(20); |
| 46 | + subTitleRun.setUnderline(UnderlinePatterns.DOT_DOT_DASH); |
| 47 | + |
| 48 | + XWPFParagraph image = document.createParagraph(); |
| 49 | + image.setAlignment(ParagraphAlignment.CENTER); |
| 50 | + XWPFRun imageRun = image.createRun(); |
| 51 | + imageRun.setTextPosition(20); |
| 52 | + Path imagePath = Paths.get(ClassLoader.getSystemResource(logo).toURI()); |
| 53 | + imageRun.addPicture(Files.newInputStream(imagePath), XWPFDocument.PICTURE_TYPE_PNG, imagePath.getFileName().toString(), Units.toEMU(50), Units.toEMU(50)); |
| 54 | + |
| 55 | + XWPFParagraph sectionTitle = document.createParagraph(); |
| 56 | + XWPFRun sectionTRun = sectionTitle.createRun(); |
| 57 | + sectionTRun.setText("What makes a good API?"); |
| 58 | + sectionTRun.setColor("00CC44"); |
| 59 | + sectionTRun.setBold(true); |
| 60 | + sectionTRun.setFontFamily("Courier"); |
| 61 | + |
| 62 | + XWPFParagraph para1 = document.createParagraph(); |
| 63 | + para1.setAlignment(ParagraphAlignment.BOTH); |
| 64 | + String string1 = convertTextFileToString(paragraph1); |
| 65 | + XWPFRun para1Run = para1.createRun(); |
| 66 | + para1Run.setText(string1); |
| 67 | + |
| 68 | + XWPFParagraph para2 = document.createParagraph(); |
| 69 | + para2.setAlignment(ParagraphAlignment.RIGHT); |
| 70 | + String string2 = convertTextFileToString(paragraph2); |
| 71 | + XWPFRun para2Run = para2.createRun(); |
| 72 | + para2Run.setText(string2); |
| 73 | + para2Run.setItalic(true); |
| 74 | + |
| 75 | + XWPFParagraph para3 = document.createParagraph(); |
| 76 | + para3.setAlignment(ParagraphAlignment.LEFT); |
| 77 | + String string3 = convertTextFileToString(paragraph3); |
| 78 | + XWPFRun para3Run = para3.createRun(); |
| 79 | + para3Run.setText(string3); |
| 80 | + |
| 81 | + FileOutputStream out = new FileOutputStream(output); |
| 82 | + document.write(out); |
| 83 | + out.close(); |
| 84 | + document.close(); |
| 85 | + } |
| 86 | + |
| 87 | + public String convertTextFileToString(String fileName) { |
| 88 | + try (Stream<String> stream = Files.lines(Paths.get(ClassLoader.getSystemResource(fileName).toURI()))) { |
| 89 | + return stream.collect(Collectors.joining(" ")); |
| 90 | + } catch (IOException | URISyntaxException e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + return null; |
| 94 | + } |
| 95 | +} |
0 commit comments