List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph XWPFParagraph
public XWPFParagraph(CTP prgrph, IBody part)
From source file:File.DOCX.WriteDocx.java
public void CreateHeader(String header) { try {/*from www . j a va2s.c o m*/ CTP ctpHeader = CTP.Factory.newInstance(); CTR ctrHeader = ctpHeader.addNewR(); CTText ctHeader = ctrHeader.addNewT(); ctHeader.setStringValue(header); XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, docx); XWPFParagraph[] parsHeader = new XWPFParagraph[1]; parsHeader[0] = headerParagraph; policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:File.DOCX.WriteDocx.java
public void CreateFooter(String footer) { try {//from w ww . java2 s . co m CTP ctpFooter = CTP.Factory.newInstance(); CTR ctrFooter = ctpFooter.addNewR(); CTText ctFooter = ctrFooter.addNewT(); // String footerText = "This is footer"; ctFooter.setStringValue(footer); XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, docx); XWPFParagraph[] parsFooter = new XWPFParagraph[1]; parsFooter[0] = footerParagraph; policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java
License:Open Source License
/** * Add body elements from the given token source. * /*from w w w. j ava 2 s.c o m*/ * @param source * @param part * @param bodyElements */ private void addBodyElements(XmlTokenSource source, IBody part, List<IBodyElement> bodyElements) { // parse the document with cursor and add // the XmlObject to its lists XmlCursor cursor = source.newCursor(); cursor.selectPath("./*"); while (cursor.toNextSelection()) { XmlObject o = cursor.getObject(); if (o instanceof CTSdtBlock) { // <w:sdt><w:sdtContent><p... CTSdtBlock block = (CTSdtBlock) o; CTSdtContentBlock contentBlock = block.getSdtContent(); if (contentBlock != null) { addBodyElements(contentBlock, part, bodyElements); } } else if (o instanceof CTP) { XWPFParagraph p = new XWPFParagraph((CTP) o, part); bodyElements.add(p); } else if (o instanceof CTTbl) { XWPFTable t = new XWPFTable((CTTbl) o, part); bodyElements.add(t); } } cursor.dispose(); }
From source file:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator.java
License:Apache License
private void extractParagraph(XWPFParagraph paragraph, XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException { // If this paragraph is actually a whole new section, then // it could have its own headers and footers // Check and handle if so XWPFHeaderFooterPolicy headerFooterPolicy = null; if (paragraph.getCTP().getPPr() != null) { CTSectPr ctSectPr = paragraph.getCTP().getPPr().getSectPr(); if (ctSectPr != null) { headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr); extractHeaders(xhtml, headerFooterPolicy); }//from w w w. j av a 2 s . c o m } // Is this a paragraph, or a heading? String tag = "p"; String styleClass = null; if (paragraph.getStyleID() != null) { XWPFStyle style = styles.getStyle(paragraph.getStyleID()); if (style != null && style.getName() != null) { TagAndStyle tas = WordExtractor.buildParagraphTagAndStyle(style.getName(), paragraph.getPartType() == BodyType.TABLECELL); tag = tas.getTag(); styleClass = tas.getStyleClass(); } } if (styleClass == null) { xhtml.startElement(tag); } else { xhtml.startElement(tag, "class", styleClass); } // Output placeholder for any embedded docs: // TODO: replace w/ XPath/XQuery: for (XWPFRun run : paragraph.getRuns()) { XmlCursor c = run.getCTR().newCursor(); c.selectPath("./*"); while (c.toNextSelection()) { XmlObject o = c.getObject(); if (o instanceof CTObject) { XmlCursor c2 = o.newCursor(); c2.selectPath("./*"); while (c2.toNextSelection()) { XmlObject o2 = c2.getObject(); XmlObject embedAtt = o2.selectAttribute(new QName("Type")); if (embedAtt != null && embedAtt.getDomNode().getNodeValue().equals("Embed")) { // Type is "Embed" XmlObject relIDAtt = o2.selectAttribute(new QName( "http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id")); if (relIDAtt != null) { String relID = relIDAtt.getDomNode().getNodeValue(); AttributesImpl attributes = new AttributesImpl(); attributes.addAttribute("", "class", "class", "CDATA", "embedded"); attributes.addAttribute("", "id", "id", "CDATA", relID); xhtml.startElement("div", attributes); xhtml.endElement("div"); } } } c2.dispose(); } } c.dispose(); } // Attach bookmarks for the paragraph // (In future, we might put them in the right place, for now // we just put them in the correct paragraph) for (CTBookmark bookmark : paragraph.getCTP().getBookmarkStartList()) { xhtml.startElement("a", "name", bookmark.getName()); xhtml.endElement("a"); } TmpFormatting fmtg = new TmpFormatting(false, false); // Do the iruns for (IRunElement run : paragraph.getIRuns()) { if (run instanceof XWPFSDT) { fmtg = closeStyleTags(xhtml, fmtg); processSDTRun((XWPFSDT) run, xhtml); // for now, we're ignoring formatting in sdt // if you hit an sdt reset to false fmtg.setBold(false); fmtg.setItalic(false); } else { fmtg = processRun((XWPFRun) run, paragraph, xhtml, fmtg); } } closeStyleTags(xhtml, fmtg); // Now do any comments for the paragraph XWPFCommentsDecorator comments = new XWPFCommentsDecorator(paragraph, null); String commentText = comments.getCommentText(); if (commentText != null && commentText.length() > 0) { xhtml.characters(commentText); } String footnameText = paragraph.getFootnoteText(); if (footnameText != null && footnameText.length() > 0) { xhtml.characters(footnameText + "\n"); } // Also extract any paragraphs embedded in text boxes: for (XmlObject embeddedParagraph : paragraph.getCTP().selectPath( "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' .//*/wps:txbx/w:txbxContent/w:p")) { extractParagraph(new XWPFParagraph(CTP.Factory.parse(embeddedParagraph.xmlText()), paragraph.getBody()), xhtml); } // Finish this paragraph xhtml.endElement(tag); if (headerFooterPolicy != null) { extractFooters(xhtml, headerFooterPolicy); } }
From source file:modificarcabeceradocs.appModificacionDOCs.java
private void modificarDOCs(File pathsDocumento) throws FileNotFoundException, IOException { XWPFDocument doc = new XWPFDocument(); XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy(); if (policy.getDefaultHeader() == null && policy.getFirstPageHeader() == null && policy.getDefaultFooter() == null) { // Need to create some new headers // The easy way, gives a single empty paragraph XWPFHeader headerD = policy.createHeader(policy.DEFAULT); //headerD.getParagraphs().createRun().setText("Hello Header World!"); headerD.getParagraphArray(0).createRun().setText("AAAAAAAAAAAAA"); // Or the full control way CTP ctP1 = CTP.Factory.newInstance(); CTR ctR1 = ctP1.addNewR();//from w w w . j a v a2s. c o m CTText t = ctR1.addNewT(); t.setStringValue("Paragraph in header"); XWPFParagraph p1 = new XWPFParagraph(ctP1, doc); XWPFParagraph[] pars = new XWPFParagraph[1]; pars[0] = p1; policy.createHeader(policy.FIRST, pars); doc.write(new FileOutputStream(new File("prueba.docx"))); System.out.println("Cabecera terminada"); } else { // Already has a header, change it } }
From source file:org.apache.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator.java
License:Apache License
private void extractParagraph(XWPFParagraph paragraph, XWPFListManager listManager, XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException { // If this paragraph is actually a whole new section, then // it could have its own headers and footers // Check and handle if so XWPFHeaderFooterPolicy headerFooterPolicy = null; if (paragraph.getCTP().getPPr() != null) { CTSectPr ctSectPr = paragraph.getCTP().getPPr().getSectPr(); if (ctSectPr != null) { headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr); extractHeaders(xhtml, headerFooterPolicy, listManager); }//w w w . j a v a 2 s . c o m } // Is this a paragraph, or a heading? String tag = "p"; String styleClass = null; if (paragraph.getStyleID() != null) { XWPFStyle style = styles.getStyle(paragraph.getStyleID()); if (style != null && style.getName() != null) { TagAndStyle tas = WordExtractor.buildParagraphTagAndStyle(style.getName(), paragraph.getPartType() == BodyType.TABLECELL); tag = tas.getTag(); styleClass = tas.getStyleClass(); } } if (styleClass == null) { xhtml.startElement(tag); } else { xhtml.startElement(tag, "class", styleClass); } writeParagraphNumber(paragraph, listManager, xhtml); // Output placeholder for any embedded docs: // TODO: replace w/ XPath/XQuery: for (XWPFRun run : paragraph.getRuns()) { XmlCursor c = run.getCTR().newCursor(); c.selectPath("./*"); while (c.toNextSelection()) { XmlObject o = c.getObject(); if (o instanceof CTObject) { XmlCursor c2 = o.newCursor(); c2.selectPath("./*"); while (c2.toNextSelection()) { XmlObject o2 = c2.getObject(); XmlObject embedAtt = o2.selectAttribute(new QName("Type")); if (embedAtt != null && embedAtt.getDomNode().getNodeValue().equals("Embed")) { // Type is "Embed" XmlObject relIDAtt = o2.selectAttribute(new QName( "http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id")); if (relIDAtt != null) { String relID = relIDAtt.getDomNode().getNodeValue(); AttributesImpl attributes = new AttributesImpl(); attributes.addAttribute("", "class", "class", "CDATA", "embedded"); attributes.addAttribute("", "id", "id", "CDATA", relID); xhtml.startElement("div", attributes); xhtml.endElement("div"); } } } c2.dispose(); } } c.dispose(); } // Attach bookmarks for the paragraph // (In future, we might put them in the right place, for now // we just put them in the correct paragraph) for (int i = 0; i < paragraph.getCTP().sizeOfBookmarkStartArray(); i++) { CTBookmark bookmark = paragraph.getCTP().getBookmarkStartArray(i); xhtml.startElement("a", "name", bookmark.getName()); xhtml.endElement("a"); } TmpFormatting fmtg = new TmpFormatting(false, false); // Do the iruns for (IRunElement run : paragraph.getIRuns()) { if (run instanceof XWPFSDT) { fmtg = closeStyleTags(xhtml, fmtg); processSDTRun((XWPFSDT) run, xhtml); //for now, we're ignoring formatting in sdt //if you hit an sdt reset to false fmtg.setBold(false); fmtg.setItalic(false); } else { fmtg = processRun((XWPFRun) run, paragraph, xhtml, fmtg); } } closeStyleTags(xhtml, fmtg); // Now do any comments for the paragraph XWPFCommentsDecorator comments = new XWPFCommentsDecorator(paragraph, null); String commentText = comments.getCommentText(); if (commentText != null && commentText.length() > 0) { xhtml.characters(commentText); } String footnameText = paragraph.getFootnoteText(); if (footnameText != null && footnameText.length() > 0) { xhtml.characters(footnameText + "\n"); } // Also extract any paragraphs embedded in text boxes: for (XmlObject embeddedParagraph : paragraph.getCTP().selectPath( "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' .//*/wps:txbx/w:txbxContent/w:p")) { extractParagraph(new XWPFParagraph(CTP.Factory.parse(embeddedParagraph.xmlText()), paragraph.getBody()), listManager, xhtml); } // Finish this paragraph xhtml.endElement(tag); if (headerFooterPolicy != null) { extractFooters(xhtml, headerFooterPolicy, listManager); } }
From source file:org.cgiar.ccafs.marlo.action.summaries.AnualReportPOISummaryAction.java
License:Open Source License
public void createPageFooter() { CTP ctp = CTP.Factory.newInstance(); // this add page number incremental ctp.addNewR().addNewPgNum();//from www . j av a2 s.c om XWPFParagraph codePara = new XWPFParagraph(ctp, document); XWPFParagraph[] paragraphs = new XWPFParagraph[1]; paragraphs[0] = codePara; // position of number codePara.setAlignment(ParagraphAlignment.CENTER); CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); try { XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr); headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, paragraphs); } catch (IOException e) { LOG.error("Failed to createFooter. Exception: " + e.getMessage()); } }
From source file:org.cgiar.ccafs.marlo.utils.POISummary.java
License:Open Source License
/** * Footer title//from w w w . j ava 2s.c om * * @param document * @param text * @throws IOException */ public void pageFooter(XWPFDocument document, String text) throws IOException { CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr); CTP ctpFooter = CTP.Factory.newInstance(); CTR ctrFooter = ctpFooter.addNewR(); CTText ctFooter = ctrFooter.addNewT(); ctFooter.setStringValue(text); XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document); footerParagraph.setAlignment(ParagraphAlignment.LEFT); XWPFParagraph[] parsFooter = new XWPFParagraph[1]; parsFooter[0] = footerParagraph; policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter); }
From source file:org.cgiar.ccafs.marlo.utils.POISummary.java
License:Open Source License
/** * Header title//from w ww . ja va 2s. c o m * * @param document * @param text * @throws IOException */ public void pageHeader(XWPFDocument document, String text) throws IOException { CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr); CTP ctpHeader = CTP.Factory.newInstance(); CTR ctrHeader = ctpHeader.addNewR(); CTText ctHeader = ctrHeader.addNewT(); ctHeader.setStringValue(text); XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document); headerParagraph.setAlignment(ParagraphAlignment.RIGHT); XWPFParagraph[] parsHeader = new XWPFParagraph[1]; parsHeader[0] = headerParagraph; policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader); }