use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.
the class Docx4J_例子2 method newImage.
public P newImage(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, Part sourcePart, byte[] bytes, String filenameHint, String altText, int id1, int id2, boolean isUnderLine, String underLineSize, JcEnumeration jcEnumeration) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, sourcePart, bytes);
Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);
P p = factory.createP();
R run = factory.createR();
p.getContent().add(run);
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);
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(underLineSize));
pBdr.setBetween(value);
pPr.setPBdr(pBdr);
}
setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
return p;
}
use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.
the class Docx4J_简单例子 method newImage.
public P newImage(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, Part sourcePart, byte[] bytes, String filenameHint, String altText, int id1, int id2, JcEnumeration jcEnumeration) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, sourcePart, bytes);
Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);
P p = factory.createP();
R run = factory.createR();
p.getContent().add(run);
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);
return p;
}
use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage 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, "0", "0");
return p;
}
use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.
the class Docx4jTest method newImage.
/**
* 创建包含图片的内容
*
* @param word
* @param sourcePart
* @param imageFilePath
* @return
* @throws Exception
*/
public static P newImage(WordprocessingMLPackage word, Part sourcePart, String imageFilePath) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(word, sourcePart, new File(imageFilePath));
// 随机数ID
int id = (int) (Math.random() * 10000);
// 这里的id不重复即可
Inline inline = imagePart.createImageInline("image", "image", id, id * 2, false);
Drawing drawing = factory.createDrawing();
drawing.getAnchorOrInline().add(inline);
R r = factory.createR();
r.getContent().add(drawing);
P p = factory.createP();
p.getContent().add(r);
return p;
}
use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.
the class Docx4j_SaveDocxImg_S3_Test method saveDocxImg.
/**
* @Description: 提取word图片
*/
public void saveDocxImg(String filePath, String savePath) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts().entrySet()) {
if (entry.getValue() instanceof BinaryPartAbstractImage) {
BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry.getValue();
// 图片minetype
String imgContentType = binImg.getContentType();
PartName pt = binImg.getPartName();
String fileName = null;
if (pt.getName().indexOf("word/media/") != -1) {
fileName = pt.getName().substring(pt.getName().indexOf("word/media/") + "word/media/".length());
}
System.out.println(String.format("mimetype=%s,filePath=%s", imgContentType, pt.getName()));
FileOutputStream fos = new FileOutputStream(savePath + fileName);
((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);
fos.close();
}
}
}
Aggregations