java aspose 加水印_使用Aspose.words for java去掉Word文档的水印(底图)

这段代码展示了如何使用Aspose.Words for Java库从Word文档中删除水印。通过创建一个`RemoveWordWatermark`类,代码遍历文档的各个部分,从所有页眉中移除水印形状。
摘要由CSDN通过智能技术生成

不说话,直接上代码

/**

* @CopyRright (c)2011: BrokenStone

* @Project: WordWatermark

* @File: RemoveWordWatermark.java

* @JDK version used: JDK1.6 @

* @Author: BrokenStone

* @Blog: http://sheng.javaeye.com)

* @Email: wdmsyf@yahoo.com

* @since: 2012-1-13

* @Ver: 1.0

*/

package com.iteye.sheng.utils.tools.office;

import java.awt.Color;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.net.URI;

import com.aspose.words.Document;

import com.aspose.words.HeaderFooter;

import com.aspose.words.HeaderFooterType;

import com.aspose.words.HorizontalAlignment;

import com.aspose.words.License;

import com.aspose.words.Paragraph;

import com.aspose.words.RelativeHorizontalPosition;

import com.aspose.words.RelativeVerticalPosition;

import com.aspose.words.Section;

import com.aspose.words.Shape;

import com.aspose.words.ShapeType;

import com.aspose.words.VerticalAlignment;

import com.aspose.words.WrapType;

/**

* @author ShengYoufu

*

*/

public class RemoveWordWatermark {

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

// Sample infrastructure.

//URI exeDir = RemoveWordWatermark.class.getResource("").toURI();

//String dataDir = new File(exeDir.resolve("../../Data")) + File.separator;

//Document doc = new Document(dataDir + "TestFile.doc");

//insertWatermarkText(doc, "CONFIDENTIAL");

//doc.save(dataDir + "TestFile Out.doc");

loadLicense();

Document doc = new Document("c:/with_watermark.doc");

removeWatermark(doc);

doc.save("c:/without_watermark.doc");

}

/**

*

* Inserts a watermark into a document.

*

* @param doc The input document.

* @param watermarkText Text of the watermark.

*

*/

private static void insertWatermarkText(Document doc, String watermarkText) throws Exception {

// Create a watermark shape. This will be a WordArt shape.

// You are free to try other shape types as watermarks.

Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

// Set up the text of the watermark.

watermark.getTextPath().setText(watermarkText);

watermark.getTextPath().setFontFamily("Arial");

watermark.setWidth(500);

watermark.setHeight(100);

// Text will be directed from the bottom-left to the top-right corner.

watermark.setRotation(-40);

// Remove the following two lines if you need a solid black text.

watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark

watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark

// Place the watermark in the page center.

watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

watermark.setWrapType(WrapType.NONE);

watermark.setVerticalAlignment(VerticalAlignment.CENTER);

watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

// Create a new paragraph and append the watermark to this paragraph.

Paragraph watermarkPara = new Paragraph(doc);

watermarkPara.appendChild(watermark);

// Insert the watermark into all headers of each document section.

for (Section sect : doc.getSections()) {

// There could be up to three different headers in each section, since we want

// the watermark to appear on all pages, insert into all headers.

insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);

insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);

insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);

}

}

/**

* 插入水印

* @param watermarkPara

* @param sect

* @param headerType

* @throws Exception

*/

private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception {

HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

if (header == null) {

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(sect.getDocument(), headerType);

sect.getHeadersFooters().add(header);

}

// Insert a clone of the watermark into the header.

header.appendChild(watermarkPara.deepClone(true));

}

/**

* 移除全部水印

* @param doc

* @throws Exception

*/

private static void removeWatermark(Document doc) throws Exception {

for (Section sect : doc.getSections()) {

// There could be up to three different headers in each section, since we want

// the watermark to appear on all pages, insert into all headers.

removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_PRIMARY);

removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_FIRST);

removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_EVEN);

}

}

/**

* 移除指定Section的水印

* @param sect

* @param headerType

* @throws Exception

*/

private static void removeWatermarkFromHeader(Section sect, int headerType) throws Exception {

HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

if (header != null) {

header.removeAllChildren();

}

}

/**

* 从Classpath(jar文件中)中读取License

*/

private static void loadLicense() {

//返回读取指定资源的输入流

License license = new License();

InputStream is = null;

try {

is = RemoveWordWatermark.class.getResourceAsStream("/resources/aspose.word.license.xml");

if(is==null)

throw new RuntimeException("Cannot find licenses file. Please contact wdmsyf@yahoo.com or visit http://sheng.javaeye.com for get more information.");

license.setLicense(is);

} catch (Exception ex) {

ex.printStackTrace();

}finally{

if(is!=null){

try{ is.close(); }catch(IOException ex){ };

is = null;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值