最近在网上看了很多资料,遇到了很多头疼的问题,看了各类大神写的方法,最初想要研究的是在线预览word
现在来看,不太现实,除了微软研究的一套在线预览的url,但是满足的条件有点扯淡,有兴趣的可以去查资料
还有就是各类各样的收费平台,来看看这种转换的处理方法吧
docx文档转换为PDF
转换之后排版不混乱
使用工具(Jar包)
aspose-words-15.11.0.jar(用于PDF转换 )
JDK1.8
public static void main(String[] args) {
doc2pdf("/Users/lzl/Desktop/a.docx","/Users/lzl/Desktop/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文档
//insertWatermarkText(doc, "四叶草的诗雨");
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
加水印(升级版)
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));