首页>代码>java通过Aspose.Word控件实现Word文档的操作>/AsposeWord/src/com/demo/AsposeDemo.java
package com.demo;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.aspose.words.ConvertUtil;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.HeaderFooter;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.HorizontalAlignment;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.License;
import com.aspose.words.Paragraph;
import com.aspose.words.RelativeHorizontalPosition;
import com.aspose.words.RelativeVerticalPosition;
import com.aspose.words.SaveFormat;
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;

public class AsposeDemo {

	/**
	 * license
	 * @return
	 */
	static{
		InputStream is = ConvertUtil.class.getClassLoader().getResourceAsStream("license.xml"); 
		License aposeLic = new License();
		try {
			aposeLic.setLicense(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
        try {
        	//开始时间
            long old = System.currentTimeMillis();
            File file1 = new File ("C:\\TEST\\Test001.doc");
            File file1_1 = new File ("C:\\TEST\\Test001.doc");
            File file2 = new File ("C:\\TEST\\Test001.pdf");
            File file3 = new File ("C:\\TEST\\Test001.rtf");
            //1、word转PDF
            word2Pdf(file1,file2);
            //2、word转RTF
            word2Rtf(file1,file3);
            //3、将doc2合并到doc1
            Document mergeDoc = mergeDoc(getDoc(file1),getDoc(file1_1));
            mergeDoc.save("C:\\TEST\\mergeDoc.doc",SaveFormat.DOC);
            //4、添加水印
            insertWatermarkText(getDoc(file1),"我愛北京天安門");
            getDoc(file1).save("C:\\TEST\\file1_2.doc",SaveFormat.DOC);
            //结束时间
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	/**
	 * word转PDF
	 * @param src 目标文件 
	 * @param result 保存文件
	 * @throws FileNotFoundException
	 * @throws Exception
	 */
	public static void word2Pdf(File src,File result) throws FileNotFoundException, Exception {
		Document doc = new Document(new FileInputStream(src));
		doc.save(result.getAbsolutePath(),SaveFormat.PDF);
	}
	
	/**
	 * 将doc2合并到doc1
	 * @param doc1
	 * @param doc2
	 * @throws Exception 
	 */
	public static Document mergeDoc(Document doc1, Document doc2) throws Exception{
		doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
		return doc1;
	}
	
	/**
	 * 转RTF
	 * @param src
	 * @param result
	 * @throws FileNotFoundException
	 * @throws Exception
	 */
	public static void word2Rtf(File src,File result) throws FileNotFoundException, Exception {
		Document doc = new Document(new FileInputStream(src));
		doc.save(result.getAbsolutePath(),SaveFormat.RTF);
	}
	
	/**
	 * 获得要操作的word文档对象
	 * @param src
	 * @return
	 * @throws Exception
	 */
	public static Document getDoc(File src) throws Exception{
		return new Document(new FileInputStream(src));
	}
	
	
	/**
	 * 整个doc添加水印
	 * @param doc 文档对象
	 * @param watermarkText 水印内容
	 * @throws Exception
	 */
	public static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
		Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
		watermark.getTextPath().setText(watermarkText);
		watermark.getTextPath().setFontFamily("宋体");
		watermark.setWidth(500);
		watermark.setHeight(100);
		watermark.setRotation(-40);
		watermark.getFill().setColor(Color.GRAY); 
		watermark.setStrokeColor(Color.GRAY); 
		watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
		watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
		watermark.setWrapType(WrapType.NONE);
		watermark.setVerticalAlignment(VerticalAlignment.CENTER);
		watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
		Paragraph watermarkPara = new Paragraph(doc);
		watermarkPara.appendChild(watermark);
		for (Section sect : doc.getSections()) {
			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) {
			header = new HeaderFooter(sect.getDocument(), headerType);
			sect.getHeadersFooters().add(header);
		}
		header.appendChild(watermarkPara.deepClone(true));
	}
	
	/**
	 * 移除全部水印
	 * @param doc
	 * @throws Exception
	 */
	public static void removeWatermark(Document doc) throws Exception {
		for (Section sect : doc.getSections()) {
			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();
		}
	}
	
	/**
	 * 向文档指定位置插入图片
	 * @param doc
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	private static void insertInMage(Document doc) throws Exception {
		DocumentBuilder builder2 = new DocumentBuilder(doc);
		BufferedImage image = ImageIO.read(new FileInputStream("C:/Users/Administrator/Desktop/测试/eplugger.png")); 
		//输出图象文件二进制数制 
		builder2.moveToBookmark("userimage");
		builder2.insertImage(image, 90, 90);
	}
	
    
	

}
最近下载更多
lmroman  LV8 4月2日
wusiyin  LV14 2022年9月1日
xueying049  LV5 2022年5月9日
x2b2d2  LV12 2021年8月4日
1439107348  LV1 2020年10月27日
慵懒的小橘猫  LV11 2020年5月21日
gxpcwm  LV22 2019年7月23日
1105570390  LV8 2019年7月22日
news_2019  LV2 2019年7月3日
123456fdfdsf  LV4 2019年5月20日
最近浏览更多
lmroman  LV8 4月2日
467277  LV13 3月26日
akittyboy  LV9 2月25日
neuwxr2015  LV8 2023年2月7日
1358849392  LV21 2022年11月11日
li_xiangquan 2022年11月9日
暂无贡献等级
wusiyin  LV14 2022年9月1日
魔幻男灵  LV8 2022年6月12日
xueying049  LV5 2022年5月9日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友