Word 中设置水印效果时,不论是文本水印或者是 图片水印 都只能添加单个文字或者图片到Word页面,效果比较单一,本文通过 Java 代码示例介绍如何在页面中添加多行图片水印效果,即水印效果以多个图片平铺到页面。( 添加多行文字水印效果,可以查看这篇文章中的方法 )
程序环境:使用spire.doc.jar,版本: 3.9.0
Java代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextWrappingStyle; import com.spire.doc.fields.DocPicture;
public class ImageWatermark { public static void main(String[] args) { //加载Word文档 Document doc= new Document(); doc.loadFromFile( "input.docx" );
//加载图片 DocPicture picture = new DocPicture(doc); picture.loadImage( "logo.png" ); picture.setTextWrappingStyle(TextWrappingStyle.Behind); //设置图片环绕方式
//遍历所有section for ( int n = 0 ; n < doc.getSections().getCount(); n++) { Section section = doc.getSections().get(n);
//获取section的页眉 HeaderFooter header = section.getHeadersFooters().getHeader();
Paragraph paragrapg1; //获取或添加段落 if (header.getParagraphs().getCount()> 0 ) { paragrapg1 = header.getParagraphs().get( 0 ); } else { paragrapg1 = header.addParagraph(); }
//复制图片,并添加图片到段落 for ( int p = 0 ; p < 4 ; p++) { for ( int q = 0 ; q < 3 ; q++) { picture = (DocPicture)picture.deepClone(); picture.setVerticalPosition( 50 + 150 * p); picture.setHorizontalPosition( 10 + 140 * q); paragrapg1.getChildObjects().add(picture); } } }
//保存文档 doc.saveToFile( "output.docx" , FileFormat.Docx_2013); doc.dispose(); } } |
到此这篇关于Java在Word中添加多行图片水印的文章就介绍到这了,更多相关Java添加图片水印内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://HdhCmsTestcnblogs测试数据/Yesi/p/14392762.html
查看更多关于Java在Word中添加多行图片水印的详细内容...