How Can I Combine Multiple Pdf To Convert Single Pdf In Android?
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
Solution 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 {
PDFMergerUtilityut=newPDFMergerUtility();
ut.addSource(NetworkUtils.downloadFile(urlToPdf1, 20));
ut.addSource(NetworkUtils.downloadFile(urlToPdf2, 20));
ut.addSource(NetworkUtils.downloadFile(urlToPdf3, 20));
finalFilefile=newFile(getContext().getExternalCacheDir(), System.currentTimeMillis() + ".pdf");
finalFileOutputStreamfileOutputStream=newFileOutputStream(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:
publicstatic InputStream downloadFileThrowing(String url, int timeOutInSeconds)throws IOException {
OkHttpClientclient=newOkHttpClient();
client.setConnectTimeout(timeOutInSeconds, TimeUnit.SECONDS);
client.setReadTimeout(timeOutInSeconds, TimeUnit.SECONDS);
Requestrequest=newRequest.Builder().url(url).build();
Responseresponse= client.newCall(request).execute();
if (!response.isSuccessful())
thrownewIOException("Download not successful.response:" + response);
elsereturn response.body().byteStream();
}
To use the OkHttpClient add this to your build.gradle:
compile'com.squareup.okhttp:okhttp:2.7.2'
Post a Comment for "How Can I Combine Multiple Pdf To Convert Single Pdf In Android?"