java将word转换成pdf,并去除水印

注意我这里只是将word字节替换成pdf字节,如果是文件根据自己实际情况来做

1、所需jar包

 <dependency>
      <groupId>com.aspose</groupId>
      <artifactId>aspose-words</artifactId>
      <version>15.8.0</version>
  </dependency>

2、去除水印配置文件,此文件放到resource文件夹下,如图

java将word转换成pdf,并去除水印_第1张图片

3、license配置文件

<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、核心代码

@Componet
@Slf4j
public class FileUtil {
		@Autowired
		private ResourceLoader resourceLoader;
		/**
		* 去除水印校验
		*/
	 	public boolean getLicense() {
	           boolean result = false;
	            try {
	                Resource resource = resourceLoader.getResource("classpath:/license.xml");
	                License aposeLic = new License();
	                aposeLic.setLicense(is);
	                result = true;
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	            return result;
	     }
	
		/**
		* 将word字节转换成pdf字节
		*/
      	public byte[] wordBytes2PdfBytes(byte[] wordBytes) throws Exception{
			boolean license = getLicense();// 验证license 若不验证则转化出pdf文档会有水印产生
			log.info("验证结果:{}", license);
			Document document = new Document(new ByteArrayInputStream(wordBytes));
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			document.save(outputStream, SaveFormat.PDF);
			// 返回生成的pdf字节码
			return outputStream.toByteArray();
		}
  }

你可能感兴趣的:(java操作集合,java,word,pdf)