Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFDocument#close()
The following examples show how to use
org.apache.poi.xwpf.usermodel.XWPFDocument#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TocTest.java From poi-tl with Apache License 2.0 | 7 votes |
public static void main(String[] args) throws FileNotFoundException, IOException { XWPFDocument doc = new XWPFDocument(new FileInputStream("toc.docx")); XWPFStyles styles = doc.getStyles(); System.out.println(styles); XWPFParagraph p = doc.createParagraph(); XWPFRun run = p.createRun(); run.setText("Heading 1"); p.getCTP().addNewPPr(); p.getCTP().getPPr().addNewPStyle(); p.setStyle(HEADING1); XWPFParagraph tocPara = doc.createParagraph(); CTP ctP = tocPara.getCTP(); CTSimpleField toc = ctP.addNewFldSimple(); toc.setInstr("TOC \\h"); toc.setDirty(STOnOff.TRUE); System.out.println(doc.isEnforcedUpdateFields()); // doc.enforceUpdateFields(); // doc.createTOC(); doc.write(new FileOutputStream("CreateWordBookmark.docx")); doc.close(); }
Example 2
Source File: WatermarkWordTests.java From kbase-doc with Apache License 2.0 | 6 votes |
@Test public void testDocx2() throws IOException { String filepath = "E:\\ConvertTester\\docx\\NVR5X-I人脸比对配置-ekozhan.docx"; XWPFDocument doc = new XWPFDocument(new FileInputStream(filepath)); XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(doc); policy.createWatermark("ekozhan123"); doc.write(new FileOutputStream("E:\\ConvertTester\\docx\\NVR5X-I人脸比对配置-ekozhan-11.docx")); doc.close(); }
Example 3
Source File: Issue313.java From poi-tl with Apache License 2.0 | 6 votes |
public void excelRender(String temppath) throws Exception { TextRenderData[] arr = new TextRenderData[10]; arr[1] = new TextRenderData("FFFFFF", "姓名"); TableStyle headerStyle = new TableStyle(); headerStyle.setBackgroundColor("80C687");// 表的标题背景 RowRenderData header = RowRenderData.build(new TextRenderData("FFFFFF", "姓名"), new TextRenderData("FFFFFF", "学历"), new TextRenderData("FFFFFF", "aaf"));// 表头 header.setRowStyle(headerStyle); RowRenderData row0 = RowRenderData.build("张三", "研究生", "");// 每列的数据 RowRenderData row1 = RowRenderData.build("李四", "博士", "aa"); RowRenderData row2 = RowRenderData.build("王五", "博士后", ""); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("excel_first", new MiniTableRenderData(header, Arrays.asList(row0, row1))); map.put("excel_second", new MiniTableRenderData(header, Arrays.asList(row2))); XWPFTemplate template = XWPFTemplate.compile(temppath).render(map); XWPFDocument document = XWPFTestSupport.readNewDocument(template); List<XWPFTable> tables = document.getTables(); assertEquals(tables.size(), 2); assertEquals(tables.get(0).getRows().size(), 3); assertEquals(tables.get(1).getRows().size(), 2); document.close(); new File(temppath).deleteOnExit(); }
Example 4
Source File: ELModeTest.java From poi-tl with Apache License 2.0 | 6 votes |
@Test public void testPoitlELMode() throws Exception { model.getDetail().setDesc(null); // poi_tl_mode 当变量不存在时,会友好的认为变量是null,不会抛出异常 XWPFTemplate template = XWPFTemplate.compile(resource).render(model); XWPFDocument document = XWPFTestSupport.readNewDocument(template); XWPFParagraph paragraph = document.getParagraphArray(0); assertEquals(paragraph.getText(), "Sayi"); paragraph = document.getParagraphArray(1); assertEquals(paragraph.getText(), "卅一"); paragraph = document.getParagraphArray(3); assertEquals(paragraph.getText(), ""); paragraph = document.getParagraphArray(4); assertEquals(paragraph.getText(), ""); assertEquals(document.getAllPictures().size(), 1); assertEquals(document.getTables().size(), 1); document.close(); }
Example 5
Source File: ELModeTest.java From poi-tl with Apache License 2.0 | 6 votes |
@Test public void testSpringELMode() throws Exception { // Spring EL 无法容忍变量不存在,直接抛出异常,表达式计算引擎为Spring Expression Language Configure config = Configure.newBuilder().setElMode(ELMode.SPEL_MODE).build(); XWPFTemplate template = XWPFTemplate.compile(resource, config).render(model); XWPFDocument document = XWPFTestSupport.readNewDocument(template); XWPFParagraph paragraph = document.getParagraphArray(0); assertEquals(paragraph.getText(), "Sayi"); paragraph = document.getParagraphArray(1); assertEquals(paragraph.getText(), "卅一"); paragraph = document.getParagraphArray(3); assertEquals(paragraph.getText(), "2018-10-01"); paragraph = document.getParagraphArray(4); assertEquals(paragraph.getText(), "http://www.deepoove.com"); assertEquals(document.getAllPictures().size(), 1); assertEquals(document.getTables().size(), 1); document.close(); }
Example 6
Source File: WordIntegrationTest.java From tutorials with MIT License | 6 votes |
@Test public void whenParsingOutputDocument_thenCorrect() throws Exception { Path msWordPath = Paths.get(WordDocument.output); XWPFDocument document = new XWPFDocument(Files.newInputStream(msWordPath)); List<XWPFParagraph> 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()); }
Example 7
Source File: PDF2WordExample.java From tutorials with MIT License | 6 votes |
private static void generateDocFromPDF(String filename) throws IOException { XWPFDocument doc = new XWPFDocument(); String pdf = filename; PdfReader reader = new PdfReader(pdf); PdfReaderContentParser parser = new PdfReaderContentParser(reader); for (int i = 1; i <= reader.getNumberOfPages(); i++) { TextExtractionStrategy strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); String text = strategy.getResultantText(); XWPFParagraph p = doc.createParagraph(); XWPFRun run = p.createRun(); run.setText(text); run.addBreak(BreakType.PAGE); } FileOutputStream out = new FileOutputStream("src/output/pdf.docx"); doc.write(out); out.close(); reader.close(); doc.close(); }
Example 8
Source File: SpELTest.java From poi-tl with Apache License 2.0 | 5 votes |
@Test public void testSpELTemplate() throws IOException { Configure config = Configure.newBuilder().setElMode(ELMode.SPEL_MODE).setSpELFunction(spELFunction).build(); XWPFTemplate template = XWPFTemplate.compile("src/test/resources/template/config_spel.docx", config).render(data); XWPFDocument document = XWPFTestSupport.readNewDocument(template); XWPFParagraph paragraph = document.getParagraphArray(0); assertEquals(paragraph.getText(), "poi-tl"); paragraph = document.getParagraphArray(1); assertEquals(paragraph.getText(), "lowCase:poi-tlUpcase:POI-TL"); paragraph = document.getParagraphArray(2); assertEquals(paragraph.getText(), "这个字段为空"); paragraph = document.getParagraphArray(3); assertEquals(paragraph.getText(), "男"); paragraph = document.getParagraphArray(4); assertEquals(paragraph.getText(), "2019-05-20 22:14:10"); paragraph = document.getParagraphArray(5); assertEquals(paragraph.getText(), "2019-05-20 10:14"); paragraph = document.getParagraphArray(6); assertEquals(paragraph.getText(), "88880000"); paragraph = document.getParagraphArray(7); assertEquals(paragraph.getText(), "8888万元"); paragraph = document.getParagraphArray(8); assertEquals(paragraph.getText(), "阿黄"); paragraph = document.getParagraphArray(9); assertEquals(paragraph.getText(), "6"); paragraph = document.getParagraphArray(10); assertEquals(paragraph.getText(), "阿蓝"); paragraph = document.getParagraphArray(11); assertEquals(paragraph.getText(), "ElFunction"); document.close(); }
Example 9
Source File: XWPFParagraphWrapperTest.java From poi-tl with Apache License 2.0 | 4 votes |
@Test public void testWrapper() throws FileNotFoundException, IOException { XWPFDocument doc = new XWPFDocument(); XWPFParagraph paragraph = doc.createParagraph(); XWPFParagraphWrapper wrapper = new XWPFParagraphWrapper(paragraph); XWPFRun hyperRun = wrapper.insertNewHyperLinkRun(0, "http:deepoove.com"); hyperRun.setText("Deepoove0"); XWPFRun newRun = wrapper.insertNewRun(0); newRun.setText("website0"); newRun = wrapper.insertNewRun(0); newRun.setText("website1"); newRun = wrapper.insertNewRun(0); newRun.setText("website2"); hyperRun = wrapper.insertNewHyperLinkRun(0, "http:deepoove.com"); hyperRun.setText("Deepoove1"); XWPFFieldRun fieldRun = wrapper.insertNewField(0); CTSimpleField ctField = fieldRun.getCTField(); ctField.setInstr(" CREATEDATE \\* MERGEFORMAT "); fieldRun.setText("2019"); assertEquals(paragraph.getRuns().size(), 6); for (int i = 6; i >= 0; i--) { wrapper.removeRun(i); } assertEquals(paragraph.getRuns().size(), 0); doc.close(); }
Example 10
Source File: Issue157.java From poi-tl with Apache License 2.0 | 4 votes |
@Test public void testNullMerge() throws Exception { Map<String, Object> params = new HashMap<String, Object>(); params.put("title", "testTitle"); // 不加入这一段, 虽然 {{+teachers}} 设置值了,但是仍然不会渲染 /* * List<Teacher> stuList = new ArrayList<Teacher>(); Teacher p1 = new Teacher(); * p1.setName("test1"); stuList.add(p1); * * p1 = new Teacher(); p1.setName("test2"); stuList.add(p1); * * params.put("students", new DocxRenderData(new * File("src/test/resources/issue/test_teacher.docx"), stuList)); */ List<Teacher> teacherList = new ArrayList<Teacher>(); Teacher t1 = new Teacher(); t1.setName("t1"); t1.setAge(18); teacherList.add(t1); t1 = new Teacher(); t1.setName("t2"); t1.setAge(36); teacherList.add(t1); params.put("teachers", new DocxRenderData(new File("src/test/resources/issue/157_MERGE.docx"), teacherList)); XWPFTemplate doc = XWPFTemplate.compile("src/test/resources/issue/157.docx"); doc.render(params); XWPFDocument document = XWPFTestSupport.readNewDocument(doc); XWPFParagraph paragraph = document.getParagraphArray(0); assertEquals(paragraph.getText(), "testTitle"); paragraph = document.getParagraphArray(1); assertEquals(paragraph.getText(), ""); paragraph = document.getParagraphArray(2); assertEquals(paragraph.getText(), "老师:t1\n" + "年龄:18"); paragraph = document.getParagraphArray(3); assertEquals(paragraph.getText(), "老师:t2\n" + "年龄:36"); document.close(); }
Example 11
Source File: Issue111.java From poi-tl with Apache License 2.0 | 4 votes |
@SuppressWarnings("serial") @Test public void testCRBR() throws Exception { StringBuffer sb = new StringBuffer(); for (int i = 0; i < 5; i++) { String info = "姓名{{name" + i + "}},年龄:{{age" + i + "}}。"; sb.append(info).append("\n"); } final String Info = sb.toString(); XWPFTemplate templateRule = XWPFTemplate.compile("src/test/resources/issue/111.docx") .render(new HashMap<String, Object>() { { put("title", Info); } }); templateRule.writeToFile("out_111temp.docx"); XWPFTemplate template = XWPFTemplate.compile("out_111temp.docx").render(new HashMap<String, Object>() { { for (int j = 0; j < 5; j++) { put("name" + j, "测试姓名" + j); put("age" + j, "测试年龄" + j); } } }); // template.writeToFile("out_issue_111.docx"); XWPFDocument document = XWPFTestSupport.readNewDocument(template); XWPFParagraph paragraph = document.getParagraphArray(0); assertEquals(paragraph.getText(), "姓名测试姓名0,年龄:测试年龄0。\n" + "姓名测试姓名1,年龄:测试年龄1。\n" + "姓名测试姓名2,年龄:测试年龄2。\n" + "姓名测试姓名3,年龄:测试年龄3。\n" + "姓名测试姓名4,年龄:测试年龄4。\n" + "姓名测试姓名0,年龄:测试年龄0。\n" + "姓名测试姓名1,年龄:测试年龄1。\n" + "姓名测试姓名2,年龄:测试年龄2。\n" + "姓名测试姓名3,年龄:测试年龄3。\n" + "姓名测试姓名4,年龄:测试年龄4。\n"); document.close(); new File("out_111temp.docx").deleteOnExit(); }