use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP 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();
}
}
use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP in project poi by apache.
the class XWPFHeaderFooterPolicy method buildHdrFtr.
/**
* MB 24 May 2010. Created this overloaded buildHdrFtr() method because testing demonstrated
* that the XWPFFooter or XWPFHeader object returned by calls to the createHeader(int, XWPFParagraph[])
* and createFooter(int, XWPFParagraph[]) methods or the getXXXXXHeader/Footer methods where
* headers or footers had been added to a document since it had been created/opened, returned
* an object that contained no XWPFParagraph objects even if the header/footer itself did contain
* text. The reason was that this line of code; CTHdrFtr ftr = CTHdrFtr.Factory.newInstance();
* created a brand new instance of the CTHDRFtr class which was then populated with data when
* it should have recovered the CTHdrFtr object encapsulated within the XWPFHeaderFooter object
* that had previoulsy been instantiated in the createHeader(int, XWPFParagraph[]) or
* createFooter(int, XWPFParagraph[]) methods.
*/
private CTHdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper) {
CTHdrFtr ftr = wrapper._getHdrFtr();
if (paragraphs != null) {
for (int i = 0; i < paragraphs.length; i++) {
CTP p = ftr.addNewP();
ftr.setPArray(i, paragraphs[i].getCTP());
}
// } else {
// CTP p = ftr.addNewP();
// CTBody body = doc.getDocument().getBody();
// if (body.sizeOfPArray() > 0) {
// CTP p0 = body.getPArray(0);
// if (p0.isSetRsidR()) {
// byte[] rsidr = p0.getRsidR();
// byte[] rsidrdefault = p0.getRsidRDefault();
// p.setRsidP(rsidr);
// p.setRsidRDefault(rsidrdefault);
// }
// }
// CTPPr pPr = p.addNewPPr();
// pPr.addNewPStyle().setVal(pStyle);
}
return ftr;
}
use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP in project poi by apache.
the class XWPFHeaderFooter method removeParagraph.
/**
* Removes a specific paragraph from this header / footer
*
* @param paragraph - {@link XWPFParagraph} object to remove
*/
public void removeParagraph(XWPFParagraph paragraph) {
if (paragraphs.contains(paragraph)) {
CTP ctP = paragraph.getCTP();
XmlCursor c = ctP.newCursor();
c.removeXml();
c.dispose();
paragraphs.remove(paragraph);
bodyElements.remove(paragraph);
}
}
use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP in project poi by apache.
the class XWPFTableCell method setText.
public void setText(String text) {
CTP ctP = (ctTc.sizeOfPArray() == 0) ? ctTc.addNewP() : ctTc.getPArray(0);
XWPFParagraph par = new XWPFParagraph(ctP, this);
par.createRun().setText(text);
}
use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP in project poi by apache.
the class XWPFTableCell method insertNewParagraph.
/**
* add a new paragraph at position of the cursor
*
* @param cursor The XmlCursor structure created with XmlBeans
* @return the inserted paragraph
*/
public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
if (!isCursorInTableCell(cursor)) {
return null;
}
String uri = CTP.type.getName().getNamespaceURI();
String localPart = "p";
cursor.beginElement(localPart, uri);
cursor.toParent();
CTP p = (CTP) cursor.getObject();
XWPFParagraph newP = new XWPFParagraph(p, this);
XmlObject o = null;
while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
o = cursor.getObject();
}
if ((!(o instanceof CTP)) || (CTP) o == p) {
paragraphs.add(0, newP);
} else {
int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
paragraphs.add(pos, newP);
}
int i = 0;
XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
bodyElements.add(i, newP);
p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
cursor.toEndToken();
return newP;
}
Aggregations