Reputation: 3
I have the following form, created using LinearLayout
Code:
private LinearLayout buildBox(Integer w){
LinearLayout.LayoutParams boxParams =
new LinearLayout.LayoutParams(
getDpToInt(w),
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout whiteBox = new LinearLayout(this.getContext());
whiteBox.setLayoutParams(boxParams);
whiteBox.setOrientation(LinearLayout.VERTICAL);
whiteBox.setBackgroundResource(R.color.colorAccent);
return whiteBox;
}
I need to convert this form after it is rendered to pdf or bitmap. On that same screen, you will have a button to print it. I tried some solutions found on the site, but I was not successful. Can someone help me?
Upvotes: 0
Views: 314
Reputation: 130
write code on pdf file button click like,
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("size"," "+llPdf.getWidth() +" "+llPdf.getWidth());
bitmap = loadBitmapFromView(llPdf, llPdf.getWidth(), llPdf.getHeight());
createPdfFile();
}
});
and implement these methods,
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
private void createPdfFile(){
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float hight = displaymetrics.heightPixels ;
float width = displaymetrics.widthPixels ;
int convertHighet = (int) hight, convertWidth = (int) width;
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, convertHighet, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawPaint(paint);
bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true);
paint.setColor(Color.BLUE);
canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);
String targetPdf = "/storage/mypdflayout.pdf";
File filePath;
filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
document.close();
Toast.makeText(this, "PDF is created successfully!!!", Toast.LENGTH_SHORT).show();
openGeneratedPDFFile();
}
private void openGeneratedPDFFile(){
File file = new File("/sdcard/pdffromlayout.pdf");
if (file.exists())
{
Intent i=new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
i.setDataAndType(uri, "application/pdf");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(i);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this, "No pdf view available", Toast.LENGTH_LONG).show();
}
}
}
Upvotes: 1