Java使用poi给Word加水印(目前自己了解的仅支持后缀为.docx格式的,.doc仍在研究)开源、免费。

废话不多说,直接上代码!

1、pom依赖:

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.2</version>
    </dependency>
</dependencies>

 

2、代码:
import java.io.*;

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

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

public class Test1 {

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

        //输入的docx文档
        InputStream in = new FileInputStream(new File("D:/aa.docx"));
        XWPFDocument doc= new XWPFDocument(in);

        // 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();

        // 水印内容
        headerFooterPolicy.createWatermark("WaterMaker");

        // 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);
        }
        //文件输出地址
        FileOutputStream out = new FileOutputStream("D:\\watermark.docx");
        System.out.println("水印添加成功!");
        doc.write(out);
        out.close();
        doc.close();
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Apache POI库来解析Word文档,POI支持读取和写入.doc和.docx格式Word文档。以下是一个简单的Java代码示例,演示如何使用POI库来读取Word文档中的标题、内容和表格,并为它们添自定义样式。请注意,这只是一个示例代码,您需要根据实际情况进行修改和优化。 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFTableCell; public class WordParser { public static void parse(String inputFilePath, String outputFilePath) throws IOException { if (inputFilePath.endsWith(".doc")) { parseDocFile(inputFilePath, outputFilePath); } else if (inputFilePath.endsWith(".docx")) { parseDocxFile(inputFilePath, outputFilePath); } else { throw new IllegalArgumentException("Unsupported file format"); } } private static void parseDocFile(String inputFilePath, String outputFilePath) throws IOException { FileInputStream inputStream = new FileInputStream(inputFilePath); HWPFDocument document = new HWPFDocument(inputStream); Range range = document.getRange(); FileOutputStream outputStream = new FileOutputStream(outputFilePath); for (int i = 0; i < range.numParagraphs(); i++) { Paragraph paragraph = range.getParagraph(i); String text = paragraph.text(); if (paragraph.isInTable()) { Table table = range.getTable(paragraph); for (int j = 0; j < table.numRows(); j++) { TableRow row = table.getRow(j); for (int k = 0; k < row.numCells(); k++) { TableCell cell = row.getCell(k); String cellText = cell.getParagraph(0).text(); // add custom style to cellText cell.getParagraph(0).setStyle("CustomCellStyle"); } } } else if (text.startsWith("Heading")) { // add custom style to heading paragraph.setStyle("CustomHeadingStyle"); } else { // add custom style to content paragraph.setStyle("CustomContentStyle"); } range.getParagraph(i).writeReplace(paragraph); } document.write(outputStream); outputStream.close(); inputStream.close(); } private static void parseDocxFile(String inputFilePath, String outputFilePath) throws IOException { FileInputStream inputStream = new FileInputStream(inputFilePath); XWPFDocument document = new XWPFDocument(inputStream); FileOutputStream outputStream = new FileOutputStream(outputFilePath); for (XWPFParagraph paragraph : document.getParagraphs()) { String text = paragraph.getText(); if (paragraph.getBody().getTables().size() > 0) { for (XWPFTable table : paragraph.getBody().getTables()) { for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { String cellText = cell.getText(); // add custom style to cellText cell.getParagraphs().get(0).setStyle("CustomCellStyle"); } } } } else if (text.startsWith("Heading")) { // add custom style to heading paragraph.setStyle("CustomHeadingStyle"); } else { // add custom style to content paragraph.setStyle("CustomContentStyle"); } } document.write(outputStream); outputStream.close(); inputStream.close(); } } ``` 在上面的代码中,我们使用了两个不同的类来处理.doc和.docx文件格式。对于.doc文件,我们使用HWPFDocument和Range类来获取文档的段落、表格等信息;对于.docx文件,我们使用XWPFDocument、XWPFParagraph、XWPFTable等类来获取文档的段落、表格等信息。在解析过程中,我们可以根据文本内容的特征,如标题、表格等,为它们添自定义样式。最终,我们将修改后的文档写入到输出文件中。 请注意,在实际使用中,您需要确保您的Word文档结构良好,例如,标题应该使用Word的标题样式,而不是手动粗或者大字体;表格应该使用Word的表格工具,而不是手动绘制表格。否则,解析器可能无法正确识别文档中的各个部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值