Search in sources :

Example 1 with CTR

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

the class TestXWPFParagraph method testRuns.

@SuppressWarnings("deprecation")
@Test
public void testRuns() throws IOException {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();
    CTR run = CTR.Factory.newInstance();
    XWPFRun r = new XWPFRun(run, doc.createParagraph());
    p.addRun(r);
    p.addRun(r);
    assertNotNull(p.getRun(run));
    assertNull(p.getRun(null));
    doc.close();
}
Also used : CTR(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR) Test(org.junit.Test)

Example 2 with CTR

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR in project Gargoyle by callakrsos.

the class KrXWPFRun method setFontFamily.

@Override
public void setFontFamily(String fontFamily) {
    CTR run = getCTR();
    CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
    CTFonts fonts = pr.isSetRFonts() ? pr.getRFonts() : pr.addNewRFonts();
    fonts.setHAnsi(fontFamily);
    fonts.setEastAsia(fontFamily);
    fonts.setAscii(fontFamily);
    super.setFontFamily(fontFamily);
}
Also used : CTR(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR) CTRPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr) CTFonts(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts)

Example 3 with CTR

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR 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 4 with CTR

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

the class XWPFHeaderFooterPolicy method getWatermarkParagraph.

/*
     * This is the default Watermark paragraph; the only variable is the text message
     * TODO: manage all the other variables
     */
private XWPFParagraph getWatermarkParagraph(String text, int idx) {
    CTP p = CTP.Factory.newInstance();
    byte[] rsidr = doc.getDocument().getBody().getPArray(0).getRsidR();
    byte[] rsidrdefault = doc.getDocument().getBody().getPArray(0).getRsidRDefault();
    p.setRsidP(rsidr);
    p.setRsidRDefault(rsidrdefault);
    CTPPr pPr = p.addNewPPr();
    pPr.addNewPStyle().setVal("Header");
    // start watermark paragraph
    CTR r = p.addNewR();
    CTRPr rPr = r.addNewRPr();
    rPr.addNewNoProof();
    CTPicture pict = r.addNewPict();
    CTGroup group = CTGroup.Factory.newInstance();
    CTShapetype shapetype = group.addNewShapetype();
    shapetype.setId("_x0000_t136");
    shapetype.setCoordsize("1600,21600");
    shapetype.setSpt(136);
    shapetype.setAdj("10800");
    shapetype.setPath2("m@7,0l@8,0m@5,21600l@6,21600e");
    CTFormulas formulas = shapetype.addNewFormulas();
    formulas.addNewF().setEqn("sum #0 0 10800");
    formulas.addNewF().setEqn("prod #0 2 1");
    formulas.addNewF().setEqn("sum 21600 0 @1");
    formulas.addNewF().setEqn("sum 0 0 @2");
    formulas.addNewF().setEqn("sum 21600 0 @3");
    formulas.addNewF().setEqn("if @0 @3 0");
    formulas.addNewF().setEqn("if @0 21600 @1");
    formulas.addNewF().setEqn("if @0 0 @2");
    formulas.addNewF().setEqn("if @0 @4 21600");
    formulas.addNewF().setEqn("mid @5 @6");
    formulas.addNewF().setEqn("mid @8 @5");
    formulas.addNewF().setEqn("mid @7 @8");
    formulas.addNewF().setEqn("mid @6 @7");
    formulas.addNewF().setEqn("sum @6 0 @5");
    CTPath path = shapetype.addNewPath();
    path.setTextpathok(STTrueFalse.T);
    path.setConnecttype(STConnectType.CUSTOM);
    path.setConnectlocs("@9,0;@10,10800;@11,21600;@12,10800");
    path.setConnectangles("270,180,90,0");
    CTTextPath shapeTypeTextPath = shapetype.addNewTextpath();
    shapeTypeTextPath.setOn(STTrueFalse.T);
    shapeTypeTextPath.setFitshape(STTrueFalse.T);
    CTHandles handles = shapetype.addNewHandles();
    CTH h = handles.addNewH();
    h.setPosition("#0,bottomRight");
    h.setXrange("6629,14971");
    CTLock lock = shapetype.addNewLock();
    lock.setExt(STExt.EDIT);
    CTShape shape = group.addNewShape();
    shape.setId("PowerPlusWaterMarkObject" + idx);
    shape.setSpid("_x0000_s102" + (4 + idx));
    shape.setType("#_x0000_t136");
    shape.setStyle("position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin");
    shape.setWrapcoords("616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068");
    shape.setFillcolor("black");
    shape.setStroked(STTrueFalse.FALSE);
    CTTextPath shapeTextPath = shape.addNewTextpath();
    shapeTextPath.setStyle("font-family:"Cambria";font-size:1pt");
    shapeTextPath.setString(text);
    pict.set(group);
    // end watermark paragraph
    return new XWPFParagraph(p, doc);
}
Also used : CTR(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) CTLock(com.microsoft.schemas.office.office.CTLock) CTShape(com.microsoft.schemas.vml.CTShape) CTFormulas(com.microsoft.schemas.vml.CTFormulas) CTPath(com.microsoft.schemas.vml.CTPath) CTTextPath(com.microsoft.schemas.vml.CTTextPath) CTRPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr) CTShapetype(com.microsoft.schemas.vml.CTShapetype) CTHandles(com.microsoft.schemas.vml.CTHandles) CTPPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr) CTH(com.microsoft.schemas.vml.CTH) CTGroup(com.microsoft.schemas.vml.CTGroup) CTPicture(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)

Example 5 with CTR

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

CTR (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR)6 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)4 CTText (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)3 Test (org.junit.Test)2 CTPPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr)2 CTRPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr)2 CTLock (com.microsoft.schemas.office.office.CTLock)1 CTFormulas (com.microsoft.schemas.vml.CTFormulas)1 CTGroup (com.microsoft.schemas.vml.CTGroup)1 CTH (com.microsoft.schemas.vml.CTH)1 CTHandles (com.microsoft.schemas.vml.CTHandles)1 CTPath (com.microsoft.schemas.vml.CTPath)1 CTShape (com.microsoft.schemas.vml.CTShape)1 CTShapetype (com.microsoft.schemas.vml.CTShapetype)1 CTTextPath (com.microsoft.schemas.vml.CTTextPath)1 BigInteger (java.math.BigInteger)1 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)1 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)1 CTFonts (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts)1 CTPicture (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture)1