Search in sources :

Example 21 with CTP

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP 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)

Example 22 with CTP

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

the class TestXWPFParagraph method testSetGetSpacingLineRule.

@Test
public void testSetGetSpacingLineRule() throws IOException {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();
    CTP ctp = p.getCTP();
    CTPPr ppr = ctp.getPPr() == null ? ctp.addNewPPr() : ctp.getPPr();
    assertEquals(STLineSpacingRule.INT_AUTO, p.getSpacingLineRule().getValue());
    CTSpacing spacing = ppr.addNewSpacing();
    spacing.setLineRule(STLineSpacingRule.AT_LEAST);
    assertEquals(LineSpacingRule.AT_LEAST, p.getSpacingLineRule());
    p.setSpacingAfter(100);
    assertEquals(100, spacing.getAfter().intValue());
    doc.close();
}
Also used : CTPPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr) CTSpacing(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) Test(org.junit.Test)

Example 23 with CTP

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

the class TestXWPFParagraph method testSetGetWordWrap.

@Test
public void testSetGetWordWrap() throws IOException {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();
    CTP ctp = p.getCTP();
    CTPPr ppr = ctp.getPPr() == null ? ctp.addNewPPr() : ctp.getPPr();
    CTOnOff wordWrap = ppr.addNewWordWrap();
    wordWrap.setVal(STOnOff.FALSE);
    assertEquals(false, p.isWordWrap());
    p.setWordWrapped(true);
    assertEquals(STOnOff.TRUE, ppr.getWordWrap().getVal());
    doc.close();
}
Also used : CTPPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr) CTOnOff(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) Test(org.junit.Test)

Example 24 with CTP

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

the class TestXWPFTable method testGetText.

public void testGetText() {
    XWPFDocument doc = new XWPFDocument();
    CTTbl table = CTTbl.Factory.newInstance();
    CTRow row = table.addNewTr();
    CTTc cell = row.addNewTc();
    CTP paragraph = cell.addNewP();
    CTR run = paragraph.addNewR();
    CTText text = run.addNewT();
    text.setStringValue("finally I can write!");
    XWPFTable xtab = new XWPFTable(table, doc);
    assertEquals("finally I can write!\n", xtab.getText());
}
Also used : CTR(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) CTTc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc)

Example 25 with CTP

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

the class TestXWPFParagraph method testSetGetAlignment.

@Test
public void testSetGetAlignment() throws IOException {
    //new clean instance of paragraph
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();
    assertEquals(STJc.LEFT.intValue(), p.getAlignment().getValue());
    CTP ctp = p.getCTP();
    CTPPr ppr = ctp.getPPr() == null ? ctp.addNewPPr() : ctp.getPPr();
    CTJc align = ppr.addNewJc();
    align.setVal(STJc.CENTER);
    assertEquals(ParagraphAlignment.CENTER, p.getAlignment());
    p.setAlignment(ParagraphAlignment.BOTH);
    assertEquals(STJc.BOTH, ppr.getJc().getVal());
    doc.close();
}
Also used : CTJc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc) CTPPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) Test(org.junit.Test)

Aggregations

CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)28 XmlCursor (org.apache.xmlbeans.XmlCursor)12 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)11 XmlObject (org.apache.xmlbeans.XmlObject)10 Test (org.junit.Test)10 CTPPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr)10 CTR (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR)4 CTText (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)4 BigInteger (java.math.BigInteger)3 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 POIXMLException (org.apache.poi.POIXMLException)2 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)2 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)2 CTOnOff (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff)2 CTSpacing (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing)2 CTLock (com.microsoft.schemas.office.office.CTLock)1 CTFormulas (com.microsoft.schemas.vml.CTFormulas)1 CTGroup (com.microsoft.schemas.vml.CTGroup)1