java aspose.words_java通过Aspose.Word控件实现Word文档的操作_bjackzjack的博客-CSDN博客 - Aspose.Word

package com.demo;

import java.awt.Color;

import java.awt.image.BufferedImage;

import java.io.FileInputStream;

import java.io.InputStream;

import javax.imageio.ImageIO;

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.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;

/**

*

* 由于ASPOSE比较吃内存,操作大一点的文件就会堆溢出,所以请先设置好java虚拟机参数:-Xms512m -Xmx512m(参考值)

* 如有疑问,请在CSDN下载界面留言,或者联系QQ569925980

*

* @author Spark

*

*/

public class TestWord {

private static InputStream license;

private static InputStream word;

/**

* 获取license

*

* @return

*/

public static boolean getLicense() {

boolean result = false;

try {

license = TestWord.class.getClassLoader().getResourceAsStream("\license.xml");// license路径

word = TestWord.class.getClassLoader().getResourceAsStream("\test_image.doc");// 原始word路径

License aposeLic = new License();

aposeLic.setLicense(license);

result = true;

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

*

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

}

}

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

}

/**

*

* @param args

*/

public static void main(String[] args) {

// 验证License

if (!getLicense()) {

return;

}

try {

long old = System.currentTimeMillis();

Document doc = new Document(word);

insertInMage(doc);

doc.save("C:\without_watermark.doc", SaveFormat.DOC);

long now = System.currentTimeMillis();

// System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath());

} catch (Exception e) {

e.printStackTrace();

}

}

}


原网址: 访问
创建于: 2023-05-08 18:21:42
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论