Search in sources :

Example 6 with XWPFParagraph

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

the class TestDocumentProtection method testIntegration.

@Test
public void testIntegration() throws IOException {
    XWPFDocument doc1 = new XWPFDocument();
    XWPFParagraph p1 = doc1.createParagraph();
    XWPFRun r1 = p1.createRun();
    r1.setText("Lorem ipsum dolor sit amet.");
    doc1.enforceCommentsProtection();
    File tempFile = TempFile.createTempFile("documentProtectionFile", ".docx");
    FileOutputStream out = new FileOutputStream(tempFile);
    doc1.write(out);
    out.close();
    FileInputStream inputStream = new FileInputStream(tempFile);
    XWPFDocument doc2 = new XWPFDocument(inputStream);
    inputStream.close();
    assertTrue(doc2.isEnforcedCommentsProtection());
    doc2.close();
    doc1.close();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) TempFile(org.apache.poi.util.TempFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 7 with XWPFParagraph

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

the class TestXWPFDecorators method testHyperlink.

public void testHyperlink() {
    XWPFParagraph ps;
    XWPFParagraph ph;
    assertEquals(7, simple.getParagraphs().size());
    assertEquals(5, hyperlink.getParagraphs().size());
    // Simple text
    ps = simple.getParagraphs().get(0);
    assertEquals("I am a test document", ps.getParagraphText());
    assertEquals(1, ps.getRuns().size());
    ph = hyperlink.getParagraphs().get(4);
    assertEquals("We have a hyperlink here, and another.", ph.getParagraphText());
    assertEquals(3, ph.getRuns().size());
    // The proper way to do hyperlinks(!)
    assertFalse(ps.getRuns().get(0) instanceof XWPFHyperlinkRun);
    assertFalse(ph.getRuns().get(0) instanceof XWPFHyperlinkRun);
    assertTrue(ph.getRuns().get(1) instanceof XWPFHyperlinkRun);
    assertFalse(ph.getRuns().get(2) instanceof XWPFHyperlinkRun);
    XWPFHyperlinkRun link = (XWPFHyperlinkRun) ph.getRuns().get(1);
    assertEquals("http://poi.apache.org/", link.getHyperlink(hyperlink).getURL());
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFHyperlinkRun(org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun)

Example 8 with XWPFParagraph

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

the class TestXWPFDecorators method testComments.

public void testComments() {
    int numComments = 0;
    for (XWPFParagraph p : comments.getParagraphs()) {
        XWPFCommentsDecorator d = new XWPFCommentsDecorator(p, null);
        if (d.getCommentText().length() > 0) {
            numComments++;
            assertEquals("\tComment by", d.getCommentText().substring(0, 11));
        }
    }
    assertEquals(3, numComments);
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph)

Example 9 with XWPFParagraph

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

the class XWPFWordExtractorDecorator method extractIBodyText.

private void extractIBodyText(IBody bodyElement, XWPFListManager listManager, XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException {
    for (IBodyElement element : bodyElement.getBodyElements()) {
        if (element instanceof XWPFParagraph) {
            XWPFParagraph paragraph = (XWPFParagraph) element;
            extractParagraph(paragraph, listManager, xhtml);
        }
        if (element instanceof XWPFTable) {
            XWPFTable table = (XWPFTable) element;
            extractTable(table, listManager, xhtml);
        }
        if (element instanceof XWPFSDT) {
            extractSDT((XWPFSDT) element, xhtml);
        }
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) IBodyElement(org.apache.poi.xwpf.usermodel.IBodyElement) XWPFSDT(org.apache.poi.xwpf.usermodel.XWPFSDT)

Example 10 with XWPFParagraph

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

the class MSWord method addToTable.

/**
	 * 테이블을 추가한다.
	 *
	 * List의 로우는 각 행을 의미. TreeSet은 테이블 컬럼Index 데이터를 의미.
	 *
	 * @param list
	 */
public XWPFTable addToTable(List<List<String>> list, ITableCustomProperty property) /*
																						* ,
																						* ThFunction
																						* <
																						* ,
																						* U
																						* ,
																						* V
																						* ,
																						* R
																						* >
																						*/
{
    if (list == null || list.isEmpty()) {
        return null;
    }
    // -- OR --
    // open an existing empty document with styles already defined
    // XWPFDocument doc = new XWPFDocument(new
    // FileInputStream("base_document.docx"));
    int rowSize = list.size();
    int colSize = list.get(0).size();
    XWPFTable table = doc.createTable(rowSize, colSize);
    // 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");
    }
    /* Table Width조절을 위한 작업 */
    {
        CTTbl ctTbl = table.getCTTbl();
        CTTblPr tblPr = ctTbl.getTblPr();
        CTTblWidth tblW = tblPr.getTblW();
        // 화면에 WIDTH를 딱 맞춤.
        tblW.setW(BigInteger.valueOf(5000));
        tblW.setType(STTblWidth.PCT);
    }
    // Get a list of the rows in the table
    // List<XWPFTableRow> rows = table.getRows();
    int rowCt = 0;
    int colCt = 0;
    for (List<String> set : list) {
        XWPFTableRow row = table.getRow(rowCt);
        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 */
        240));
        // get the cells in this row
        List<XWPFTableCell> cells = row.getTableCells();
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            if (cells.size() == colCt) {
                row.createCell();
            }
            // get a table cell properties element (tcPr)
            XWPFTableCell cell = cells.get(colCt);
            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("FFFE99");
            } else if (rowCt % 2 == 0) {
                // even row
                // FFFFFF : 흰색
                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
            // 2015.3.17 fix
            // XWPFRun rh = para.createRun();
            KrXWPFRun rh = new KrXWPFRun(para.createRun());
            rh.setFontFamily(fontName);
            // style cell as desired
            if (colCt == colSize - 1) {
                // last column is 10pt Courier
                rh.setFontSize(DEFAULT_FONT_SIZE);
            }
            String content = it.next();
            if (rowCt == 0) {
                // header row
                rh.setText(content);
                rh.setFontSize(H5);
                rh.setBold(true);
                para.setAlignment(ParagraphAlignment.CENTER);
            } else if (rowCt % 2 == 0) {
                // even row
                addTableText(rh, content);
                rh.setSubscript(VerticalAlign.BASELINE);
                para.setAlignment(ParagraphAlignment.LEFT);
            } else {
                addTableText(rh, content);
                // odd row
                rh.setSubscript(VerticalAlign.BASELINE);
                para.setAlignment(ParagraphAlignment.LEFT);
            }
            colCt++;
        }
        // for cell
        colCt = 0;
        rowCt++;
    }
    if (property != null) {
        property.doCustom(table);
    }
    return table;
}
Also used : CTTblWidth(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) CTShd(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd) CTString(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString) CTTrPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) CTHeight(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight) CTString(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTVerticalJc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)

Aggregations

XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)19 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)13 FileOutputStream (java.io.FileOutputStream)9 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)9 File (java.io.File)5 OutputStream (java.io.OutputStream)5 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)5 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)3 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)3 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)3 FileInputStream (java.io.FileInputStream)2 XWPFFooter (org.apache.poi.xwpf.usermodel.XWPFFooter)2 XWPFHeader (org.apache.poi.xwpf.usermodel.XWPFHeader)2 XWPFHyperlinkRun (org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun)2 XWPFSDT (org.apache.poi.xwpf.usermodel.XWPFSDT)2 CTHeight (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight)2 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)2 CTShd (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd)2 CTString (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString)2 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)2