Creating Pdf File Using Itext From A Recyclerview (entire Items Inside) In Android?
Hi i was trying to create a PDF output file from a Recyclerview using the library iText . After hours of struggle i was able to create PDF from recylerview . Following is are class
Solution 1:
After hours of trail and error i found the answer . I am sharing the code snippets as it may be helpful for some else
publicvoidgeneratePDF(RecyclerView view) {
RecyclerView.Adapteradapter= view.getAdapter();
BitmapbigBitmap=null;
if (adapter != null) {
intsize= adapter.getItemCount();
intheight=0;
Paintpaint=newPaint();
intiHeight=0;
finalintmaxMemory= (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.finalintcacheSize= maxMemory / 8;
LruCache<String, Bitmap> bitmaCache = newLruCache<>(cacheSize);
for (inti=0; i < size; i++) {
RecyclerView.ViewHolderholder= adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.buildDrawingCache();
BitmapdrawingCache= holder.itemView.getDrawingCache();
if (drawingCache != null) {
bitmaCache.put(String.valueOf(i), drawingCache);
}
height += holder.itemView.getMeasuredHeight();
}
bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
CanvasbigCanvas=newCanvas(bigBitmap);
bigCanvas.drawColor(Color.WHITE);
Documentdocument=newDocument(PageSize.A4);
finalFilefile=newFile(getStorageDir("PDF"), "print.pdf");
try {
PdfWriter.getInstance(document, newFileOutputStream(file));
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
}
for (inti=0; i < size; i++) {
try {
//Adding the content to the documentBitmapbmp= bitmaCache.get(String.valueOf(i));
ByteArrayOutputStreamstream=newByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Imageimage= Image.getInstance(stream.toByteArray());
floatscaler= ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin() - 0) / image.getWidth()) * 100; // 0 means you have no indentation. If you have any, change it.
image.scalePercent(scaler);
image.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER | com.itextpdf.text.Image.ALIGN_TOP);
if (!document.isOpen()) {
document.open();
}
document.add(image);
} catch (Exception ex) {
Log.e("TAG-ORDER PRINT ERROR", ex.getMessage());
}
}
if (document.isOpen()) {
document.close();
}
// Set on UI Thread
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
AlertDialog.Builderbuilder=newAlertDialog.Builder(Index.this);
builder.setTitle("Success")
.setMessage("PDF File Generated Successfully.")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.ok, newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int whichButton) {
Intentintent=newIntent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
}).show();
}
});
}
}
Solution 2:
You saved my day. But i had to change this line :
image.scalePercent(70);
image.setAlignment(Image.MIDDLE);
with this :
float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin() - 0) / image.getWidth()) * 100; // 0 means you have no indentation. If you have any, change it.
image.scalePercent(scaler);
image.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER | com.itextpdf.text.Image.ALIGN_TOP);
Thank a lot
Post a Comment for "Creating Pdf File Using Itext From A Recyclerview (entire Items Inside) In Android?"