/** * get the paragraph with the CTP class p * * @param p * @return the paragraph with the CTP class p */ @Override public XWPFParagraph getParagraph(CTP p) { for (int i = 0; i < getParagraphs().size(); i++) { if (getParagraphs().get(i).getCTP() == p) { return getParagraphs().get(i); } } return null; }
XWPFDocument doc = new XWPFDocument(OPCPackage.open("input.docx")); for (XWPFParagraph p : doc.getParagraphs()) { List<XWPFRun> runs = p.getRuns(); if (runs != null) { for (XWPFRun r : runs) { String text = r.getText(0); if (text != null && text.contains("needle")) { text = text.replace("needle", "haystack"); r.setText(text, 0); } } } } for (XWPFTable tbl : doc.getTables()) { for (XWPFTableRow row : tbl.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph p : cell.getParagraphs()) { for (XWPFRun r : p.getRuns()) { String text = r.getText(0); if (text.contains("needle")) { text = text.replace("needle", "haystack"); r.setText(text); } } } } } } doc.write(new FileOutputStream("output.docx"));
@Override public List<ElementTemplate> visitDocument(XWPFDocument doc) { if (null == doc) return null; this.eleTemplates = new ArrayList<ElementTemplate>(); visitParagraphs(doc.getParagraphs()); visitTables(doc.getTables()); visitHeaders(doc.getHeaderList()); visitFooters(doc.getFooterList()); return eleTemplates; }
XWPFDocument doc = new XWPFDocument(fis); List<XWPFParagraph> paragraphs = doc.getParagraphs(); for (XWPFParagraph p : paragraphs) { ... do something here }
/** * get the paragraph with the CTP class p * * @param p * @return the paragraph with the CTP class p */ public XWPFParagraph getParagraph(CTP p) { for (int i = 0; i < getParagraphs().size(); i++) { if (getParagraphs().get(i).getCTP() == p) { return getParagraphs().get(i); } } return null; }
/** * get the paragraph with the CTP class p * * @param p * @return the paragraph with the CTP class p */ @Override public XWPFParagraph getParagraph(CTP p) { for (int i = 0; i < getParagraphs().size(); i++) { if (getParagraphs().get(i).getCTP() == p) { return getParagraphs().get(i); } } return null; }
public void readFontSizeFromDocx() throws IOException { InputStream is = this.getClass().getClassLoader().getResourceAsStream("templates/ExampleFontSize.docx"); XWPFDocument doc = new XWPFDocument(is); for (XWPFParagraph paragraph : doc.getParagraphs()) { for (XWPFRun run : paragraph.getRuns()) { System.out.println(run.getFontSize()); } } }
XWPFDocument doc = new XWPFDocument(OPCPackage.open("myfile.docx")); for (XWPFParagraph paragraph : doc.getParagraphs()) { int pos = 0; for (XWPFRun run : paragraph.getRuns()) { for (character c : run.text().toCharArray()) { System.out.println("The character at " + pos + " is " + c); pos++; } } }
private void replaceInWholeDocument(XWPFDocument document, Map<String, String> map) { List<XWPFParagraph> paragraphs = document.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { if (StringUtils.isEmpty(paragraph.getText()) == false) { replaceInParagraph(paragraph, map); } } }
import java.io.File; import java.io.FileInputStream; import java.util.List; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; public void readDocxFile() { try { File file = new File("C:/NetBeans Output/documentx.docx"); FileInputStream fis = new FileInputStream(file.getAbsolutePath()); XWPFDocument document = new XWPFDocument(fis); List<XWPFParagraph> paragraphs = document.getParagraphs(); for (XWPFParagraph para : paragraphs) { System.out.println(para.getText()); } fis.close(); } catch (Exception e) { e.printStackTrace(); } }
@Override public Collection<Requirement> parse(final URI uri, final ReqDeclTagConfig declTagConfig, final Map<String, Object> options) throws ReqParserException { try (final InputStream in = Files.newInputStream(Paths.get(uri)); final XWPFDocument document = new XWPFDocument(in)) { final Set<Requirement> declaredRequirements = new HashSet<>(); final Predicate<String> textContainsRequirementPredicate = Pattern.compile(declTagConfig.getCompleteRegex()).asPredicate(); LOGGER.debug("Retrieving all paragraphs contained into the docx file -> {}", uri); document.getParagraphs().stream().map(XWPFParagraph::getText).filter(textContainsRequirementPredicate) .forEach(text -> declaredRequirements.addAll(getRequirementsFromString(declTagConfig, text))); return declaredRequirements; } catch (IOException e) { String errMsg = "Error while reading the docx file : " + e.getMessage(); LOGGER.error(errMsg, e); throw new ReqParserException(errMsg, e); } }
@Override public void read(InputStream inputStream, WordReaderCallBack callBack) throws Exception { XWPFDocument document = new XWPFDocument(inputStream); document.getParagraphs().forEach(callBack::onParagraph); document.getTables().forEach(callBack::onTable); callBack.done(document); }
@Override public void read(InputStream inputStream, WordReaderCallBack callBack) throws Exception { XWPFDocument document = new XWPFDocument(inputStream); document.getParagraphs().forEach(callBack::onParagraph); document.getTables().forEach(callBack::onTable); callBack.done(document); }
File f=new File("test.docx"); FileInputStream fis = new FileInputStream(f); XWPFDocument xdoc=new XWPFDocument(OPCPackage.open(fis)); XWPFStyles styles=xdoc.getStyles(); List<XWPFParagraph> xwpfparagraphs =xdoc.getParagraphs(); System.out.println(); for(int i=0;i<xwpfparagraphs.size();i++) { System.out.println("paragraph style id "+(i+1)+":"+xwpfparagraphs.get(i).getStyleID()); if(xwpfparagraphs.get(i).getStyleID()!=null) { String styleid=xwpfparagraphs.get(i).getStyleID(); XWPFStyle style=styles.getStyle(styleid); if(style!=null) { System.out.println("Style name:"+style.getName()); if(style.getName().startsWith("heading")) { //this is a heading } } } }
XWPFDocument document = new XWPFDocument(PoiTest.class.getResourceAsStream("twocolumn.docx")); XWPFParagraph tmpParagraph = document.getParagraphs().get(0); for (int i = 0; i < 100; i++) { XWPFRun tmpRun = tmpParagraph.createRun(); tmpRun.setText("LALALALAALALAAAA"); tmpRun.setFontSize(18); } document.write(new FileOutputStream(new File("C:\\temp\\poi.docx")));
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream(new File("transformed.docx")); XWPFDocument doc = new XWPFDocument(new FileInputStream(new File("original.docx"))); for(XWPFParagraph p:doc.getParagraphs()){ for(XWPFRun r:p.getRuns()){ for(CTText ct:r.getCTR().getTList()){ String str = ct.getStringValue(); if(str.contains("NAME")){ str = str.replace("NAME", "Java Dev"); ct.setStringValue(str); } } } } doc.write(fos); }
@Override public List<Page> parse(InputStream fs, List<String> filterPatterns) throws Exception { List<Page> pages = new ArrayList<>(); XWPFDocument document = new XWPFDocument(fs); int i = 1; for (XWPFParagraph xwpfParagraph : document.getParagraphs()) { Page page = new Page(i); List<XWPFRun> runs = xwpfParagraph.getRuns(); StringBuilder sb = new StringBuilder(); for (XWPFRun run : runs) { if (null != run.getText(0)) { sb.append(run.getText(0)); } } page.getParagraphs().add(sb.toString()); if (!sb.toString().equals("")) { pages.add(page); ++i; } } return pages; } }
XWPFParagraph pr = doc.getParagraphs().get(0); int pos = destDoc.getParagraphs().size() - 1;
for (XWPFParagraph p : doc.getParagraphs()){
/** * 构造函数,用以分析文档,解析出所有的标签 * * @param document Word OOXML document instance. */ public WordBookMarkParser(XWPFDocument document) { //初始化标签缓存 this.bookmarks = new HashMap<String, BookMark>(); // 首先解析文档普通段落中的标签 this.procParaList(document.getParagraphs()); //利用繁琐的方法,从所有的表格中得到得到标签,处理比较原始和简单 List<XWPFTable> tableList = document.getTables(); for (XWPFTable table : tableList) { //得到表格的列信息 List<XWPFTableRow> rowList = table.getRows(); for (XWPFTableRow row : rowList){ //得到行中的列信息 List<XWPFTableCell> cellList = row.getTableCells(); for (XWPFTableCell cell : cellList) { //逐个解析标签信息 //this.procParaList(cell.getParagraphs(), row); this.procParaList(cell); } } } }