List of usage examples for org.apache.poi.xwpf.usermodel IBodyElement getElementType
BodyElementType getElementType();
From source file:com.example.office.DOCDocumentParse.java
@SuppressWarnings("unused") private File doc2docxOld(File docFile) { String docxFilePath = docFile.getPath() + "x"; File docxFile = new File(docxFilePath); if (!docxFile.exists()) { XWPFDocument document = null;/*from w ww.ja v a 2s . co m*/ try (InputStream ins = new FileInputStream(docxFile); OutputStream out = new FileOutputStream(docxFile);) { Document doc = new Document(docFile.getPath()); doc.save(docxFile.getPath()); document = new XWPFDocument(ins); // document.removeBodyElement(0) List<IBodyElement> elements = document.getBodyElements(); IBodyElement element = elements.get(elements.size() - 1); if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element.getElementType().name())) { XWPFParagraph xp = ((XWPFParagraph) element); String text = xp.getText(); if (StringUtils.isNotBlank(text)) { if (text.contains("Evaluation") && text.contains("Aspose")) { document.removeBodyElement(elements.size() - 1); } } } IBodyElement element0 = elements.get(0); if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element0.getElementType().name())) { XWPFParagraph xp = ((XWPFParagraph) element0); String text = xp.getText(); if (StringUtils.isNotBlank(text)) { if (text.contains("Evaluation") && text.contains("Aspose")) { document.removeBodyElement(0); } } } document.write(out); } catch (Exception e) { LogUtils.writeWarnExceptionLog(log, e); } finally { try { if (document != null) document.close(); } catch (IOException e) { LogUtils.writeDebugExceptionLog(log, e); } } } return docxFile; }
From source file:com.example.office.DOCDocumentParse.java
private void deleteAsposeInfo(File docxFile) { XWPFDocument document = null;/*from ww w .java 2s . c o m*/ try (InputStream ins = new FileInputStream(docxFile); OutputStream out = new FileOutputStream(docxFile);) { document = new XWPFDocument(ins); List<IBodyElement> elements = document.getBodyElements(); IBodyElement element = elements.get(elements.size() - 1); if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element.getElementType().name())) { XWPFParagraph xp = ((XWPFParagraph) element); String text = xp.getText(); if (StringUtils.isNotBlank(text)) { if (text.contains("Evaluation") && text.contains("Aspose")) { document.removeBodyElement(elements.size() - 1); } } } IBodyElement element0 = elements.get(0); if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element0.getElementType().name())) { XWPFParagraph xp = ((XWPFParagraph) element0); String text = xp.getText(); if (StringUtils.isNotBlank(text)) { if (text.contains("Evaluation") && text.contains("Aspose")) { document.removeBodyElement(0); } } } document.write(out); } catch (Exception e) { LogUtils.writeWarnExceptionLog(log, e); } finally { if (document != null) { try { document.close(); } catch (IOException e) { LogUtils.writeDebugExceptionLog(log, e); } } } }
From source file:com.pdf.GetPdf.java
public static void docConvert(Document document, String url, String type) throws IOException, DocumentException { WordExtractor we;/*from w ww.ja v a2s.c om*/ if (type.equals("doc")) { HWPFDocument wordDoc = new HWPFDocument(new URL(url).openStream()); we = new WordExtractor(wordDoc); String[] paragraphs = we.getParagraphText(); for (int i = 0; i < paragraphs.length; i++) { paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", ""); document.add(new Paragraph(paragraphs[i])); } } else { XWPFDocument wordDoc = new XWPFDocument(new URL(url).openStream()); List<IBodyElement> contents = wordDoc.getBodyElements(); for (IBodyElement content : contents) { if (content.getElementType() == BodyElementType.PARAGRAPH) { List<XWPFParagraph> paras = content.getBody().getParagraphs(); for (XWPFParagraph para : paras) { document.add(new Paragraph(para.getParagraphText())); } } else if (content.getElementType() == BodyElementType.TABLE) { List<XWPFTable> tables = content.getBody().getTables(); for (XWPFTable table : tables) { List<XWPFTableRow> rows = table.getRows(); for (XWPFTableRow row : rows) { List<XWPFTableCell> tablecells = row.getTableCells(); } } } } } }
From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java
License:Open Source License
private static void replaceAllBodyElements(List<IBodyElement> bodyElements, String placeHolder, String replaceText) {/* w ww . ja v a 2s .c om*/ for (IBodyElement bodyElement : bodyElements) { if (bodyElement.getElementType().compareTo(BodyElementType.PARAGRAPH) == 0) replaceParagraph((XWPFParagraph) bodyElement, placeHolder, replaceText); } }
From source file:de.iisys.schub.processMining.similarity.parsing.DocxParser.java
License:Apache License
/** * Parses the .docx Word file. After that, you can get its full text and title. * And also its chapters and their names and texts. *///from w w w .j a v a2 s . c om public void parseDocxAndChapters() { if (theDoc != null) { this.chapters = new ArrayList<List<IBodyElement>>(); this.chapterTexts = new ArrayList<String>(); this.chapterHeadlines = new ArrayList<String>(); this.chapterHeadlines.add(0, "0:"); StringBuffer buf = new StringBuffer(); List<IBodyElement> tempChapter = new ArrayList<IBodyElement>(); StringBuffer chapBuf = new StringBuffer(); String tempText = ""; int chapterCount = 1; for (IBodyElement el : theDoc.getBodyElements()) { switch (el.getElementType().toString().toLowerCase()) { case ("paragraph"): tempText = ((XWPFParagraph) el).getText(); if (CHAPTER_STYLES.contains(((XWPFParagraph) el).getStyle())) { // if first chapter & first paragraph & headline if (chapterCount == 1 && chapBuf.length() == 0) { // add headline for first chapter chapterHeadlines.add(0, (chapterCount) + ": '" + tempText + "'"); } else { // save former chapter: chapters.add(tempChapter); chapterTexts.add(chapBuf.toString()); buf.append(chapBuf); // prepare for next chapter: tempChapter = new ArrayList<IBodyElement>(); chapBuf = new StringBuffer(); // add headline for next/following chapter chapterHeadlines.add((chapterCount) + ": '" + tempText + "'"); } chapterCount++; } break; case ("table"): tempText = ((XWPFTable) el).getText(); break; default: tempText = ""; } chapBuf.append(tempText + " "); tempChapter.add(el); } // add last chapter: chapters.add(tempChapter); chapterTexts.add(chapBuf.toString()); this.fullText = buf.toString(); } }
From source file:File.DOCX.ReadDocx.java
public void ReadTable(String path, String filename) { try {/* www .ja v a 2 s. c o m*/ FileInputStream fis = new FileInputStream(path + filename + ".docx"); XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis)); Iterator<IBodyElement> bodyElementIterator = xdoc.getBodyElementsIterator(); while (bodyElementIterator.hasNext()) { IBodyElement element = bodyElementIterator.next(); if ("TABLE".equalsIgnoreCase(element.getElementType().name())) { List<XWPFTable> tableList = element.getBody().getTables(); for (XWPFTable table : tableList) { System.out.println("Total Number of Rows of Table:" + table.getNumberOfRows()); System.out.println(table.getText()); } } } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:fr.opensagres.poi.xwpf.converter.core.styles.A.java
License:Open Source License
@Test public void testParagraphStyles() throws Exception { // 1) Load docx with Apache POI XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("DocxStructures.docx")); // Create styles engine XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document); // Loop for each paragraph List<IBodyElement> elements = document.getBodyElements(); for (IBodyElement element : elements) { if (element.getElementType() == BodyElementType.PARAGRAPH) { testParagraph((XWPFParagraph) element, stylesDocument); }/* w ww . j av a2s. com*/ } }
From source file:fr.opensagres.poi.xwpf.converter.core.styles.FontStylesBasedOnTestCase.java
License:Open Source License
@Test public void testParagraphStyles() throws Exception { // 1) Load docx with Apache POI XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("TestFontStylesBasedOn.docx")); // Create styles engine XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document); // Loop for each paragraph List<IBodyElement> elements = document.getBodyElements(); for (IBodyElement element : elements) { if (element.getElementType() == BodyElementType.PARAGRAPH) { testParagraph((XWPFParagraph) element, stylesDocument); }/*w w w . j a va 2 s .c om*/ } }
From source file:fr.opensagres.poi.xwpf.converter.core.styles.run.FontSizeDocDefaultsTestCase.java
License:Open Source License
private void internalTest(Float size, String docName) throws Exception { XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream(docName)); XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document); List<IBodyElement> elements = document.getBodyElements(); boolean ran = false; for (IBodyElement element : elements) { if (element.getElementType() == BodyElementType.PARAGRAPH) { for (XWPFRun docxRun : ((XWPFParagraph) element).getRuns()) { Object sizeFromStyle = stylesDocument.getFontSize(docxRun); ran = true;//from w ww . j a va2 s . c o m assertEquals(sizeFromStyle, size); } } } assertTrue(ran); }
From source file:fr.opensagres.poi.xwpf.converter.core.styles.TableCellVerticalAlignmentTestCase.java
License:Open Source License
@Test public void testParagraphStyles() throws Exception { // 1) Load docx with Apache POI XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("TableCellVerticalAlignment.docx")); // Create styles engine XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document); // Loop for each paragraph List<IBodyElement> elements = document.getBodyElements(); for (IBodyElement element : elements) { if (element.getElementType() == BodyElementType.TABLE) { testTable((XWPFTable) element, stylesDocument); }//www . java 2 s . com } }