将上传的word文档一键转为pdf并附带文字水印
需jar包:aspose-words-15.8.0-jdk16.jar
license.xml 为去除生成的pdf附带的产品信息 将其置于resource目录下
package com.cbay.ms.ctr.infrastructure;
import com.aspose.words.Shape;
import com.aspose.words.*;
import lombok.extern.slf4j.Slf4j;
import java.awt.*;
import java.io.*;
/**
* @Author: Mr.HRL
* @Description:
* @Date: 2020-03-03 22:25
* @Version: 1.0
*/
@Slf4j
public class wordToPdfTest {
public static void main(String[] args) {
doc2pdf("F:\\ZX\\附件:售后回租赁合同.docx","F:\\ZX\\test.pdf");
}
public static void doc2pdf(String inPath, String outPath) {
if(getLicense()) {
FileOutputStream os = null;
try {
File file = new File(outPath);
os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
insertWatermarkText(doc, "水印");
doc.save(os, SaveFormat.PDF);
log.info("输出pdf---->结束"+os);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 去除自带产品标识
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = wordToPdfTest.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
*
* @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));
}
}
<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>
效果

本文介绍了一种使用Aspose.Words Java库将Word文档转换为PDF的方法,并在转换过程中添加文字水印。通过示例代码展示了如何设置水印的字体、大小、颜色及位置,确保水印覆盖整个页面。

2854

被折叠的 条评论
为什么被折叠?



