首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

运用过滤器移除Word文件水印

2012-09-02 
使用过滤器移除Word文件水印还是移除Word水印的问题,这次需要在应用系统上实现用户请求Word文件时,自动把W

使用过滤器移除Word文件水印

还是移除Word水印的问题,这次需要在应用系统上实现用户请求Word文件时,自动把Word的水印移除。这次的重点不是如何移除Word水印的问题,而是如何实现在response发送给客户端之前对数据流进行修改。实现的方式是实现自己的HttpServletResponseWrapper类并进行相应的修改操作。

直接上代码:

?

?

?

?

?

?

/** * @CopyRright (c)2011: BrokenStone * @Project: WordWatermark * @File:    WordWatermarkUtil.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.office.tools;import java.awt.Color;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;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.SectionCollection;import com.aspose.words.Shape;import com.aspose.words.ShapeType;import com.aspose.words.VerticalAlignment;import com.aspose.words.WrapType;/** * @author ShengYoufu * */public class WordWatermarkUtil {/** *  * Inserts a watermark into a document. *  * @param doc The input document. * @param watermarkText Text of the watermark. *  */public 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 watermarkwatermark.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.SectionCollection sects = doc.getSections();Iterator it = sects.iterator();while(it.hasNext()){Section sect = (Section)it.next();// 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 */public 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 inStream * @throws Exception */public static void removeWatermark(InputStream inStream) throws Exception {Document doc = new Document(inStream);removeWatermark(doc);}/** * 移除全部水印 * @param doc * @throws Exception */public static void removeWatermark(Document doc) throws Exception {SectionCollection sects = doc.getSections();Iterator it = sects.iterator();while(it.hasNext()){Section sect = (Section)it.next();// 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 */public static void removeWatermarkFromHeader(Section sect, int headerType) throws Exception {HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);if (header != null) {header.removeAllChildren();}}/** * 从Classpath(jar文件中)中读取License */public static void loadLicense() {  //返回读取指定资源的输入流  License license = new License();  InputStream is = null;  try {    is = WordWatermarkUtil.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;    }  }}/** * @param args */public static void main(String[] args) throws Exception {loadLicense();Document doc = new Document("c:/with_watermark.doc");removeWatermark(doc);doc.save("c:/without_watermark.doc");}}

?

1 楼 duohuoteng 2012-03-08   aspose.words java 用收费哎,有破解办的吗

热点排行