这是我的代码,想要用java给word添加平铺水印,代码不报错,但是水印没加上,只有doc那个红头文字,求帮助,急
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.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class multiTextWatermark {
public static void main(String[] args) {
//加载示例文档
Document doc = new Document();
doc.loadFromFile("C:\Users\Test1\Desktop\Sample.docx");
//添加艺术字并设置大小
ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
shape.setWidth(60);
shape.setHeight(20);
//设置艺术字文本内容、颜色,位置及样式
shape.setVerticalPosition(30);
shape.setHorizontalPosition(20);
shape.setRotation(315);
shape.getWordArt().setText("内部使用");
shape.setFillColor(Color.green);
shape.setLineStyle(ShapeLineStyle.Single);
shape.setStrokeColor(new Color(192, 192, 192, 255));
shape.setStrokeWeight(1);
Section section;
HeaderFooter header;
for (int n = 0; n < doc.getSections().getCount(); n++) {
section = doc.getSections().get(n);
//获取section的页眉
header = section.getHeadersFooters().getHeader();
Paragraph paragraph1;
for (int i = 0; i < 4; i++) {
//添加段落到页眉
paragraph1 = header.addParagraph();
for (int j = 0; j < 3; j++) {
//复制艺术字并设置多行多列位置
shape = (ShapeObject) shape.deepClone();
shape.setVerticalPosition(50 + 150 * i);
shape.setHorizontalPosition(20 + 160 * j);
paragraph1.getChildObjects().add(shape);
}
}
}
//保存文档
doc.saveToFile("output/multi-lineTextwatermark.docx", FileFormat.Docx_2013);
}
}
增加一行,字体的设定:
shape.getWordArt().setFontFamily("宋体");
效果:
在Word 2010中,通过简单的鼠标单击即可为文档添加水印,但这样只能在每一个文档页面内添加一个水印,这个在前面的文章已经有所介绍,具体请见:Word文档添加内置水印、个性化图片及文字水印的方法,但按照前面的方法即使多次执行添加水印的命令,也只能保留最后一次添加的水印,怎样才能在页面内添加多个水印呢?要到达到让水印铺满整个Word页面的效果,可以按照。
图1 添加一个自定义水印
2、在文档页面中添加一个水印后,双击页眉或页脚进入页眉页脚编辑状态,此时即可使用鼠标选中文档中的水印,然后进行移动、缩放、旋转、复制、粘贴等操作即可。
另外,当文档中的水印处于选中状态时,功能区中将显示“艺术字工具”,通过使用其下的“格式”上下文选项卡,可对水印进行各项编辑操作,例。
图2 “艺术字工具”的“格式”上下文选项卡
3、根据需要缩小水印,并进行复制,多次粘贴,将每一个水印移动到适当位置,最终达到水印铺满整个页面的效果。
4、双击正文任意位置,退出页眉页脚编辑状态,水印编辑完成,。
图3 水印铺满整个页面
更多相关阅读
如何删除Word 2010库中的自定义页眉
Word 2010中的橡皮擦工具在哪?Word 2010中的橡皮擦工具怎么用
Word 2010中使用快速访问工具栏添加自定义命令按钮的方法
如何在Word 2010文档中进行复制、剪切和粘贴操作
Word 2010文档自动摘录出子标题做为各章节的摘要文字的方法
参看Spire.Doc官方文档:Java 添加多行文字水印到 Word 文档
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class WordWatermark {
public static void main(String[] args) {
//加载示例文档
Document doc = new Document();
doc.loadFromFile("Sample.docx");
//添加艺术字并设置大小
ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
shape.setWidth(60);
shape.setHeight(20);
//设置艺术字文本内容、位置及样式
shape.setVerticalPosition(30);
shape.setHorizontalPosition(20);
shape.setRotation(315);
shape.getWordArt().setFontFamily("宋体");
shape.getWordArt().setText("内部使用");
shape.setFillColor(Color.red);
shape.setLineStyle(ShapeLineStyle.Single);
shape.setStrokeColor(new Color(192, 192, 192, 255));
shape.setStrokeWeight(1);
Section section;
HeaderFooter header;
for (int n = 0; n < doc.getSections().getCount(); n++) {
section = doc.getSections().get(n);
//获取section的页眉
header = section.getHeadersFooters().getHeader();
Paragraph paragraph;
if (header.getParagraphs().getCount() > 0) {
//如果页眉有段落,取它第一个段落
paragraph = header.getParagraphs().get(0);
} else {
//否则新增加一个段落到页眉
paragraph = header.addParagraph();
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
//复制艺术字并设置多行多列位置
shape = (ShapeObject) shape.deepClone();
shape.setVerticalPosition(50 + 150 * i);
shape.setHorizontalPosition(20 + 160 * j);
paragraph.getChildObjects().add(shape);
}
}
}
//保存文档
doc.saveToFile("result.docx", FileFormat.Docx_2013);
}
}