Search in sources :

Example 31 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project poi by apache.

the class SimpleTable method createStyledTable.

/**
     * Create a table with some row and column styling. I "manually" add the
     * style name to the table, but don't check to see if the style actually
     * exists in the document. Since I'm creating it from scratch, it obviously
     * won't exist. When opened in MS Word, the table style becomes "Normal".
     * I manually set alternating row colors. This could be done using Themes,
     * but that's left as an exercise for the reader. The cells in the last
     * column of the table have 10pt. "Courier" font.
     * I make no claims that this is the "right" way to do it, but it worked
     * for me. Given the scarcity of XWPF examples, I thought this may prove
     * instructive and give you ideas for your own solutions.

     * @throws Exception
     */
public static void createStyledTable() throws Exception {
    // Create a new document from scratch
    XWPFDocument doc = new XWPFDocument();
    try {
        // -- OR --
        // open an existing empty document with styles already defined
        //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));
        // Create a new table with 6 rows and 3 columns
        int nRows = 6;
        int nCols = 3;
        XWPFTable table = doc.createTable(nRows, nCols);
        // Set the table style. If the style is not defined, the table style
        // will become "Normal".
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTString styleStr = tblPr.addNewTblStyle();
        styleStr.setVal("StyledTable");
        // Get a list of the rows in the table
        List<XWPFTableRow> rows = table.getRows();
        int rowCt = 0;
        int colCt = 0;
        for (XWPFTableRow row : rows) {
            // get table row properties (trPr)
            CTTrPr trPr = row.getCtRow().addNewTrPr();
            // set row height; units = twentieth of a point, 360 = 0.25"
            CTHeight ht = trPr.addNewTrHeight();
            ht.setVal(BigInteger.valueOf(360));
            // get the cells in this row
            List<XWPFTableCell> cells = row.getTableCells();
            // add content to each cell
            for (XWPFTableCell cell : cells) {
                // get a table cell properties element (tcPr)
                CTTcPr tcpr = cell.getCTTc().addNewTcPr();
                // set vertical alignment to "center"
                CTVerticalJc va = tcpr.addNewVAlign();
                va.setVal(STVerticalJc.CENTER);
                // create cell color element
                CTShd ctshd = tcpr.addNewShd();
                ctshd.setColor("auto");
                ctshd.setVal(STShd.CLEAR);
                if (rowCt == 0) {
                    // header row
                    ctshd.setFill("A7BFDE");
                } else if (rowCt % 2 == 0) {
                    // even row
                    ctshd.setFill("D3DFEE");
                } else {
                    // odd row
                    ctshd.setFill("EDF2F8");
                }
                // get 1st paragraph in cell's paragraph list
                XWPFParagraph para = cell.getParagraphs().get(0);
                // create a run to contain the content
                XWPFRun rh = para.createRun();
                // style cell as desired
                if (colCt == nCols - 1) {
                    // last column is 10pt Courier
                    rh.setFontSize(10);
                    rh.setFontFamily("Courier");
                }
                if (rowCt == 0) {
                    // header row
                    rh.setText("header row, col " + colCt);
                    rh.setBold(true);
                    para.setAlignment(ParagraphAlignment.CENTER);
                } else {
                    // other rows
                    rh.setText("row " + rowCt + ", col " + colCt);
                    para.setAlignment(ParagraphAlignment.LEFT);
                }
                colCt++;
            }
            // for cell
            colCt = 0;
            rowCt++;
        }
        // for row
        // write the file
        OutputStream out = new FileOutputStream("styledTable.docx");
        try {
            doc.write(out);
        } finally {
            out.close();
        }
    } finally {
        doc.close();
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) CTShd(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd) CTTrPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) CTHeight(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CTString(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) CTVerticalJc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)

Example 32 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project tika by apache.

the class XWPFWordExtractorDecorator method extractParagraph.

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);
        }
    }
    // Is this a paragraph, or a heading?
    String tag = "p";
    String styleClass = null;
    //TIKA-2144 check that styles is not null
    if (paragraph.getStyleID() != null && styles != 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);
    // 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();
    }
    //  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);
    //hyperlinks may or may not have hyperlink ids
    String lastHyperlinkId = null;
    boolean inHyperlink = false;
    // Do the iruns
    for (IRunElement run : paragraph.getIRuns()) {
        if (run instanceof XWPFHyperlinkRun) {
            XWPFHyperlinkRun hyperlinkRun = (XWPFHyperlinkRun) run;
            if (hyperlinkRun.getHyperlinkId() == null || !hyperlinkRun.getHyperlinkId().equals(lastHyperlinkId)) {
                if (inHyperlink) {
                    //close out the old one
                    xhtml.endElement("a");
                    inHyperlink = false;
                }
                lastHyperlinkId = hyperlinkRun.getHyperlinkId();
                fmtg = closeStyleTags(xhtml, fmtg);
                XWPFHyperlink link = hyperlinkRun.getHyperlink(document);
                if (link != null && link.getURL() != null) {
                    xhtml.startElement("a", "href", link.getURL());
                    inHyperlink = true;
                } else if (hyperlinkRun.getAnchor() != null && hyperlinkRun.getAnchor().length() > 0) {
                    xhtml.startElement("a", "href", "#" + hyperlinkRun.getAnchor());
                    inHyperlink = true;
                }
            }
        } else if (inHyperlink) {
            //if this isn't a hyperlink, but the last one was
            closeStyleTags(xhtml, fmtg);
            xhtml.endElement("a");
            lastHyperlinkId = null;
            inHyperlink = false;
        }
        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);
    if (inHyperlink) {
        xhtml.endElement("a");
    }
    // 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:
    if (config.getIncludeShapeBasedContent()) {
        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);
    }
}
Also used : XWPFHyperlink(org.apache.poi.xwpf.usermodel.XWPFHyperlink) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFCommentsDecorator(org.apache.poi.xwpf.model.XWPFCommentsDecorator) XWPFStyle(org.apache.poi.xwpf.usermodel.XWPFStyle) QName(javax.xml.namespace.QName) XWPFHyperlinkRun(org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun) CTBookmark(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) CTSectPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr) XWPFSDT(org.apache.poi.xwpf.usermodel.XWPFSDT) XmlCursor(org.apache.xmlbeans.XmlCursor) CTObject(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTObject) AttributesImpl(org.xml.sax.helpers.AttributesImpl) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) TagAndStyle(org.apache.tika.parser.microsoft.WordExtractor.TagAndStyle) XmlObject(org.apache.xmlbeans.XmlObject) IRunElement(org.apache.poi.xwpf.usermodel.IRunElement)

Example 33 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project Gargoyle by callakrsos.

the class MSWord method addNewPage.

/**
	 * 새로운 페이지로 넘긴다.
	 */
public void addNewPage() {
    XWPFParagraph p = doc.createParagraph();
    XWPFRun r1 = p.createRun();
    r1.addBreak(BreakType.PAGE);
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun)

Example 34 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project Gargoyle by callakrsos.

the class MSWord method addBreak.

/**
	 * 줄바꿈한다.
	 */
public void addBreak() {
    XWPFParagraph p = doc.createParagraph();
    XWPFRun r1 = new KrXWPFRun(p.createRun());
    r1.addBreak();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun)

Example 35 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project tutorials by eugenp.

the class WordIntegrationTest method whenParsingOutputDocument_thenCorrect.

@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());
}
Also used : Path(java.nio.file.Path) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) Test(org.junit.Test)

Aggregations

XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)45 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)39 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)20 FileOutputStream (java.io.FileOutputStream)11 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)10 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)8 IOException (java.io.IOException)7 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)7 File (java.io.File)5 OutputStream (java.io.OutputStream)5 CommonUtils.nullToEmptyString (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)5 FileInputStream (java.io.FileInputStream)4 XmlCursor (org.apache.xmlbeans.XmlCursor)4 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)3 HashMap (java.util.HashMap)3 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)3 IORuntimeException (cn.hutool.core.io.IORuntimeException)2 POIException (cn.hutool.poi.exceptions.POIException)2 Path (java.nio.file.Path)2 Matcher (java.util.regex.Matcher)2