use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project LAMSADE-tools by LAntoine.
the class SetCoordinates method setDetails.
/**
* Add the user details to the source file and return the result in the
* destination file
*
* @param source
* @param destinationFile
* @param user
* @throws IOException
* @throws InvalidFormatException
*/
public static void setDetails(UserDetails user) throws IOException, InvalidFormatException {
String source = "com/github/lamsadetools/setCoordinates/papier_en_tete.docx";
String destination = "com/github/lamsadetools/setCoordinates/papier_en_tete_CloneTest.docx";
ClassLoader classLoader = SetCoordinates.class.getClassLoader();
File sourceFile = new File(classLoader.getResource(source).getFile());
File destinationFile = new File(classLoader.getResource(destination).getFile());
copy(sourceFile, destinationFile);
try (XWPFDocument doc = new XWPFDocument(OPCPackage.open(sourceFile))) {
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
for (XWPFParagraph p : policy.getFirstPageHeader().getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
String text = r.getText(0);
if ((text != null) && text.contains("Prenom")) {
System.out.println("contains prenom");
text = text.replace("Prenom", user.getFirstName());
r.setText(text, 0);
} else if ((text != null) && text.contains("Nom")) {
text = text.replace("Nom", user.getName());
r.setText(text, 0);
} else if ((text != null) && text.contains("e-mail")) {
text = text.replace("e-mail", user.getEmail());
r.setText(text, 0);
} else if ((text != null) && text.contains("tel.")) {
text = text.replace("tel.", user.getNumber());
r.setText(text, 0);
} else if ((text != null) && text.contains("Fonction")) {
text = text.replace("Fonction", user.getFunction());
r.setText(text, 0);
}
}
}
}
try (FileOutputStream fos = new FileOutputStream(destinationFile)) {
doc.write(fos);
}
}
long start = System.currentTimeMillis();
System.err.println("Generate papier_en_tete.html with " + (System.currentTimeMillis() - start) + "ms");
}
use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy 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.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.
the class XWPFDocument method onDocumentRead.
@SuppressWarnings("deprecation")
@Override
protected void onDocumentRead() throws IOException {
try {
DocumentDocument doc = DocumentDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
ctDocument = doc.getDocument();
initFootnotes();
// parse the document with cursor and add
// the XmlObject to its lists
XmlCursor cursor = ctDocument.getBody().newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, this);
bodyElements.add(p);
paragraphs.add(p);
} else if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, this);
bodyElements.add(t);
tables.add(t);
} else if (o instanceof CTSdtBlock) {
XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
bodyElements.add(c);
contentControls.add(c);
}
}
cursor.dispose();
// Sort out headers and footers
if (doc.getDocument().getBody().getSectPr() != null)
headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
// Create for each XML-part in the Package a PartClass
for (RelationPart rp : getRelationParts()) {
POIXMLDocumentPart p = rp.getDocumentPart();
String relation = rp.getRelationship().getRelationshipType();
if (relation.equals(XWPFRelation.STYLES.getRelation())) {
this.styles = (XWPFStyles) p;
this.styles.onDocumentRead();
} else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) {
this.numbering = (XWPFNumbering) p;
this.numbering.onDocumentRead();
} else if (relation.equals(XWPFRelation.FOOTER.getRelation())) {
XWPFFooter footer = (XWPFFooter) p;
footers.add(footer);
footer.onDocumentRead();
} else if (relation.equals(XWPFRelation.HEADER.getRelation())) {
XWPFHeader header = (XWPFHeader) p;
headers.add(header);
header.onDocumentRead();
} else if (relation.equals(XWPFRelation.COMMENT.getRelation())) {
// TODO Create according XWPFComment class, extending POIXMLDocumentPart
CommentsDocument cmntdoc = CommentsDocument.Factory.parse(p.getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
for (CTComment ctcomment : cmntdoc.getComments().getCommentArray()) {
comments.add(new XWPFComment(ctcomment, this));
}
} else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) {
settings = (XWPFSettings) p;
settings.onDocumentRead();
} else if (relation.equals(XWPFRelation.IMAGES.getRelation())) {
XWPFPictureData picData = (XWPFPictureData) p;
picData.onDocumentRead();
registerPackagePictureData(picData);
pictures.add(picData);
} else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) {
// Until we do, we do need to load the glossary child parts of it
for (POIXMLDocumentPart gp : p.getRelations()) {
// Trigger the onDocumentRead for all the child parts
// Otherwise we'll hit issues on Styles, Settings etc on save
// TODO: Refactor this to not need to access protected method
// from other package! Remove the static helper method once fixed!!!
POIXMLDocumentPart._invokeOnDocumentRead(gp);
}
}
}
initHyperlinks();
} catch (XmlException e) {
throw new POIXMLException(e);
}
}
use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.
the class XWPFDocument method createHeader.
/**
* Create a header of the given type
*
* @param type {@link HeaderFooterType} enum
* @return object of type {@link XWPFHeader}
*/
public XWPFHeader createHeader(HeaderFooterType type) {
XWPFHeaderFooterPolicy hfPolicy = createHeaderFooterPolicy();
// TODO this needs to be migrated out into section code
if (type == HeaderFooterType.FIRST) {
CTSectPr ctSectPr = getSection();
if (ctSectPr.isSetTitlePg() == false) {
CTOnOff titlePg = ctSectPr.addNewTitlePg();
titlePg.setVal(STOnOff.ON);
}
// } else if (type == HeaderFooterType.EVEN) {
// TODO Add support for Even/Odd headings and footers
}
return hfPolicy.createHeader(STHdrFtr.Enum.forInt(type.toInt()));
}
use of org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy in project poi by apache.
the class XWPFDocument method createFooter.
/**
* Create a footer of the given type
*
* @param type {@link HeaderFooterType} enum
* @return object of type {@link XWPFFooter}
*/
public XWPFFooter createFooter(HeaderFooterType type) {
XWPFHeaderFooterPolicy hfPolicy = createHeaderFooterPolicy();
// TODO this needs to be migrated out into section code
if (type == HeaderFooterType.FIRST) {
CTSectPr ctSectPr = getSection();
if (ctSectPr.isSetTitlePg() == false) {
CTOnOff titlePg = ctSectPr.addNewTitlePg();
titlePg.setVal(STOnOff.ON);
}
// } else if (type == HeaderFooterType.EVEN) {
// TODO Add support for Even/Odd headings and footers
}
return hfPolicy.createFooter(STHdrFtr.Enum.forInt(type.toInt()));
}
Aggregations