前面我们介绍了如何使用spire.doc for java 。当word 文档中的某些部分,如段落,文本域text range, 表格,文本框和图形设置了背景色时,会出现水印被遮挡现象。该示例将详细介绍在java应用程序中,给word文档添加图片水印时移除段落,文本域,表格和文本框的背景色。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;
public class wordwatermark {
public static void main(string[] args) {
//加载文档
document document = new document("sample.docx");
//移除背景色
setnofillforparagraphsandtablesandtextboxes(document);
//插入图片水印
insertimagewatermark(document);
//保存文档
string output = "result.docx";
document.savetofile(output, fileformat.docx);
}
public static void insertimagewatermark(document document) {
picturewatermark picture = new picturewatermark();
picture.setpicture("logo.jpg");
picture.setscaling(250);
picture.iswashout(false);
document.setwatermark(picture);
}
static void setnofillforparagraphsandtablesandtextboxes(document document) {
section section;
paragraph paragraph;
textrange textrange;
for (int i = 0; i < document.getsections().getcount(); i ) {
section = document.getsections().get(i);
//移除段落中的背景色
for (int j = 0; j < section.getbody().getparagraphs().getcount(); j ) {
paragraph = section.getbody().getparagraphs().get(j);
removebackgroudcolorofparagraph(paragraph);
}
table table;
//移除表格中的背景色
for (int t = 0; t < section.getbody().gettables().getcount(); t ) {
table = section.getbody().gettables().get(t);
table.gettableformat().clearbackground();
for(int r=0;r
效果图: