我想要将图像替换为文本,但当我删除形状节点时,结果是一些图片没有被删除。为什么?
public void replaceImageToMark() throws Exception {
Document doc = new Document("D:\\company\\demo.docx");
NodeCollection shapeCollection = doc.getChildNodes(NodeType.DRAWING_ML, true);
for (int i = 0; i < shapeCollection.getCount(); i++) {
DrawingML drawingML = (DrawingML) shapeCollection.get(i);
if (drawingML.hasImage()) {
String imageFileName = java.text.MessageFormat.format(
"Image.ExportImages.{0} Out{1}", i, FileFormatUtil.imageTypeToExtension(drawingML.getImageData().getImageType()));
DrawingMLImageData imageData = drawingML.getImageData();
imageData.save("D:\\company\\pic\\" + imageFileName);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(drawingML);
builder.write("${image}");
drawingML.remove(); // There are some images that are not effective
}
}
doc.save("D:\\company\\newDemo.docx");
}发布于 2018-04-17 00:07:05
您正在使用旧版本的Aspose.Words。我建议您使用最新版本的Aspose.Words for Java 18.4。请将代码中的DrawingML替换为Shape,如下所示,以获得所需的输出。
Document doc = new Document(MyDir + "in.docx");
NodeCollection shapeCollection = doc.getChildNodes(NodeType.SHAPE, true);
for (int i = 0; i < shapeCollection.getCount(); i++) {
Shape shape = (Shape) shapeCollection.get(i);
if (shape.hasImage()) {
String imageFileName = java.text.MessageFormat.format(
"Image.ExportImages.{0} Out{1}", i, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
shape.getImageData().save("D:\\company\\pic\\" + imageFileName);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(shape);
builder.write("${image}");
shape.remove();
}
}
doc.save(MyDir + "output.docx");我与Aspose一起工作,作为开发人员的布道者。
https://stackoverflow.com/questions/49851287
复制相似问题