前段时间,项目需要自动生成word文档,用WordFreeMarker生成word文档后,又要求生成的文档能在浏览器浏览,思来想去,把word文档转成pdf就好了,于是乎研究了一下。

将word文档转化为PDF是项目中常见的需求之一,目前主流的方法可以分为两大类,一类是利用各种Office应用进行转换,譬如Microsoft
Office、WPS以及LiberOffice,另一种是利用各种语言提供的对于Office文档读取的接口(譬如Apache POI,jacob,docx4j,
openoffice),这些要么收费,要么要装插件,要么转换之后样式走形,乱码等等。

我使用Aspose.Words for Java 可以导出复杂WORD PDF HTML 多种数据格式 
官方下载地址:http://www.aspose.com/java/word-component.aspx
<http://www.aspose.com/java/word-component.aspx> 

我所用的版本是aspose-words-14.9.0-jdk16。需要到的两个jar包(所需jar包下载
<https://download.csdn.net/download/qq_36892341/10577589>),也可以联系我,免费分享。

废话不多说,直接上代码:
import java.io.File; import java.io.FileOutputStream; import
java.io.IOException; import org.slf4j.Logger; import com.aspose.words.Document;
import com.aspose.words.SaveFormat; /** * * @ClassName: Word2PdfUtilt *
@Description: PDF转换工具类 * @author mzl * @date 2018年7月30日 下午5:26:44 * */ public
class Word2PdfUtilt { protected static Logger log = Log.get(); public static
void main(String[] args) {
doc2pdf("D:\\Workspaces\\小花测试_20180731094521.doc","D:\\Workspaces\\test.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文档 doc.save(os, SaveFormat.PDF); } catch (Exception e) {
e.printStackTrace(); }finally{ if(os!=null){ try { os.close(); } catch
(IOException e) { e.printStackTrace(); } } } } }
转成PDF格式后,图片等样式均没有乱。



以上就是生产PDF的步骤了,接下来添加水印:
/** * * @Title: insertWatermarkText * @Description: PDF生成水印 * @author mzl *
@param doc * @param watermarkText * @throws Exception * @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)); }
效果图如下,我添加的水印为我的博客昵称:择依MZL


友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信