Aspose.Java实现word转pdf,添加水印等操作
- 一. word转pdf
- 二. 文档插入水印
Aspose是一款商用版控件,支持各类文档操作,这里主要介绍如何在Springboot项目中使用破解版。
一. word转pdf
- 在项目中导入aspose.word包 百度网盘下载 提取码:pokb
- 新建一个asposeUtil类,类中建立如下静态方法
/*** @param wordPath 需要被转换的word全路径带文件名* @param pdfPath 转换之后pdf的全路径带文件名*/public static void doc2pdf(String wordPath, String pdfPath) {if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生return;}try {long old = System.currentTimeMillis();File file = new File(pdfPath); //新建一个pdf文档FileOutputStream os = new FileOutputStream(file);Document doc = new Document(wordPath); //Address是将要被转化的word文档doc.save(os, com.aspose.words.SaveFormat.PDF);long now = System.currentTimeMillis();os.close();
// System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时} catch (Exception e) {e.printStackTrace();}}
- 新建license.xml文件(放入Springboot项目的resources目录下)
<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
存放位置如图所示
4. 建立getLicense()方法
private static boolean getLicense() {boolean result = false;try {InputStream is = asposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); // 如果是普通java项目(非Springboot项目)license.xml应放在..\WebRoot\WEB-INF\classes路径下System.out.println(is);License aposeLic = new License();aposeLic.setLicense(is);
// com.aspose.cells.License cell = new com.aspose.cells.License();
// cell.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}
- 之后我们直接通过
asposeUtil.doc2pdf(wordPath, pdfPath)
即可完成转换
二. 文档插入水印
插入水印的方法与上面大致相同,下面给出插入水印具体代码
/*** 为word文档添加水印* @param doc word文档模型* @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(100);//水印高度watermark.setHeight(20);//旋转水印watermark.setRotation(-40);//水印颜色 浅灰色watermark.getFill().setColor(Color.lightGray);watermark.setStrokeColor(Color.lightGray);//设置相对水平位置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);}System.out.println("Watermark Set");}/*** 在页眉中插入水印* @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){header = new HeaderFooter(sect.getDocument(), headerType);sect.getHeadersFooters().add(header);}header.appendChild(watermarkPara.deepClone(true));}/*** 设置水印属性* @param doc* @param wmText* @param left* @param top* @return* @throws Exception*/public static Shape ShapeMore(Document doc, String wmText, double left, double top)throws Exception{
// Shape waterShape = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);Shape waterShape = new Shape(doc, ShapeType.IMAGE);waterShape.getImageData().setImage(wmText);waterShape.setWidth(100.0);waterShape.setHeight(100.0);waterShape.setRotation(0);waterShape.setFilled(true);
// //水印内容
// waterShape.getTextPath().setText(wmText);
// //水印字体
// waterShape.getTextPath().setFontFamily("宋体");
// //水印宽度
// waterShape.setWidth(40);
// //水印高度
// waterShape.setHeight(13);
// //旋转水印
// waterShape.setRotation(-40);//水印颜色 浅灰色/*waterShape.getFill().setColor(Color.lightGray);waterShape.setStrokeColor(Color.lightGray);*/waterShape.setStrokeColor(new Color(210,210,210));//将水印放置在页面中心waterShape.setLeft(left);waterShape.setTop(top);//设置包装类型waterShape.setWrapType(WrapType.NONE);return waterShape;}/*** 插入多个水印* @param mdoc* @param wmText* @throws Exception*/public static void WaterMarkMore(Document mdoc, String wmText)throws Exception{Paragraph watermarkPara = new Paragraph(mdoc);
// for (int j = 0; j < 500; j = j + 100)
// {
// for (int i = 0; i < 700; i = i + 85)
// {
// Shape waterShape = ShapeMore(mdoc, wmText, j, i);
// watermarkPara.appendChild(waterShape);
// }
// }Shape waterShape = ShapeMore(mdoc, wmText, 155, 300);watermarkPara.appendChild(waterShape);for (Section sect : mdoc.getSections()){insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);}}