본문 바로가기

IT개발일지/Android

[Android] PDF 파일 합치기 merge

public static File getMergePDF(ArrayList<File> fileList, String wantFileName, final Activity act) {
final File outputFile = new File(Cvalue._DOWNLOAD_FOLDER + "/" + wantFileName);
// Log.d(CommonUtil.TAG, "outputFile1 > " + outputFile.getAbsolutePath() + " / " + outputFile.length());
try {
List<InputStream> pdfs = new ArrayList<InputStream>();
for (int i = 0; i < fileList.size(); i++) {
pdfs.add(new FileInputStream(fileList.get(i)));
Log.d(CommonUtil.TAG, "파일 리스트 : " + fileList.get(i).getAbsolutePath());
}
OutputStream output = new FileOutputStream(outputFile);
//concatPDFs
Document document = new Document(PageSize.A4);
try {
List<PdfReader> readers = new ArrayList<PdfReader>();
int totalPage = 0;
//create Reader for PDF Files
for (InputStream input : pdfs) {
PdfReader newReader = new PdfReader(input);
readers.add(newReader);
totalPage += newReader.getNumberOfPages();
}
//create writer for Output
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();

BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent(); // Holds the PDF

//data
PdfImportedPage page;
int currentPage = 0;
int pageOfCurrentReaderPDF = 0;

//add pages
for (int index = 0; index < readers.size(); index++) {
PdfReader reader = readers.get(index);
while (pageOfCurrentReaderPDF < reader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF += 1;
currentPage += 1;
page = writer.getImportedPage(reader, pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);

// Log.i("TEST", "page > " + currentPage + " of " + totalPage);
}

File file = fileList.get(index);
pageOfCurrentReaderPDF = 0;
if (file.exists()) {
// Log.i("TEST", "file["+index+"] > " + file.getName() + " delete");
//file.delete();
} else {
// Log.i("TEST", "file["+index+"] > not exist");
}
}
output.flush();
document.close();
output.close();
} catch (IOException ioe) {
ioe.printStackTrace();
//ERROR_MSG = "AUTO_INPUT_OUTPUT_ERROR";
Log.d(CommonUtil.TAG, "예외처리");
/*if (act != null && act instanceof Activity) {
((Activity) act).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(act, "파일생성중 문제가 발생하였습니다1", Toast.LENGTH_SHORT).show();
}
});
}*/
} catch (DocumentException e) {
e.printStackTrace();
Log.d(CommonUtil.TAG, "예외처리2");
//ERROR_MSG = "DOCUMENT_ERROR";
/*if (act != null && act instanceof Activity) {
((Activity) act).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(act, "파일생성중 문제가 발생하였습니다2", Toast.LENGTH_SHORT).show();
}
});
}*/
} finally {
if (document.isOpen()) {
document.close();
}
try {
if (output != null) {
output.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
Log.d(CommonUtil.TAG, "예외처리3");
}
}
} catch (IOException e) {
e.printStackTrace();
Log.d(CommonUtil.TAG, "예외처리4");
}
Log.d(CommonUtil.TAG, "outputFile2 > " + outputFile.getAbsolutePath() + " / " + outputFile.length());
return outputFile;
}



여러장의 PDF 파일을 다운받아 합치기 <MERGE> 하는 방법이다.