当前位置: 主页 > JAVA语言

java编写程序输出图形-java编写图形界面程序

发布时间:2023-03-24 07:13   浏览次数:次   作者:佚名

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);
    }
}

输出:

java编写图形界面程序_java编写程序输出图形_图形程序编写