`

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

 
阅读更多

不说话,直接上代码

 

/**
 * @CopyRright (c)2011: BrokenStone
 * @Project: WordWatermark
 * @File: RemoveWordWatermark.java
 * @JDK version used: JDK1.6 @<br/>
 * @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;
	    }
	  }
	}

}

 

 

 

分享到:
评论
2 楼 jvincent 2012-06-13  
没有licenses,除了有那个文字,还有其他的影响吗?
1 楼 cola.ye 2012-03-05  
没有 licenses file,
word文件都会有
Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd.
求一份licenses file,
yekeri@163.com  

相关推荐

    使用Aspose.Words for Java完成复杂Word与PDF的导出

    Aspose.Words for Java是一款强大的文档处理库,它允许开发者在Java环境中创建、编辑、转换和打印Microsoft Word文档,甚至可以处理PDF格式。这个工具以其高效性和灵活性在IT行业中受到广泛应用,尤其对于需要进行...

    Aspose.Words for Java 20.7

    Aspose.Words for Java 是一个强大的Java库,专门用于处理和操作Microsoft Word文档。这个库允许开发者在Java应用程序中创建、修改、提取内容和属性、插入图片、图表等,而不需要在服务器上安装Microsoft Office。...

    使用Aspose.Words for Java完成复杂Word与PDF的导出Demo

    使用Aspose.Words for Java完成复杂Word与PDF的导出Demo使用Aspose.Words for Java完成复杂Word与PDF的导出Demo使用Aspose.Words for Java完成复杂Word与PDF的导出Demo

    Aspose.Words for .NET 18.7(.net+.net core) 去水印,学习使用

    Aspose.Words for .NET 18.7是一款强大的文档处理库,专为.NET和.NET Core框架设计,用于在各种应用程序中实现高级文档操作,包括创建、编辑、转换和渲染Microsoft Word文档。在这个版本中,重点是去水印功能,这...

    Aspose.Words_生成excel_aspose.word_生成word_aspose.words_

    虽然Aspose.Words主要用于处理Word文档,但通过Aspose.Cells,两者可以结合使用,实现从Word文档中提取数据并将其格式化为Excel表格,或者将Excel数据导入Word文档中,生成报表或合并信件。这种结合使用方式在数据...

    Word文档加水印(利用Aspose.Words.dll)

    本项目聚焦于使用C#编程语言,在WPF(Windows Presentation Foundation)环境中,结合Aspose.Words.dll库来实现Word文档的水印添加以及接受修订功能。下面将详细介绍这个过程及其相关知识点。 首先,Aspose.Words....

    aspose.word for java 无水印无页数限制

    Aspose.Word for Java是一款强大的Java库,专门用于处理Microsoft Word文档。它允许开发者在Java应用程序中执行各种复杂的Word文档操作,而无需依赖Microsoft Office。这个库不仅提供了无水印和无页数限制的功能,还...

    AsposeWord for java 23.2

    Aspose.Words for Java是Aspose公司开发的一款强大的文档处理库,主要针对Java开发者,用于在应用程序中创建、编辑、转换和打印各种文档格式。在23.2这个版本中,用户可以享受到一系列的更新和改进,使得文档操作...

    aspose.words for java解决.doc文档转为.docx问题

    使用破解后的aspose.words for java,实现.doc文档转为.docx问题,不丢失任何文档内容

    aspose.words for java 破解包

    aspose包已破解 无水印,无行数限制!需在代码中加载证书使用,压缩包中已含有加载代码,加载代码建议写在构造器中!请勿用于商业用途,如有版权纠纷,概不负责!

    Aspose.Words-for-Java, 用于Java示例的Aspose.Words,插件和展示.zip

    Aspose.Words-for-Java, 用于Java示例的Aspose.Words,插件和展示 面向Java的Java Aspose.Words 是一个高级的Java字处理 API,可以让你在Java应用程序中直接执行大量文档处理任务。 Java API的Aspose.Words 支持处理...

    Aspose.Words for NET根据word模板创建文档Demo源码

    在“Aspose.Words for .NET根据word模板创建文档Demo源码”中,我们可以通过分析源代码来学习如何利用Aspose.Words的功能根据模板文件生成新的Word文档。 1. **模板文件的理解与使用** 模板文件通常是一个预先设计...

    aspose.words操作word 一些关键方法

    在IT行业中,Aspose.Words是一款非常强大的文档处理组件,它允许开发者在不依赖Microsoft Office的情况下,进行Word文档的创建、阅读、修改和转换。这个资源聚焦于利用Aspose.Words进行一系列的关键操作,包括插入...

    Aspose.Word使用说明文档.pdf

    Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式。使用Aspose.Words可以在不使用Microsoft.Word的情况下生成、修改、转换和打印文档,本文档对Aspose.Words的一些操作进行了说明

    Aspose.Words帮助API

    4. 模板填充:Aspose.Words支持数据绑定,允许开发人员使用数据源(如数据库、XML文件)填充模板中的占位符,快速生成批量文档。 5. 文档比较:API提供了文档比较功能,可以检测两个版本的文档之间的差异,并生成...

    Aspose.Words.Cpp_18.11.zip

    Aspose.Words是一款强大的文档处理库,主要用于在C++编程环境中创建、操作和转换Microsoft Word文档。这个"Aspose.Words.Cpp_18.11.zip"压缩包包含的资源显然是Aspose.Words库的C++版本,版本号为18.11,允许开发者...

    Aspose.Words For .NET 生成word和pdf 支持模板关键字替换图片替换

    Aspose.Words for .NET 是一个强大的文档处理库,它允许开发者在.NET环境中创建、编辑、格式化和转换Microsoft Word文档以及PDF文件。这个库以其高效性和灵活性而著名,能够帮助程序员实现各种复杂的文档操作,比如...

    【连载】Aspose.Words使用教程之插入文档元素(一)

    ### Aspose.Words 使用教程之插入文档元素(一) #### 插入文本的字符串 在Aspose.Words中,为了向文档中插入文本,我们主要使用`DocumentBuilder.Write`方法。这种方法不仅简单直接,而且非常灵活,允许用户指定...

    Aspose.Words for .NET 9.7.0 官方同步破解版

    简单破解Aspose.Words for .NET 9.7.0 官方更新时间为02-06-2011,只是去除未注册情况下文档中的"Evaluation Only. Created with Aspose.Words. Copyright 2003-2010 Aspose Pty Ltd."字样.其他地方未作修改,因此你不...

    Aspose.Words根据word模板写入数据和图表(chart)

    在本文中,我们将深入探讨如何使用Aspose.Words根据Word模板写入数据和插入图表,特别是饼形图和柱形图。 首先,理解模板替换的基础是关键。Aspose.Words支持通过使用书签或域来定义模板中的可替换区域。在模板文档...

Global site tag (gtag.js) - Google Analytics