You may take a look at Spire.Office for Java library. It allows you to convert multiple file types like Word .doc/.docx, RTF, Excel .xls/.xlsx, PowerPoint .ppt/.pptx, text and images etc. to PDF without having MS Office, Adobe Acrobat or other software to be installed.
The following is a basic example shows how to convert Word .doc/.docx, Excel .xls/.xlsx, PowerPoint .ppt/.pptx and image within a particular directory to PDF files and combine the resultant files to a single PDF.
import com.spire.doc.Document;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentBase;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import com.spire.presentation.Presentation;
import com.spire.xls.Workbook;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.io.*;
public class MergeFilesToOnePDF {
public static void main(String []args) throws Exception {
//Create a File object for directory
File directoryPath = new File("D:\\Files");
//Check if the directory exists
if(!directoryPath.exists()){
throw new RuntimeException("The directory does not exist.");
}
//Get the files in the directory
File[] filesList = directoryPath.listFiles();
//Create an InputStream array
InputStream[] streams = new InputStream[filesList.length];
int i = 0;
//Loop through the files
for (File file : filesList) {
//Get the file name
String fileName = file.getName();
//Get the absolute file path
String absolutePath = file.getAbsolutePath();
//Get the file name extension
String extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
//Convert different types of files to PDF and save to streams
switch (extension) {
case ".doc":
case ".docx":
//Convert Word to PDF and save to stream
Document doc = new Document();
doc.loadFromFile(absolutePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
doc.saveToStream(bos, com.spire.doc.FileFormat.PDF);
streams[i++] = new ByteArrayInputStream(bos.toByteArray());
break;
case ".xls":
case ".xlsx":
//Convert Excel to PDF and save to stream
Workbook workbook = new Workbook();
workbook.loadFromFile(absolutePath);
//workbook.getConverterSetting().setSheetFitToPage(true);
bos = new ByteArrayOutputStream();
workbook.saveToStream(bos, com.spire.xls.FileFormat.PDF);
streams[i++] = new ByteArrayInputStream(bos.toByteArray());
break;
case ".ppt":
case ".pptx":
//Convert PowerPoint to PDF and save to stream
Presentation ppt = new Presentation();
ppt.loadFromFile(absolutePath);
bos = new ByteArrayOutputStream();
ppt.saveToFile(bos, com.spire.presentation.FileFormat.PDF);
streams[i++] = new ByteArrayInputStream(bos.toByteArray());
break;
case ".pdf":
//Save PDF to stream
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(absolutePath);
bos = new ByteArrayOutputStream();
pdf.saveToStream(bos, com.spire.pdf.FileFormat.PDF);
streams[i++] = new ByteArrayInputStream(bos.toByteArray());
break;
case ".png":
case ".jpg":
//Convert image to PDF and save to stream
pdf = new PdfDocument();
//Convert inches to points
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
float width = unitCvtr.convertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
float height = unitCvtr.convertUnits(11.0f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
Dimension2D size = new Dimension((int)width, (int)height);
//Set page size
pdf.getPageSettings().setSize(size);
PdfPageBase page = pdf.getPages().add();
PdfImage image = PdfImage.fromFile(absolutePath);
page.getCanvas().drawImage(image, 100, 50);
bos = new ByteArrayOutputStream();
pdf.saveToStream(bos, com.spire.pdf.FileFormat.PDF);
streams[i++] = new ByteArrayInputStream(bos.toByteArray());
break;
default:
System.out.println("Invalid file format.");
break;
}
}
//Merge PDFs in the streams into a single PDF
PdfDocumentBase pdf = PdfDocument.mergeFiles(streams);
OutputStream outputStream = new FileOutputStream("D:\\Output\\Combine.pdf");
pdf.save(outputStream);
pdf.close();
}
}
Apart from file conversion, it also provides lots of other functionalities, you can visit the documentation for more information:
https://www.e-iceblue.com/Tutorials.html