Is it possible to combine PDF files into one with mupdf on Android?

I have many one-page PDF files and I would like to combine it into one pdf file with the mupdf library on my Android device. Is it possible?

If this 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 also be encrypted.

+4
source share
2 answers

While the MuPDF library on which the viewer is based is capable of such a feat, the viewer application can no longer combine PDF files.

I do not know a single tool that will combine PDF files and run on Android, although I could easily be mistaken.

As for encryption, you will have to decrypt all input files and encrypt the final file as separate operations. Thus, in addition to the user interface that allows you to specify multiple input files, the order in which they should be combined (and, indeed, potentially, the order in which the pages from each should be used), you should also be able to specify passwords for decryption and the final encryption methodology. Pretty complicated interface.

+2
source

I don’t know how to do this for MuPDF, but you can combine several PDF files on Android with the latest Apache PdfBox Release , (Not final yet ... RC3 ...)

Just add this dependency to your build.gradle:

compile 'org.apache.pdfbox:pdfbox:2.0.0-RC3' 

And in the asynchronous task do the following:

 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 an SD card, you can open FileInputStream.

I download PDF files from the Internet as follows:

 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 OkHttpClient, add this to your build.gradle:

 compile 'com.squareup.okhttp:okhttp:2.7.2' 

Note: This does not work with encrypted files. To combine encrypted files, you must first decrypt all the individual files and then encrypt the combined pdf.

+1
source

Source: https://habr.com/ru/post/1446966/


All Articles