How to merge multiple PNG files together in Java

Merging files together in Java is actually quite a messy proposition, requiring a great deal more effort than seems really necessary. If you would rather take the slacker approach, requiring essentially no work on your part, then look no further. This tutorial will get you set up with PNG merging in record time.

To begin, we will be using Jitpack to compile our library dynamically, so we need to add a repository reference for that.

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And then a dependency for our API client.

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

Now we are ready to call our function, as demonstrated below.

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.MergeDocumentApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");MergeDocumentApi apiInstance = new MergeDocumentApi();File inputFile1 = new File("/path/to/file.txt"); // File | First input file to perform the operation on.File inputFile2 = new File("/path/to/file.txt"); // File | Second input file to perform the operation on.File inputFile3 = new File("/path/to/file.txt"); // File | Third input file to perform the operation on.File inputFile4 = new File("/path/to/file.txt"); // File | Fourth input file to perform the operation on.File inputFile5 = new File("/path/to/file.txt"); // File | Fifth input file to perform the operation on.File inputFile6 = new File("/path/to/file.txt"); // File | Sixth input file to perform the operation on.File inputFile7 = new File("/path/to/file.txt"); // File | Seventh input file to perform the operation on.File inputFile8 = new File("/path/to/file.txt"); // File | Eighth input file to perform the operation on.File inputFile9 = new File("/path/to/file.txt"); // File | Ninth input file to perform the operation on.File inputFile10 = new File("/path/to/file.txt"); // File | Tenth input file to perform the operation on.try {byte[] result = apiInstance.mergeDocumentPngMulti(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling MergeDocumentApi#mergeDocumentPngMulti");e.printStackTrace();}

And that’s it!

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store