Merge multiple files of different formats into one PDF file

Hi Aspose Team, we need some help with the Aspose PDF java library. We are building a document management solution in Mulesoft (with Java plugin) which will have the option to upload and download documents. Users can upload a document of any type (pdf, tiff, gif, jpg, jpeg, png, doc, docx, xlsx) for a given order. An order can have multiple files associated with it. While downloading an order, we need to merge all these files into one single PDF and send back to the user.

We just need help with the download part. The input to the java function will be an array of base64 encoded strings, which can contain any number of files of the types mentioned above. We need to iterate through the list and add them to a PDF document.

We are not java or aspose experts so will appreciate any help in this regard! Please find below a skeleton code of what we are trying to do -

public class Merge {
    public static Object createPdf(String[] base64EncodedFiles, String[] fileTypes, String orderNum) {
        try {

            System.out.println("**** PDF BUILDING STARTED ****");
			
            System.out.println(base64EncodedFiles.length);
            System.out.println(fileTypes);
            Base64.Decoder dec = Base64.getDecoder();
            List<InputStream> list = new ArrayList<InputStream>();

            System.out.println("**** PDF FILES SETUP STARTED ****");
			
            for (int i = 0; i < base64EncodedFiles.length; i++){
                byte[] decbytes = dec.decode(base64EncodedFiles[i]);
                InputStream inputStream = new ByteArrayInputStream(decbytes);
                list.add(inputStream);
                System.out.println("**** PDF FILES SETUP: " + (i+1) + " of " + base64EncodedFiles.length + " ****");
            }
			
			//Initialize main PDF here
			
			for (int i = 0; i < list.size(); i++) {
				InputStream inStr = list.get(i);
                System.out.println("NEXT FILE IS OF TYPE: " + fileTypes[i]);
				
				//Convert the file and add to the main PDF below
				if (fileTypes[i].contains("PDF") || fileTypes[i].contains("pdf")) {
					//add to main PDF
				} else if(fileTypes[i].contains("TIFF") || fileTypes[i].contains("tif") || fileTypes[i].contains("TIF")) {
					//add to main PDF
				} else if(fileTypes[i].contains("jpg") || fileTypes[i].contains("JPG"))  {
					//add to main PDF
				} else if(fileTypes[i].contains("JPEG") || fileTypes[i].contains("jpeg"))  {
					//add to main PDF
				} else if(fileTypes[i].contains("PNG") || fileTypes[i].contains("png"))  {
					//add to main PDF
				} else if(fileTypes[i].contains("GIF") || fileTypes[i].contains("gif"))  {
					//add to main PDF
				} else if(fileTypes[i].contains("DOC") || fileTypes[i].contains("doc") || fileTypes[i].contains("docx")) {
					//add to main PDF
				} else if(fileTypes[i].contains("XLSX") || fileTypes[i].contains("xlsx"))  {
					//add to main PDF
				}
			}
			
		} catch (Exception e) {
            e.printStackTrace();
        }

        return base64String;
    }
}

@abhishekmulgund

Would you please clear a bit more; do you need assistance in downloading the final merged PDF file? OR you need to determine what format the supplied base64 string in? e.g. .xlsx, .docx, etc.?

Hi @asad.ali - We need help in understanding if this can be done with Aspose. We are still in POC phase and want to evaluate if Aspose can be used to achieve what we are trying to do.

We will have an array of files (in base64 string), and we will also know the format of the files (.pdf, .docx, .xlsx, etc). We want to understand if we can merge them all in one PDF file using Aspose.

For example - Input files are [fileA.pdf, fileB.jpg, fileC.png, fileD.docx, fileE.xlsx] (an array of base64 strings)
Expected output - outputFile.pdf (which will contain all the input documents)

Any code samples would be helpful!

@abhishekmulgund

Yes, you can first convert all of these files into PDF and then finally merge all of the PDFs into one final resultant PDF file. You will need to use different Aspose APIs for the purpose. Please check below sample links for better understandings and feasibility proof: