Combine Word, Excel, PowerPoint, PDF and Image Files into One PDF in Java

Introduction

When we intend to share multiple types of documents such as Word, Excel, PowerPoint, PDF and image with others, we might want to combine the documents into a single PDF because it can help us freeze the document formatting and layouts, furthermore, people who receive the PDF document can view it easily on their devices without installing a variety of applications like Microsoft Office or other equivalents. In this article, we are going to illustrate how to achieve this function programmatically using Java.

Add Dependencies

In order to combine documents, we will use Spire.Office for Java, which is a multi-functional and easy-to-use API for creating, manipulating and converting Office and PDF documents. 

You can either download the API’s jar from this website or install it from maven by adding the following code to your maven-based project’s pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.office</artifactId>
        <version>4.9.5</version>
    </dependency>
</dependencies>


Combine Word, Excel, PowerPoint, PDF and Image into One PDF

Spire.Office for Java API allows you to convert documents in various formats like Word doc/ docx, Excel xls/xlsx, PowerPoint ppt/pptx, image jpg/png etc. to PDF documents and then combine the PDF documents into one PDF. In the table below, you will see the main classes and methods used to implement this task:

Classes Methods Method Descriptions
Document Document.loadFromFile(String) Loads a Word document from disk.
Document.SaveToStream(OutputStream, FileFormat) Saves Word document to OutputStream in Word or other formats.
Workbook Workbook.loadFromFile(String) Loads an Excel document from disk.
Workbook.saveToStream(OutputStream, FileFormat) Saves Excel document to OutputStream in Excel or other formats.
Presentation Presentation.loadFromFile(String) Loads a PowerPoint document from disk.
Presentation.saveToFile(OutputStream, FileFormat) Saves PowerPoint document to OutputStream in PowerPoint or other formats.
PdfDocument PdfDocument.loadFromFile(String) Loads a PDF document from disk.
PdfDocument.saveToStream(OutputStream, FileFormat) Saves PDF document to OutputStream in PDF or other formats.
PdfDocument.mergeFiles(InputStream[]) Combines PDF input streams into one PDF.
PdfDocumentBase PdfDocumentBase.save(OutputStream) Saves PDF document to OutputStream.
PdfPageCollection PdfPageCollection.add() Adds a new page to a PDF document.
PdfImage PdfImage.fromFile(String) Creates a PdfImage object from an image file.
PdfCanvas PdfCanvas.drawImage(PdfImage, float, float) Draws an image to the specified location of a PDF page.

Before compiling the code, you need to add the following “import statements”:

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.*;

The following code example shows how to combine Word doc/ docx, Excel xls/xlsx, PowerPoint ppt/pptx, image jpg/png within a specific directory into one PDF:

//Create a File object for the 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 into a File array
File[] filesList = directoryPath.listFiles();

//Create an InputStream array
InputStream[] streams = new InputStream[filesList.length];

//Check if the File array is null
if(filesList != null && filesList.length > 0) {
    int i = 0;
    //Loop through the files in the array
    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());

        switch (extension) {
            case ".doc":
            case ".docx":
                //Convert Word doc/docx 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 xls/xlsx 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 ppt/pptx 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 png/jpg to PDF and save to stream
                pdf = new PdfDocument();
                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);
                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;
        }
    }
}

//Combine PDF input streams into one PDF
PdfDocumentBase pdf = PdfDocument.mergeFiles(streams);
OutputStream outputStream = new FileOutputStream("D:\\Output\\Combine.pdf");
pdf.save(outputStream);
outputStream.close();
pdf.close();

The input Word, Excel, PDF, PowerPoint and image documents:

Spire.Office for Java API

The combined PDF:

Spire.Office for Java API

Conclusion

This article demonstrated how to convert and combine Word, Excel, PowerPoint, PDF, and image documents into one PDF using Spire.Office for Java API. The API also supports lots of other document manipulations such as document splitting, editing, encryption, decryption, and printing, you can explore more about it by visiting the documentation. If you have any questions, you can post it on the forum.


Similar Articles