如何使用Java将多个图像水印添加到Word文档?试试这款小众工具

Spire.Doc for Java 是一款专业的Java Word组件,开发人员使用它可以轻松地将Word文档创建、读取、编辑、转换和打印等功能集成到自己的Java应用程序中。

本文将向您展示如何借助Spire.Doc for Java将多个图像水印添加到Word文档中。可下载最新版测试。

示例代码如下:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

public class WordImageWatermark {

    public static void main(String[] args) throws Exception {
        //Load the sample file
        Document doc=new Document();
        doc.loadFromFile("Sample.docx");
        //Load the image
        DocPicture picture = new DocPicture(doc);
        picture.loadImage("Logo.png");

        //Set the text wrapping style
        picture.setTextWrappingStyle(TextWrappingStyle.Behind);

        for (int n = 0; n < doc.getSections().getCount(); n++) {
            Section section = doc.getSections().get(n);
            //Get the head of section
            HeaderFooter header = section.getHeadersFooters().getHeader();
            Paragraph paragrapg1;
            if(header.getParagraphs().getCount()>0){
                paragrapg1=header.getParagraphs().get(0);

            }else {
                //Add the header to the paragraph
                paragrapg1 = header.addParagraph();
            }

            for (int p = 0; p < 3; p++) {

                for (int q = 0; q < 2; q++) {
                    //copy the image and add it to many places
                    picture = (DocPicture)picture.deepClone();
                    picture.setVerticalPosition(100 + 200 * p);
                    picture.setHorizontalPosition(50 + 210 * q);
                    paragrapg1.getChildObjects().add(picture);
                }
            }
        }
        //Save the document to file
        doc.saveToFile("Result.docx", FileFormat.Docx_2013);
    }
}

输出:

Word格式处理控件Spire.Doc功能演示:将 Word 转换为受密码保护的 PDF 文档


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值