java实现word转pdf实现在线预览(可以带水印)

硅谷探秘者 2442 0 0

java实现word转pdf实现在线预览(可以带水印)

使用之前需要两个jar包的依赖

文件下载地址:http://www.jiajiajia.club/file/info/4uwe2d/111

Java代码实现
package test.pdftest;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.aspose.words.Document;
import com.aspose.words.HeaderFooter;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.HorizontalAlignment;
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;
/**
 * 	world转pdf实现在线预览
 * @author 硅谷探秘者(jia)
 *
 */
public class Test {
	public static void main(String[] args) {
		doc2pdf("E:\\test\\files\\a.docx","E:\\test\\files\\a.pdf");
	}
	
	public static void doc2pdf(String inPath, String outPath) {
	      FileOutputStream os =null;
	    try {
	      File file = new File(outPath); // 新建一个空白pdf文档
	      os = new FileOutputStream(file);
	      Document doc = new Document(inPath); // Address是将要被转化的word文档
	      insertWatermarkText(doc,"贾佳佳");//带水印,如果不需要直接注释掉就行了
	      doc.save(os, SaveFormat.PDF);
	    } catch (Exception e) {
	      e.printStackTrace();
	    }finally{
	        if(os!=null){
	            try {
	                    os.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	        }
	    }
	  }
	
	/**
	* @Description: PDF生成水印
	* @throws
	*/
	private 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.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");
	}
	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));
	}
}	
实现效果

 


评论区
请写下您的评论...
暂无评论...
猜你喜欢
jar依赖 2357 javaworldpdf所需jar包-pdf线一、依赖文件二、java代码packagetest.pdftest;importjava.awt.Color
其他 730 Aspose  Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,主要提供.net跟java两个开发语言的控件套包,通过它,有计划地操纵一些商业中最流行的文件格
工具 5638 java完美htmlpdf1.pom依赖:dependencygroupIdcom.itextpdf/groupIdartifactIditextpdf
keepalived,nginx,linux 1273 障切换移,是通过VRRP来的。  Keepalived服务正常工作时,主Master节点会不断地向备节点发送(多播的方式)心跳消息,用告诉备Backup节点自己还活着,当主Master节点发
java基础 1762 java集合之TreeMap原理TreeMap集合的说简单也简单说复杂也复杂,说简单是因为TreeMap底层完全依靠红黑树这个数据结构,相比与HashMap来说TreeMap不用考虑
springboot,java基础 1220   从springboot的文档知道,springboot打包一个systemV直接执行的jar文件。操作也很简单,只需要pom.xml中加入
数据结构与算法 2260 ; publicNode(intvalue,Nodenext){ super(); this.value=value; this.next=next; }}算法packageclub.test;/***
official 752 viewer.js和viewer.css,这两个文件项目的dist文件夹中。例如:htmlheadtitletitle/titlemetahttp-equiv="Content-Type"content="t
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。