Springboot ppt to pdf - aspose method

Springboot ppt to pdf - aspose method

1. Download the package required for converting ppt to pdf

[网盘地址](https://pan.baidu.com/s/1V-CZ0zXcJzKofxr6qC1g8A)
提取码:86lp

2. maven add dependency

  1. Create a lib folder in the project root directory
  2. Put the downloaded aspose-slides.jar package in the lib directoryinsert image description here
  3. Introduce local dependencies in pom.xml
    <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-slides</artifactId>
            <version>19.3</version>
            <scope>system</scope>
            <systemPath>${
    
    project.basedir}/lib/aspose.slides-19.3.jar</systemPath>
        </dependency>

3. Place the license.xml file in the root directory of resources (to remove the watermark)

insert image description here

4. Create java class and write code



import com.aspose.slides.License;
import com.aspose.slides.Presentation;

import com.aspose.slides.SaveFormat;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.*;
import java.util.Date;

public class PPT2PdfAsposeUtil {
    
    
    public static boolean getLicensePPT() {
    
    
        boolean result = false;
        InputStream is = null;
        try {
    
    
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");
            is = resources[0].getInputStream();
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (is != null) {
    
    
                try {
    
    
                    is.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     *
     * @param inPath ppt路径
     * @param outPath pdf路径
     * @return
     */
    public static boolean ppt2Pdf(String inPath,String outPath){
    
    
        // 验证License 去除水印
        if (!getLicensePPT()) {
    
    
            return false;
        }
        long start = new Date().getTime();
        try {
    
    
            FileInputStream fileInput = new FileInputStream(inPath);
            Presentation pres = new Presentation(fileInput);
            FileOutputStream out = new FileOutputStream(new File(outPath));
            pres.save(out, SaveFormat.Pdf);
            out.close();
        } catch (Exception e) {
    
    
             e.printStackTrace();
            return false;
        }
        long end = new Date().getTime();
        System.out.println("pdf转换成功,共耗时:" + ((end - start) / 1000.0) + "秒"); // 转化用时
        return true;
    }

    public static void main(String[] args) {
    
    
        ppt2Pdf("E:\\code\\test\\1.pptx","E:\\code\\test\\1.pdf");
    }
}

5. Test

Execute the main method

 public static void main(String[] args) {
    
    
        ppt2Pdf("E:\\code\\test\\1.pptx","E:\\code\\test\\1.pdf");
    }

Guess you like

Origin blog.csdn.net/qq_42900469/article/details/130720651