Convert Springboot WORD to PDF - Aspose method

Convert Springboot WORD to PDF - Aspose method

1. Prepare relevant resources

1. Download the converted Aspose resource package

Network disk address
Extraction code: dddc

2. Download the license file to remove the watermark

Network disk address
Extraction code: yesg

Two, configure maven

1. Put the downloaded jar package in the project root directory

insert image description here

2. Put the downloaded license.xml under resources in the root directory

insert image description here

3. Configure pom.xml

  		 <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.8.0</version>
            <scope>system</scope>
            <systemPath>${
    
    project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
        </dependency>

3. Write code

public class Word2PdfAsposeUtil {
    
    
    public static boolean getLicense() {
    
    
        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();
//            Resource resource = new ClassPathResource("license.xml");
//            is = resource.getInputStream();
            //InputStream is = Word2PdfAsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            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;
    }
        /**
     * word 转pdf
     *
     * @param inPath  word路径
     * @param outPath pdf路径
     * @return
     */
    public static boolean doc2pdf(String inPath, String outPath) {
    
    
        if (!getLicense()) {
    
     // 验证License 若不验证则转化出的pdf文档会有水印产生
            return false;
        }
        FileOutputStream os = null;
        try {
    
    
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档

            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
    
    
            e.printStackTrace();
            return false;
        } finally {
    
    
            if (os != null) {
    
    
                try {
    
    
                    os.flush();
                    os.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }

Fourth, write the main method test

  public static void main(String[] args) {
    
    
        doc2pdf("E:\\123\\1.docx","E:\\123\\1.pdf");
    }

Guess you like

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