Java PPT转PDF 亲测无水印

最近有个需求需要把PPT文件转为PDF,整了好久转换完成一直都是有水印的,最终终于找到了去水印的办法,记录一下。

jar包自取

链接:https://pan.baidu.com/s/1IBJGQcnuNBhlPDb7nPpDPg?pwd=fJrs

提取码:fJrs

嫌百度网盘慢的可以用下方阿里云盘链接

阿里云盘分享

本地安装jar命令:

mvn install:install-file -Dfile="C:/Users/DELL/Desktop/aspose.slides-15.9.0.jar" -DgroupId=com.aspose -DartifactId=aspose-slides -Dversion=15.9.0 -Dpackaging=jar

下面上代码块(全部的看下面AsposeUtil代码):

    /**
	 * ppt to pdf
	 * @param inputStream
	 */
	@SneakyThrows
	public static ByteArrayInputStream ppt2PDF(InputStream inputStream) {
		// 验证License
		if (!getLicenseq()) {
			return null;
		}
		try {
			Presentation ppt = new Presentation(inputStream);
			ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
			ppt.save(dstStream, com.aspose.slides.SaveFormat.Pdf);
			ByteArrayInputStream inputStream1 = parse(dstStream);
			return inputStream1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

    /**
	 * licence 验证
	 *
	 * @return
	 * @throws Exception
	 */
	public static boolean getLicenseq() throws Exception {
		boolean result = false;
		try {
			InputStream is = AsposeUtil.class
				.getResourceAsStream("/slides/license.xml");
			com.aspose.slides.License aposeLic = new com.aspose.slides.License();
			aposeLic.setLicense(is);
			result = true;
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return result;
	}

AsposeUtil 类

import com.aspose.slides.Presentation;
import com.aspose.words.*;
import com.aspose.words.Shape;
import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * @author 不敢喝雪碧 我怕心飞扬
 * @version
 */
public class AsposeUtil {

	private static InputStream slides;

	/**
	 * licence 验证
	 *
	 * @return
	 * @throws Exception
	 */
	public static boolean getLicenseq() throws Exception {
		boolean result = false;
		try {
			InputStream is = AsposeUtil.class
				.getResourceAsStream("/slides/license.xml");
			com.aspose.slides.License aposeLic = new com.aspose.slides.License();
			aposeLic.setLicense(is);
			result = true;
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return result;
	}

/**
	 * ppt to pdf
	 * @param inputStream
	 */
	@SneakyThrows
	public static ByteArrayInputStream ppt2PDF(InputStream inputStream) {
		// 验证License
		if (!getLicenseq()) {
			return null;
		}
		try {
			Presentation ppt = new Presentation(inputStream);
			ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
			ppt.save(dstStream, com.aspose.slides.SaveFormat.Pdf);
			ByteArrayInputStream inputStream1 = parse(dstStream);
			return inputStream1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

 license.xml

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>29991231</SubscriptionExpiry>
    <LicenseExpiry>29991231</LicenseExpiry>
    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
  </Data>
  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

最后结果:

注:仅用于学习和交流,切勿用于商业用途!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要实现PPTPDF,可以使用Apache POI和Apache PDFBox两个Java库。具体步骤如下: 1. 使用Apache POI读取PPT文件,获取每一页的内容和样式信息。 2. 创建一个PDF文档对象,使用Apache PDFBox。 3. 将每一页的内容和样式信息写入PDF文档对象中。 4. 保存PDF文档对象到本地文件。 以下是一个简单的PPTPDFJava代码示例: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.tools.imageio.ImageIOUtil; public class PptToPdfConverter { public static void convert(String pptFilePath, String pdfFilePath) throws IOException { // 读取PPT文件 HSLFSlideShow ppt = new HSLFSlideShow(new FileInputStream(pptFilePath)); // 创建PDF文档对象 PDDocument pdf = new PDDocument(); // 遍历PPT每一页,将内容写入PDF文档对象中 for (int i = 0; i < ppt.getSlides().size(); i++) { PDPage page = new PDPage(); pdf.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(pdf, page); contentStream.drawImage(new PDFRenderer(ppt.getSlides().get(i)).renderImageWithDPI(300), 0, 0); contentStream.close(); } // 保存PDF文档对象 pdf.save(pdfFilePath); pdf.close(); // 释放PPT文件资源 ppt.close(); } public static void main(String[] args) throws IOException { String pptFilePath = "test.ppt"; String pdfFilePath = "test.pdf"; convert(pptFilePath, pdfFilePath); } } ``` 该代码使用了Apache POI的HSLFSlideShow类读取PPT文件,使用了Apache PDFBox的PDDocument类创建PDF文档对象和PDPage类创建PDF页面对象,使用了PDFRenderer类将PPT页面换成PDF页面,使用了PDPageContentStream类将PDF页面写入PDF文档对象中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值