Reputation:
I have many one page pdf files and I would like to merge it into one pdf file with mupdf library on android device. Is this possible?
If it is not possible, can you recommend something else that I can use on android?
Note: all pdf files are encrypted and the resulting pdf file must be also encrypted.
Upvotes: 3
Views: 2974
Reputation: 1020
I don't know how to do this for MuPDF, but you can combine multiple PDF Files on Android with with the latest Apache PdfBox Release. (At the moment still not final... RC3...)
Just add this dependency to your build.gradle:
compile 'org.apache.pdfbox:pdfbox:2.0.0-RC3'
And in an async task do this:
private File downloadAndCombinePDFs(String urlToPdf1, String urlToPdf2, String urlToPdf3 ) throws IOException {
PDFMergerUtility ut = new PDFMergerUtility();
ut.addSource(NetworkUtils.downloadFile(urlToPdf1, 20));
ut.addSource(NetworkUtils.downloadFile(urlToPdf2, 20));
ut.addSource(NetworkUtils.downloadFile(urlToPdf3, 20));
final File file = new File(getContext().getExternalCacheDir(), System.currentTimeMillis() + ".pdf");
final FileOutputStream fileOutputStream = new FileOutputStream(file);
try {
ut.setDestinationStream(fileOutputStream);
ut.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());
} finally {
fileOutputStream.close();
}
return file;
}
Here NetworkUtils.downloadFile() should return an InputStream. If you have them on your sdcard you can open a FileInputStream.
I download the PDFs from the internet like this:
public static InputStream downloadFileThrowing(String url, int timeOutInSeconds) throws IOException {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(timeOutInSeconds, TimeUnit.SECONDS);
client.setReadTimeout(timeOutInSeconds, TimeUnit.SECONDS);
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Download not successful.response:" + response);
else
return response.body().byteStream();
}
To use the OkHttpClient add this to your build.gradle:
compile 'com.squareup.okhttp:okhttp:2.7.2'
Note: This works not with encrypted files. To combine encrypted files you should first decrypt all single files and later encrypt the merged pdf.
Upvotes: 1
Reputation: 31199
While the MuPDF library, on which the viewer is based, is capable of such a feat the viewer application cannot currently combine PDF files.
I'm not aware of any tool which will combine PDF files and runs on Android, though I could easily be wrong.
As regards encryption, you are going to have to decrypt all the input files, and encrypt the final file as separate operations. So in addition to a UI which allows you to specify multiple input files, the order in which they are to be combined (and indeed, potentially, the order in which pages from each are to be used), you also need to be able to specify passwords for decryption and a final encryption methodology. Quite a complex UI.
Upvotes: 1