aspose 插入图片的方法

给自己记录一下:

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.SaveFormat;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class WordDemo {
    public static void main(String[] args) throws Exception {
        test();
    }

    private static void test() throws Exception {
        byte[] bytes = Files.readAllBytes(Paths.get("path"));
        
        Document document = new Document();
        DocumentBuilder documentBuilder = new DocumentBuilder(document);
        documentBuilder.insertImage(bytes, 0, 0, 0, 0, 0, 0, 3);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        document.save(outputStream, SaveFormat.DOCX);

        FileOutputStream fileOutputStream = new FileOutputStream("test.docx");
        fileOutputStream.write(outputStream.toByteArray());
    }
}
        

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Aspose.Words插入图片到列表中,可以按照以下步骤进行操作: 1. 首先,确保已经正确加载了Aspose.Words库,并且已经创建了一个Document对象来处理Word文档。 2. 然后,找到你希望在其中插入图片的列表项。可以使用Document的Sections属性来获取文档中的所有节(Sections),并使用Sections的Body属性来获取每个节的正文(Body)。 3. 利用正文对象(Body)的Paragraphs属性来获取列表项(Paragraph)集合。你可以通过循环来遍历所有的列表项。 4. 对于每个列表项,使用Paragraph的Runs属性来获取每个列表项中的Runs集合(Run是指文本在文档中的一段,可以包含格式信息)。 5. 确定插入图片的位置,通常是在列表项的最后一个Run之后。可以使用列表项的Runs集合中的最后一个Run来确定位置。 6. 创建一个新的Run对象,并设置它的文本为插入图片的位置。可以使用Run的ParentParagraph属性获取到插入图片位置上的段落。 7. 接下来,创建一个Shape对象,用于插入图片。可以使用DrawingML的Body静态方法创建一个DrawingML格式的图片。 8. 把图片创建为DrawingML格式后,可以使用Shape对象的属性来设置图片的相关属性,例如图片的路径、大小、位置等。 9. 使用ParentParagraph对象的InsertAfter方法来在图片位置之后插入Shape对象。 10. 最后,保存并关闭Word文档。 通过以上步骤,你可以使用Aspose.Words在列表中插入图片。请注意每个步骤的具体代码实现需要根据你的具体需求进行编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值