Word, Excel, PPT to PDF, dwg to svg or png

There are many ways to convert pdf, some rely on the windows environment, some need to deploy plug-ins on the server, some conversion effects are not good, the conversion effect is good and do not depend on the payment of plug-ins, after several comparisons, the aspost method is used for file conversion, aspost is Paid service, this article provides a cracking method, which is only for learning and discussion, and a license is required for commercial use.

1. Import dependencies

1.jar resource download

Link: https://pan.baidu.com/s/1AMlmFzmuoNm2JGQrTk340g

Extraction code: 1129

2.jar package import

The paid jar package cannot be downloaded directly through maven. The downloaded jar package needs to be placed in the local maven resource library, and the jar package needs to be uploaded to the server resource library when deployed to the server.

        <!-- aspose-PPT操作依赖 -->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose.slides</artifactId>
            <version>19.3</version>
        </dependency>
        <!-- aspose-Excel操作依赖 -->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-cells</artifactId>
            <version>8.5.2</version>
        </dependency>
        <!-- aspose-Word操作依赖 -->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>14.9.0-jdk16</version>
        </dependency>
        <!-- aspose-CAD操作依赖 -->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-cad</artifactId>
            <version>22.7</version>
        </dependency>

3. The jar package is sent to the server resource library

Reference: https://blog.csdn.net/secretdaixin/article/details/129041068

2. File conversion tools

1. Word to PDF

package com.dmp.common.utils.file;

import cn.hutool.core.io.FileUtil;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author daixin
 * @version 1.0
 * @description: TODO
 * @date 2023/2/14 15:05
 */
@Slf4j
public class WordToPdfUtils {
    public static Boolean convert(File sourceFile, File targetFile) {
        InputStream inputStream = FileUtil.getInputStream(sourceFile);
        OutputStream outputStream = FileUtil.getOutputStream(targetFile);
        return convert(inputStream, outputStream);
    }
    public static Boolean convert(InputStream inputStream, OutputStream outputStream) {
        try {
            Document doc = new Document(inputStream);
            doc.save(outputStream, SaveFormat.PDF);
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

2.Excel to PDF

package com.dmp.common.utils.file;

import cn.hutool.core.io.FileUtil;
import com.aspose.cells.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Iterator;

/**
 * @author daixin
 * @version 1.0
 * @description: TODO
 * @date 2023/2/14 15:09
 */
@Slf4j
public class CellToPdfUtils {

    /**
     * aspose插件的证书
     */
    private static final String myLicense = "<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>";

    public static void convert(File sourceFile, File targetFile) {
        InputStream inputStream = FileUtil.getInputStream(sourceFile);
        OutputStream outputStream = FileUtil.getOutputStream(targetFile);
        convert(inputStream, outputStream);
    }
    public static void convert(InputStream inputStream, OutputStream outputStream) {
        try {
            getCellLicense();
            cellConvert2PDF(inputStream,outputStream);
            //Workbook workbook = new Workbook(sourceFile.getAbsolutePath());
            //workbook.save(targetFile.getAbsolutePath(), com.aspose.cells.SaveFormat.PDF);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void cellConvert2PDF(InputStream inputStream, OutputStream outputStream) {
        try {
            Workbook wb = new Workbook(inputStream);
            Iterator<Worksheet> iterator = wb.getWorksheets().iterator();
            while (iterator.hasNext()){
                Worksheet next = iterator.next();
                setColumnWithAuto(next);
            }
            PdfSaveOptions saveOptions = new PdfSaveOptions();
            saveOptions.setAllColumnsInOnePagePerSheet(true);
            wb.save(outputStream, saveOptions);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 设置表页的列宽度自适应
     * @param sheet
     */
    @SneakyThrows
    private static void setColumnWithAuto(Worksheet sheet) {
        Cells cells = sheet.getCells();
        int columnCount = cells.getMaxColumn() + 1;//获取表页的最大列数
        int rowCount = cells.getMaxRow();//获取表页的最大行数
        for (int col = 0; col < columnCount; col++) {
            sheet.autoFitColumn(col, 0, rowCount);
        }
        for (int col = 0; col < columnCount; col++) {
            int pixel = cells.getColumnWidthPixel(col) + 30;
            if (pixel > 255) {
                cells.setColumnWidthPixel(col, 255);
            } else {
                cells.setColumnWidthPixel(col, pixel);
            }
        }
    }


    public static boolean getCellLicense() throws IOException {
        boolean result = false;
        ByteArrayInputStream licenseInputStream = null;
        try {
            /*String file = FileConversionUtils.class.getClassLoader().getResource("license.xml").getFile();
            com.aspose.cells.License aposeLic = new com.aspose.cells.License();
            aposeLic.setLicense(new FileInputStream(file));*/
            licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());
            com.aspose.cells.License wordLicense = new com.aspose.cells.License();
            wordLicense.setLicense(licenseInputStream);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            licenseInputStream.close();
        }
        return result;
    }
}

3.PPT to PDF

package com.dmp.common.utils.file;

import cn.hutool.core.io.FileUtil;
import com.aspose.slides.Presentation;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author daixin
 * @version 1.0
 * @description: TODO
 * @date 2023/2/14 15:12
 */
@Slf4j
public class PowerPointToPdfUtils {

    /**
     * aspose插件的证书
     */
    private static final String myLicense = "<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>";

    public static Boolean convert(File sourceFile, File targetFile) {
        InputStream inputStream = FileUtil.getInputStream(sourceFile);
        OutputStream outputStream = FileUtil.getOutputStream(targetFile);
        return convert(inputStream, outputStream);
    }
    public static Boolean convert(InputStream inputStream, OutputStream outputStream) {
        try {
            getPPtLicense();
            Presentation pres = new Presentation(inputStream);
            pres.save(outputStream, com.aspose.slides.SaveFormat.Pdf);
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static boolean getPPtLicense() throws IOException {
        boolean result = false;
        ByteArrayInputStream licenseInputStream = null;
        try {
            licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());
            com.aspose.slides.License aposeLic = new com.aspose.slides.License();
            aposeLic.setLicense(licenseInputStream);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            licenseInputStream.close();
        }
        return result;
    }
}

4. dwg to svg/png

package com.dmp.common.utils.file;

import cn.hutool.core.io.FileUtil;
import com.aspose.cad.Image;
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
import com.aspose.cad.fileformats.cad.CadImage;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PngOptions;
import com.aspose.cad.imageoptions.SvgOptions;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author daixin
 * @version 1.0
 * @description: TODO
 * @date 2023/2/14 15:12
 */
public class CADFileUtils {

    /**
     * dwg转换成png
     * @param sourcePath
     * @param targetPath
     */
    public static void dwgToPng(String sourcePath,String targetPath){
        InputStream inputStream = FileUtil.getInputStream(sourcePath);
        OutputStream outputStream = FileUtil.getOutputStream(targetPath);
        dwgToPng(inputStream, outputStream);
    }

    /**
     * dwg转换成png
     * @param inputStream
     * @param outputStream
     */
    public static void dwgToPng(InputStream inputStream, OutputStream outputStream){
        PngOptions pngOptions = new PngOptions();
        // 设置png的压缩等级 0 最低 9 最高
        pngOptions.setCompressionLevel(0);
        CadImage image = (CadImage)CadImage.load(inputStream);
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        //等比缩放
        uniformScale(image,cadRasterizationOptions);
        cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
        pngOptions.setVectorRasterizationOptions(cadRasterizationOptions);
        image.save(outputStream, pngOptions);
    }

    /**
     * dwg转换成Svg
     * @param sourcePath
     * @param targetPath
     */
    public static void dwgToSvg(String sourcePath,String targetPath){
        InputStream inputStream = FileUtil.getInputStream(sourcePath);
        OutputStream outputStream = FileUtil.getOutputStream(targetPath);
        dwgToSvg(inputStream, outputStream);
    }

    /**
     * dwg转换成Svg
     * @param inputStream
     * @param outputStream
     */
    public static void dwgToSvg(InputStream inputStream, OutputStream outputStream){
        SvgOptions options = new SvgOptions();
        Image image = Image.load(inputStream);
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        //等比缩放
        uniformScale(image,cadRasterizationOptions);
        cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
        options.setVectorRasterizationOptions(cadRasterizationOptions);
        image.save(outputStream,options);
    }

    /**
     * 等比缩放
     * @param image
     * @param cadRasterizationOptions
     */
    private static void uniformScale(Image image, CadRasterizationOptions cadRasterizationOptions){
        double width = image.getWidth();
        double height = image.getHeight();
        double baseNum = 2073600;
        double zoom = Math.sqrt(baseNum / width / height);

        cadRasterizationOptions.setPageHeight((int)(height * zoom));
        cadRasterizationOptions.setPageWidth((int)(width * zoom));
    }
}

Guess you like

Origin blog.csdn.net/secretdaixin/article/details/129686300