1 回答
TA贡献1820条经验 获得超10个赞
错误中第 2 行第 0 列的指向具有误导性。如果你看,/word/document.xml那么根本只有 2 行。第 1 行中的 XML 声明和第 2 行中的所有其他 XML。
您的问题是架构定义CT_OuterShadowEffect是:
<xsd:complexType name="CT_OuterShadowEffect">
<xsd:sequence>
<xsd:group ref="EG_ColorChoice" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="blurRad" type="ST_PositiveCoordinate" use="optional" default="0"/>
<xsd:attribute name="dist" type="ST_PositiveCoordinate" use="optional" default="0"/>
<xsd:attribute name="dir" type="ST_PositiveFixedAngle" use="optional" default="0"/>
<xsd:attribute name="sx" type="ST_Percentage" use="optional" default="100%"/>
<xsd:attribute name="sy" type="ST_Percentage" use="optional" default="100%"/>
<xsd:attribute name="kx" type="ST_FixedAngle" use="optional" default="0"/>
<xsd:attribute name="ky" type="ST_FixedAngle" use="optional" default="0"/>
<xsd:attribute name="algn" type="ST_RectAlignment" use="optional" default="b"/>
<xsd:attribute name="rotWithShape" type="xsd:boolean" use="optional" default="true"/>
</xsd:complexType>
如您所见,EG_ColorChoice必须发生 1 次。换句话说: 必须有一个颜色设置CTOuterShadowEffect。
以下代码对我有用:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
public class WordInsertPicturesWithShadow {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument(new FileInputStream("source.docx"));
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The picture: ");
InputStream in = new FileInputStream("samplePict.jpeg");
XWPFPicture picture = run.addPicture(in, Document.PICTURE_TYPE_JPEG, "samplePict.jpeg", Units.toEMU(100), Units.toEMU(100));
in.close();
picture.getCTPicture().getSpPr().addNewEffectLst().addNewOuterShdw().setBlurRad(50000);
//picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().setDist(100000);
//picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().setDir(2700000);
//picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().setAlgn(
// org.openxmlformats.schemas.drawingml.x2006.main.STRectAlignment.TL);
//picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().setRotWithShape(false);
picture.getCTPicture().getSpPr().getEffectLst().getOuterShdw().addNewPrstClr().setVal(
org.openxmlformats.schemas.drawingml.x2006.main.STPresetColorVal.BLACK);
run.setText(" text after the picture.");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("result.docx");
document.write(out);
out.close();
document.close();
}
}
添加回答
举报