Java realizes the conversion of docx, doc, xlsx, xls, ppt files into pdf files

1. The following jar packages need to be imported

 <!--word to pdf-->
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words</artifactId>
        <version>15.8.0</version>
    </dependency>
    
    <!--xlsx or xls to pdf-->
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-cells</artifactId>
        <version>8.5.2</version>
    </dependency>
 
    <!--ppt to pdf-->
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-slides</artifactId>
        <version>19.6</version>
    </dependency>

2. Remove watermark files

3. Once you are ready, start the actual operation

(1), first is the conversion operation of xlsx and xls to pdf

i, guide package

import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

ii. Method of reading and removing watermark files

public static boolean getLicense() {
    boolean result = false;
    try {
        //Read license.xml
        InputStream is = ExcelConvertPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
        License aposeLic = new License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

iii. Conversion operation

public static void excel2pdf(String sourceFilePath, String desFilePathd){
    //Verify the License. If not verified, the converted PDF document will have a watermark.
    if (!getLicense()) {
        return;
    }
    log.info("Start conversion excel==start");
    FileOutputStream fileOS = null;
    long old = System.currentTimeMillis();
    try {
        //Original excel path
        Workbook wb = new Workbook(sourceFilePath);
        fileOS = new FileOutputStream(desFilePathd);
        PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
        pdfSaveOptions.setOnePagePerSheet(true);
        int[] autoDrawSheets={3};
        //When the corresponding sheet page width in excel is too large, it will be split and paged in PDF. Scaled here.
        autoDraw(wb,autoDrawSheets);
        int[] showSheets={0};
        //Hide unnecessary sheet pages in the workbook.
        printSheetPage(wb,showSheets);
        wb.save(fileOS, pdfSaveOptions);
        fileOS.flush();
        long now = System.currentTimeMillis();
        log.info("Total time spent: {}",((now - old) / 1000.0) );
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        if(fileOS!=null){
            try {
                fileOS.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/**
 * Hide unnecessary sheet pages in the workbook.
 * @param wb
 * @param page displays the sheet array of the page
 */
public static void printSheetPage(Workbook wb,int[] page){
    for (int i= 1; i < wb.getWorksheets().getCount(); i + + ) {
        wb.getWorksheets().get(i).setVisible(false);
    }
    if(null==page||page.length==0){
        wb.getWorksheets().get(0).setVisible(true);
    }else{
        for (int i = 0; i < page.length; i + + ) {
            wb.getWorksheets().get(i).setVisible(true);
        }
    }
}

/**
 * Set the automatic stretching ratio of printed sheets
 * @param wb
 * @param page sheet array of automatically stretched pages
 */
public static void autoDraw(Workbook wb,int[] page){
    if(null!=page & amp; & amp;page.length>0){
        for (int i = 0; i < page.length; i + + ) {
            wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
            wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
        }
    }
}

iiii, run file

public static void main(String[] args) {
    String inputExcelPath = "/Users/Desktop/xxx.xls";
    String outputPdfPath = "/Users/Desktop/xxx.pdf";
    excel2pdf(inputExcelPath,outputPdfPath);

}

(2), ppt to pdf conversion tool class

i, guide package

import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import lombok.extern.log4j.Log4j2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

ii. Method of reading and removing watermark files

 public static boolean getLicense() {
    boolean result = false;
    try {
        // license.xml
        InputStream is = ExcelConvertPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
        License aposeLic = new License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

iii. Conversion method

 public static void ppt2Pdf(String inPath,String outPath){
    //Verify License and remove watermark
    if (!getLicense()) {
        return ;
    }
    long start = System.currentTimeMillis();
    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();
    }
    long end =System.currentTimeMillis();
    // Conversion time
    log.info("pdf conversion successful, total time taken: {}" , ((end - start) / 1000.0) + "seconds");
}

iiii, run file

public static void main(String[] args) {
String inputExcelPath = “/Users/Desktop/xxx.ppt”;
String outputPdfPath = “/Users/Desktop/pdf.pdf”;
ppt2Pdf(inputExcelPath,outputPdfPath);

}

(3), work to pdf file

i, guide package

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.log4j.Log4j2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

ii. Read and remove watermark files

 public static boolean getLicense() {
    boolean result = false;
    try {
        // license.xml should be placed in
        InputStream is = WordConvertPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
        License aposeLic = new License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

iii. Conversion method

/**
 * @param inPath source file path
 * @param outPath new pdf file path
 */
public static void doc2pdf(String inPath, String outPath) {
    // Verify the License. If not verified, the converted PDF document will have a watermark.
    if (!getLicense()) {
        return;
    }
    log.info("Start converting doc==start");
    FileOutputStream os = null;
    try {
        long old = System.currentTimeMillis();
        // Create a new blank pdf document
        File file = new File(outPath);
        os = new FileOutputStream(file);
        // Address is the word document to be converted
        Document doc = new Document(inPath);
        // Fully supports DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
        doc.save(os, SaveFormat.PDF);
        // EPUB, XPS, SWF mutual conversion
        long now = System.currentTimeMillis();
        // Conversion time
        log.info("Total time spent: {}",((now - old) / 1000.0) );
    } catch (Exception e) {
        log.info("Error when converting pdf==[{}]",e);
    }
    finally {
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

iiii, run

public static void main(String[] args) {
    String inPath = "/Users/Desktop/xxx.doc";
    String outPath ="/Users/Desktop/xxx.pdf";
    doc2pdf(inPath,outPath);
}