当前位置: 首页 > 知识库问答 >
问题:

使用Apache POI在word文档中添加水印

闻人河
2023-03-14

共有1个答案

许博达
2023-03-14

私有的XWPFPagaingetWaterMarkPagain(String text,int idx)直到现在才完成。您可以等待它准备好,或者在创建默认值之后使用低级对象来操作它。

需要的设置在CTShape中。

示例:

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

public class CreateWordHeaderFooterWatermark {

 public static void main(String[] args) throws Exception {

  XWPFDocument doc= new XWPFDocument();

  // the body content
  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The Body:");

  // create header-footer
  XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
  if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

  // create default Watermark - fill color black and not rotated
  headerFooterPolicy.createWatermark("Watermark");

  // get the default header
  // Note: createWatermark also sets FIRST and EVEN headers 
  // but this code does not updating those other headers
  XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
  paragraph = header.getParagraphArray(0);

  // get com.microsoft.schemas.vml.CTShape where fill color and rotation is set
  org.apache.xmlbeans.XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(
    new javax.xml.namespace.QName("urn:schemas-microsoft-com:vml", "shape"));

  if (xmlobjects.length > 0) {
   com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape)xmlobjects[0];
   // set fill color
   ctshape.setFillcolor("#d8d8d8");
   // set rotation
   ctshape.setStyle(ctshape.getStyle() + ";rotation:315");
   //System.out.println(ctshape);
  }

  doc.write(new FileOutputStream("CreateWordHeaderFooterWatermark.docx"));
  doc.close();

 }
}
 类似资料:
  • 本文向大家介绍C#如何给word文档添加水印,包括了C#如何给word文档添加水印的使用技巧和注意事项,需要的朋友参考一下 和PDF一样,在word中,水印也分为图片水印和文本水印,给文档添加图片水印可以使文档变得更为美观,更具有吸引力。文本水印则可以保护文档,提醒别人该文档是受版权保护的,不能随意抄袭。前面我分享了如何给PDF添加水印,有人问我如何给word文档添加水印,今天正好有空,就记录一下

  • 我正在尝试创建一个包含多列的word文档。这样做(而不是使用表)的原因是,数据将跨越多个页面,在添加到新页面之前,我只能用列填充整个页面。 可以用ApachePOI实现吗?谢谢

  • 我想使用openxml在word文档中添加一个水平形状。 我试图使用OpenXML生产力工具粘贴代码,但在打开生成的文件时出现了一些错误http://schemas.microsoft.com/office/word/2010/wordml:anchorId“属性未声明)

  • 我需要一个表格,第一行和第二行的单元格合并在一起。 大概是这样的: 桌子的图片(我不能张贴图片)http://i.stack.imgur.com/dAO6j.png 我一直在复习与本主题相关的所有问题,并找到了一些将网格跨度应用于单元的答案,但我找不到真正的解决方案。 以下是我从谷歌和本网站获得的示例代码: 我从这段代码中得到的信息如下: 我试图用

  • 在使用ApachePOI写入word文档时,对于必须根据某个“计数”动态复制段落的位置,这些段落的标题必须按照章节顺序编号。假设它是word文档中的第7节,那么其中每个段落的标题应按顺序编号为7.1、7.2等。 尽管我看到了一些解决方案,其中考虑了word文档的xml,并使用了XWPFN编号。 还有其他更简单的方法吗? 或 如果我的word模板已经有了一个带有编号标题的段落,并且我必须多次复制相同