반응형
여러장의 이미지를 가지고 PDF를 만듬
1. itextpdf 라이브러리 추가
implementation 'com.itextpdf:itextg:5.5.9' //Bitmap to PDF
2.해당 파일들 리스트
//PDF만들기 테스트
private void goCreatePDF(){
GomsLog.d(TAG,"goCreatePDF()");
List<String> list = new ArrayList<String>();
list.add("cardmaker_1523253267458.jpg");
list.add("cardmaker_1523007611218.jpg");
list.add("cardmaker_1522906251381.jpg");
list.add("cardmaker_1521363256449.jpg");
list.add("cardmaker_1522118677235.jpg");
list.add("cardmaker_1521361016214.jpg");
list.add("cardmaker_1521190298381.jpg");
new CreatePdfTask(mMainActivity, list).execute();
}
3.itextpdf 라이브러리를 활용한 pdf 만들기
String fullFolder = Environment.getExternalStorageDirectory().getAbsolutePath() + mFolder;
mPath = fullFolder + "/" + mFilename + ".pdf";
Document document = new Document(PageSize.A4, 38, 38, 50, 38);
Rectangle documentRect = document.getPageSize();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(mPath));
document.open();
for (int i = 0; i < mImagesUri.size(); i++) {
Bitmap bmp = BitmapFactory.decodeFile(fullFolder + "/" + mImagesUri.get(i));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
image = Image.getInstance(fullFolder + "/" + mImagesUri.get(i));
if (bmp.getWidth() > documentRect.getWidth() || bmp.getHeight() > documentRect.getHeight()) {
//bitmap is larger than page,so set bitmap's size similar to the whole page
image.scaleAbsolute(documentRect.getWidth(), documentRect.getHeight());
} else {
//bitmap is smaller than page, so add bitmap simply.
//[note: if you want to fill page by stretching image,
// you may set size similar to page as above]
image.scaleAbsolute(bmp.getWidth(), bmp.getHeight());
}
image.setAbsolutePosition(
(documentRect.getWidth() - image.getScaledWidth()) / 2,
(documentRect.getHeight() - image.getScaledHeight()) / 2);
image.setBorder(Image.BOX);
image.setBorderWidth(15);
document.add(image);
document.newPage();
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
document.close();
오늘도 즐거운 코딩되세요
반응형
'Android Coding' 카테고리의 다른 글
Thread를 이용한 RecyclerView UI 변경 처리 (0) | 2018.12.13 |
---|---|
선택 시, Ripple 효과 (0) | 2018.10.19 |
Android Acitivty restart (0) | 2018.08.03 |
[NDK] chars To JString (0) | 2018.04.09 |
Thread UI 처리에 대한 샘플 (0) | 2017.11.29 |