I have single page several PDF files in my SD card. Now I need to programmatically merge those single page PDF files in to one PDF document. I have used Android PDF Writer library to create those single PDF files. How can I do that?I tried some codes and questions but i cant find any good answer. Can anyone please help me out ?
Asked
Active
Viewed 6,526 times
1
-
if you are able to read each pdf with that library again then you may generate new pdf using same library and add all pages in it. – Mehul Joisar Nov 04 '14 at 14:07
-
i tried but nothing there to help me.can you suggest any other libraries or some codes to help me out ? – Abhi Nov 05 '14 at 07:24
-
post some code and let me know what exactly you are facing while implementing above approach – Mehul Joisar Nov 05 '14 at 07:29
-
i don't have any codes to post. Actually am looking for some.can anyone show me some ? – Abhi Nov 06 '14 at 11:42
1 Answers
1
I don't know how to do this for APW (Android PDF Writer), 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'

StefanTo
- 971
- 1
- 10
- 28
-
For Android use port: https://github.com/TomRoush/PdfBox-Android – Daniil Andashev Sep 21 '21 at 15:53