我要求我有一个 Word(.DOCX) 文件。通过使用 java 程序,我需要使用 DOCX4J 将图像放在文档中的某个位置。谁能帮帮我吗!!!
我正在尝试使用以下代码...
final String XPATH = "//w:t";
String image_Path = "D:\\Temp\\ex.png";
String template_Path = "D:\\Temp\\example.docx";
WordprocessingMLPackage package = WordprocessingMLPackage.createPackage();
List texts = package.getMainDocumentPart().getJAXBNodesViaXPath(XPATH, true);
for (Object obj : texts) {
Text text = (Text) ((JAXBElement) obj).getValue();
ObjectFactory factory = new ObjectFactory();
P paragraph = factory.createP();
R run = factory.createR();
paragraph.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(image_Path);
package.getMainDocumentPart().addObject(paragraph);
package.save(new java.io.File("D:\\Temp\\example.docx"));here