use of org.docx4j.wml.Text in project Java-Tutorial by gpcodervn.
the class HyperlinkTest method testContent.
private void testContent(Object o, Class clazz, String content) {
assertEquals(o.getClass(), clazz);
Object o2;
if (o instanceof P.Hyperlink) {
P.Hyperlink h = (P.Hyperlink) o;
o2 = XmlUtils.unwrap(((R) h.getContent().get(0)).getContent().get(0));
} else {
R r = (R) o;
o2 = XmlUtils.unwrap(r.getContent().get(0));
}
String runText = ((Text) o2).getValue();
assertEquals(content, runText);
}
use of org.docx4j.wml.Text in project Java-Tutorial by gpcodervn.
the class ReportFromTemplate method main.
public static void main(String[] args) {
final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
String fileName = "";
try {
// Populate the Strings that will replace the template text
Map<String, String> map = new HashMap<String, String>();
map.put("Project", "BP Mount");
map.put("Date", "21-Mar-2011");
// C:\\test\\template1.docx is the template file
WordprocessingMLPackage template = WordprocessingMLPackage.load(new File("resources/Template1.docx"));
Parts parts = template.getParts();
HashMap<PartName, Part> partsMap = parts.getParts();
PartName partName = null;
Part part = null;
Set<PartName> set = partsMap.keySet();
for (Iterator<PartName> iterator = set.iterator(); iterator.hasNext(); ) {
PartName name = (PartName) iterator.next();
if (name.getName().equalsIgnoreCase("/word/media/image1.png")) {
part = partsMap.get(name);
partName = name;
}
}
if (part != null && partName != null) {
part = partsMap.get(partName);
BinaryPart binaryPart = (BinaryPart) part;
binaryPart.setBinaryData(fileToBytes(fileToReplace));
}
List<Object> texts = template.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);
for (Object obj : texts) {
Text text = (Text) ((JAXBElement) obj).getValue();
String textValue = text.getValue();
for (Object key : map.keySet()) {
// textValue = textValue.replaceAll("\\$\\{" + key + "\\}", (String) map.get(key));
textValue = textValue.trim().replace("${" + key + "}", (String) map.get(key));
}
text.setValue(textValue);
}
/*
* Add the other contents here
*/
template.save(new File("output/ReportFromTemplate.docx"));
System.out.println("Done");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Errors");
}
}
use of org.docx4j.wml.Text in project docx4j-template by vindell.
the class Docx4J_例子2 method createImageParagraph.
// 段落中插入文字和图片
public P createImageParagraph(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, P p, String fileName, String content, byte[] bytes, JcEnumeration jcEnumeration) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Inline inline = imagePart.createImageInline(fileName, "这是图片", 1, 2, false);
Text text = factory.createText();
text.setValue(content);
text.setSpace("preserve");
R run = factory.createR();
p.getContent().add(run);
run.getContent().add(text);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
PPr pPr = p.getPPr();
if (pPr == null) {
pPr = factory.createPPr();
}
Jc jc = pPr.getJc();
if (jc == null) {
jc = new Jc();
}
jc.setVal(jcEnumeration);
pPr.setJc(jc);
p.setPPr(pPr);
setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
return p;
}
use of org.docx4j.wml.Text in project docx4j-template by vindell.
the class Docx4J_例子2 method getTextHdr.
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage, ObjectFactory factory, Part sourcePart, String content, boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration) throws Exception {
Hdr hdr = factory.createHdr();
P headP = factory.createP();
Text text = factory.createText();
text.setValue(content);
R run = factory.createR();
run.getContent().add(text);
headP.getContent().add(run);
PPr pPr = headP.getPPr();
if (pPr == null) {
pPr = factory.createPPr();
}
Jc jc = pPr.getJc();
if (jc == null) {
jc = new Jc();
}
jc.setVal(jcEnumeration);
pPr.setJc(jc);
if (isUnderLine) {
PBdr pBdr = pPr.getPBdr();
if (pBdr == null) {
pBdr = factory.createPPrBasePBdr();
}
CTBorder value = new CTBorder();
value.setVal(STBorder.SINGLE);
value.setColor("000000");
value.setSpace(new BigInteger("0"));
value.setSz(new BigInteger(underLineSz));
pBdr.setBetween(value);
pPr.setPBdr(pBdr);
headP.setPPr(pPr);
}
setParagraphSpacing(factory, headP, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
hdr.getContent().add(headP);
if (isUnderLine) {
hdr.getContent().add(createHeaderBlankP(wordprocessingMLPackage, factory, underLineSz, jcEnumeration));
}
return hdr;
}
use of org.docx4j.wml.Text in project docx4j-template by vindell.
the class Docx4J_例子2 method createNormalCell.
public void createNormalCell(ObjectFactory factory, Tr tr, String content, RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor, String backgroudColor, boolean isHMerger, String mergeVal) {
Tc tableCell = factory.createTc();
P p = factory.createP();
setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
Text t = factory.createText();
t.setValue(content);
R run = factory.createR();
// 设置表格内容字体样式
run.setRPr(rpr);
TcPr tcPr = tableCell.getTcPr();
if (tcPr == null) {
tcPr = factory.createTcPr();
}
CTVerticalJc valign = factory.createCTVerticalJc();
valign.setVal(STVerticalJc.CENTER);
tcPr.setVAlign(valign);
if (isHMerger) {
HMerge merge = new HMerge();
if (mergeVal != null) {
merge.setVal(mergeVal);
}
tcPr.setHMerge(merge);
} else {
VMerge merge = new VMerge();
if (mergeVal != null) {
merge.setVal(mergeVal);
}
tcPr.setVMerge(merge);
}
run.getContent().add(t);
p.getContent().add(run);
tableCell.getContent().add(p);
if (hasBgColor) {
CTShd shd = tcPr.getShd();
if (shd == null) {
shd = factory.createCTShd();
}
shd.setColor("auto");
shd.setFill(backgroudColor);
tcPr.setShd(shd);
}
tableCell.setTcPr(tcPr);
tr.getContent().add(tableCell);
}
Aggregations