Search in sources :

Example 1 with CTText

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText in project poi by apache.

the class SimpleDocumentWithHeader method main.

public static void main(String[] args) {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();
    XWPFRun r = p.createRun();
    r.setText("Some Text");
    r.setBold(true);
    r = p.createRun();
    r.setText("Goodbye");
    CTP ctP = CTP.Factory.newInstance();
    CTText t = ctP.addNewR().addNewT();
    t.setStringValue("header");
    pars = new XWPFParagraph[1];
    p = new XWPFParagraph(ctP, doc);
    pars[0] = p;
    XWPFHeaderFooterPolicy hfPolicy = doc.createHeaderFooterPolicy();
    hfPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
    ctP = CTP.Factory.newInstance();
    t = ctP.addNewR().addNewT();
    t.setStringValue("My Footer");
    pars[0] = new XWPFParagraph(ctP, doc);
    hfPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars);
    try {
        OutputStream os = new FileOutputStream(new File("header.docx"));
        doc.write(os);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) IOException(java.io.IOException) File(java.io.File) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)

Example 2 with CTText

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText in project poi by apache.

the class XWPFRun method setText.

/**
     * Sets the text of this text run in the
     *
     * @param value the literal text which shall be displayed in the document
     * @param pos   - position in the text array (NB: 0 based)
     */
public void setText(String value, int pos) {
    if (pos > run.sizeOfTArray())
        throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)");
    CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT();
    t.setStringValue(value);
    preserveSpaces(t);
}
Also used : CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)

Example 3 with CTText

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText in project poi by apache.

the class XWPFRun method text.

/**
     * Returns the string version of the text, with tabs and
     * carriage returns in place of their xml equivalents.
     */
public String text() {
    StringBuffer text = new StringBuffer();
    // Grab the text and tabs of the text run
    // Do so in a way that preserves the ordering
    XmlCursor c = run.newCursor();
    c.selectPath("./*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();
        if (o instanceof CTText) {
            String tagName = o.getDomNode().getNodeName();
            //  in the normal text output
            if (!"w:instrText".equals(tagName)) {
                text.append(((CTText) o).getStringValue());
            }
        }
        // Complex type evaluation (currently only for extraction of check boxes)
        if (o instanceof CTFldChar) {
            CTFldChar ctfldChar = ((CTFldChar) o);
            if (ctfldChar.getFldCharType() == STFldCharType.BEGIN) {
                if (ctfldChar.getFfData() != null) {
                    for (CTFFCheckBox checkBox : ctfldChar.getFfData().getCheckBoxList()) {
                        if (checkBox.getDefault() != null && checkBox.getDefault().getVal() == STOnOff.X_1) {
                            text.append("|X|");
                        } else {
                            text.append("|_|");
                        }
                    }
                }
            }
        }
        if (o instanceof CTPTab) {
            text.append("\t");
        }
        if (o instanceof CTBr) {
            text.append("\n");
        }
        if (o instanceof CTEmpty) {
            // Some inline text elements get returned not as
            //  themselves, but as CTEmpty, owing to some odd
            //  definitions around line 5642 of the XSDs
            // This bit works around it, and replicates the above
            //  rules for that case
            String tagName = o.getDomNode().getNodeName();
            if ("w:tab".equals(tagName) || "tab".equals(tagName)) {
                text.append("\t");
            }
            if ("w:br".equals(tagName) || "br".equals(tagName)) {
                text.append("\n");
            }
            if ("w:cr".equals(tagName) || "cr".equals(tagName)) {
                text.append("\n");
            }
        }
        if (o instanceof CTFtnEdnRef) {
            CTFtnEdnRef ftn = (CTFtnEdnRef) o;
            String footnoteRef = ftn.getDomNode().getLocalName().equals("footnoteReference") ? "[footnoteRef:" + ftn.getId().intValue() + "]" : "[endnoteRef:" + ftn.getId().intValue() + "]";
            text.append(footnoteRef);
        }
    }
    c.dispose();
    // Any picture text?
    if (pictureText != null && pictureText.length() > 0) {
        text.append("\n").append(pictureText);
    }
    return text.toString();
}
Also used : CTFtnEdnRef(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) CTEmpty(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty) XmlObject(org.apache.xmlbeans.XmlObject) XmlString(org.apache.xmlbeans.XmlString) CTPTab(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPTab) CTFFCheckBox(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFFCheckBox) CTBr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr) XmlCursor(org.apache.xmlbeans.XmlCursor) CTFldChar(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar)

Example 4 with CTText

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText in project poi by apache.

the class TOC method addRow.

public void addRow(int level, String title, int page, String bookmarkRef) {
    CTSdtContentBlock contentBlock = this.block.getSdtContent();
    CTP p = contentBlock.addNewP();
    p.setRsidR("00EF7E24".getBytes(LocaleUtil.CHARSET_1252));
    p.setRsidRDefault("00EF7E24".getBytes(LocaleUtil.CHARSET_1252));
    CTPPr pPr = p.addNewPPr();
    pPr.addNewPStyle().setVal("TOC" + level);
    CTTabs tabs = pPr.addNewTabs();
    CTTabStop tab = tabs.addNewTab();
    tab.setVal(STTabJc.RIGHT);
    tab.setLeader(STTabTlc.DOT);
    tab.setPos(new BigInteger("8290"));
    pPr.addNewRPr().addNewNoProof();
    CTR run = p.addNewR();
    run.addNewRPr().addNewNoProof();
    run.addNewT().setStringValue(title);
    run = p.addNewR();
    run.addNewRPr().addNewNoProof();
    run.addNewTab();
    run = p.addNewR();
    run.addNewRPr().addNewNoProof();
    run.addNewFldChar().setFldCharType(STFldCharType.BEGIN);
    // pageref run
    run = p.addNewR();
    run.addNewRPr().addNewNoProof();
    CTText text = run.addNewInstrText();
    text.setSpace(Space.PRESERVE);
    // bookmark reference
    text.setStringValue(" PAGEREF _Toc" + bookmarkRef + " \\h ");
    p.addNewR().addNewRPr().addNewNoProof();
    run = p.addNewR();
    run.addNewRPr().addNewNoProof();
    run.addNewFldChar().setFldCharType(STFldCharType.SEPARATE);
    // page number run
    run = p.addNewR();
    run.addNewRPr().addNewNoProof();
    run.addNewT().setStringValue(Integer.toString(page));
    run = p.addNewR();
    run.addNewRPr().addNewNoProof();
    run.addNewFldChar().setFldCharType(STFldCharType.END);
}
Also used : CTR(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) CTTabs(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabs) CTPPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr) BigInteger(java.math.BigInteger) CTSdtContentBlock(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentBlock) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) CTTabStop(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop)

Example 5 with CTText

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText in project poi by apache.

the class TestXWPFHeader method testSetHeader.

@Test
public void testSetHeader() throws IOException {
    XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
    // no header is set (yet)
    XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
    assertNull(policy.getDefaultHeader());
    assertNull(policy.getFirstPageHeader());
    assertNull(policy.getDefaultFooter());
    assertNull(policy.getFirstPageFooter());
    CTP ctP1 = CTP.Factory.newInstance();
    CTR ctR1 = ctP1.addNewR();
    CTText t = ctR1.addNewT();
    String tText = "Paragraph in header";
    t.setStringValue(tText);
    // Commented MB 23 May 2010
    //CTP ctP2 = CTP.Factory.newInstance();
    //CTR ctR2 = ctP2.addNewR();
    //CTText t2 = ctR2.addNewT();
    //t2.setStringValue("Second paragraph.. for footer");
    // Create two paragraphs for insertion into the footer.
    // Previously only one was inserted MB 23 May 2010
    CTP ctP2 = CTP.Factory.newInstance();
    CTR ctR2 = ctP2.addNewR();
    CTText t2 = ctR2.addNewT();
    t2.setStringValue("First paragraph for the footer");
    CTP ctP3 = CTP.Factory.newInstance();
    CTR ctR3 = ctP3.addNewR();
    CTText t3 = ctR3.addNewT();
    t3.setStringValue("Second paragraph for the footer");
    XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);
    XWPFParagraph[] pars = new XWPFParagraph[1];
    pars[0] = p1;
    XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
    XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);
    XWPFParagraph[] pars2 = new XWPFParagraph[2];
    pars2[0] = p2;
    pars2[1] = p3;
    // Set headers
    XWPFHeader headerD = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
    XWPFHeader headerF = policy.createHeader(XWPFHeaderFooterPolicy.FIRST);
    // Set a default footer and capture the returned XWPFFooter object.
    XWPFFooter footerD = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2);
    XWPFFooter footerF = policy.createFooter(XWPFHeaderFooterPolicy.FIRST);
    // Ensure the headers and footer were set correctly....
    assertNotNull(policy.getDefaultHeader());
    assertNotNull(policy.getFirstPageHeader());
    assertNotNull(policy.getDefaultFooter());
    assertNotNull(policy.getFirstPageFooter());
    // ....and that the footer object captured above contains two
    // paragraphs of text.
    assertEquals(2, footerD.getParagraphs().size());
    assertEquals(0, footerF.getParagraphs().size());
    // Check the header created with the paragraph got them, and the one
    // created without got none
    assertEquals(1, headerD.getParagraphs().size());
    assertEquals(tText, headerD.getParagraphs().get(0).getText());
    assertEquals(0, headerF.getParagraphs().size());
    // As an additional check, recover the defauls footer and
    // make sure that it contains two paragraphs of text and that
    // both do hold what is expected.
    footerD = policy.getDefaultFooter();
    XWPFParagraph[] paras = footerD.getParagraphs().toArray(new XWPFParagraph[0]);
    assertEquals(2, paras.length);
    assertEquals("First paragraph for the footer", paras[0].getText());
    assertEquals("Second paragraph for the footer", paras[1].getText());
    // Add some text to the empty header
    String fText1 = "New Text!";
    String fText2 = "More Text!";
    headerF.createParagraph().insertNewRun(0).setText(fText1);
    headerF.createParagraph().insertNewRun(0).setText(fText2);
    //        headerF.getParagraphs().get(0).insertNewRun(0).setText(fText1);
    // Check it
    assertEquals(tText, headerD.getParagraphs().get(0).getText());
    assertEquals(fText1, headerF.getParagraphs().get(0).getText());
    assertEquals(fText2, headerF.getParagraphs().get(1).getText());
    // Save, re-open, ensure it's all still there
    XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
    policy = reopened.getHeaderFooterPolicy();
    assertNotNull(policy.getDefaultHeader());
    assertNotNull(policy.getFirstPageHeader());
    assertNull(policy.getEvenPageHeader());
    assertNotNull(policy.getDefaultFooter());
    assertNotNull(policy.getFirstPageFooter());
    assertNull(policy.getEvenPageFooter());
    // Check the new headers still have their text
    headerD = policy.getDefaultHeader();
    headerF = policy.getFirstPageHeader();
    assertEquals(tText, headerD.getParagraphs().get(0).getText());
    assertEquals(fText1, headerF.getParagraphs().get(0).getText());
    assertEquals(fText2, headerF.getParagraphs().get(1).getText());
    // Check the new footers have their new text too
    footerD = policy.getDefaultFooter();
    paras = footerD.getParagraphs().toArray(new XWPFParagraph[0]);
    footerF = policy.getFirstPageFooter();
    assertEquals(2, paras.length);
    assertEquals("First paragraph for the footer", paras[0].getText());
    assertEquals("Second paragraph for the footer", paras[1].getText());
    assertEquals(1, footerF.getParagraphs().size());
}
Also used : CTR(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) Test(org.junit.Test)

Aggregations

CTText (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)6 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)4 CTR (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR)3 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 BigInteger (java.math.BigInteger)1 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)1 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)1 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlObject (org.apache.xmlbeans.XmlObject)1 XmlString (org.apache.xmlbeans.XmlString)1 Test (org.junit.Test)1 CTBr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr)1 CTEmpty (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty)1 CTFFCheckBox (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFFCheckBox)1 CTFldChar (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar)1