First of all, I'm french and I will try my best to explain my problem in a good english.
So, I wanted to convert in PDF that screen : listeTrouActivity.java
But as you can see, there is scroll views and the hole column is going down to 18 holes.
So I followed some tutorials :
http://valokafor.com/how-to-convert-android-view-to-pdf/#comment-1018
And I started using the library ITextG
But my problem is that when I convert my bitmap into a PDF it give me a PDF with just the size of my screen. My activity is totally cut.
So I hope you guys can help me with that, there is my code that convert my bitmap into a PDF in my activity called ListTrouActivity.java:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
CoursePdf coursePdf = new CoursePdf();
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.saveHasPDF) {
View mRootView = findViewById(R.id.RootView);
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)){
Toast.makeText(ListTrouActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
File pdfDir = new File(DEFAULT_PATH, "MyApp");
if (!pdfDir.exists()){
pdfDir.mkdirs();
}
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout root = (LinearLayout) inflater.inflate(R.layout.liste_trou_activity, null); //RelativeLayout is root view of my UI(xml) file.
root.setDrawingCacheEnabled(true);
Bitmap screen = coursePdf.getBitmapFromView2(this.getWindow().findViewById(R.id.RootView));
// here give id of our root layout (here its my RelativeLayout's id)
File pdfFile = new File(pdfDir, "myPdfFile.pdf");
try {
Rectangle pagesize = new Rectangle(1720f, 3200f);
Document document = new Document(pagesize, 36f, 72f, 108f, 180f);
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
coursePdf.addImage(document,byteArray);
document.close();
}
catch (Exception e){
e.printStackTrace();
}
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(pdfDir, "myPdfFile.pdf"));
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Code for getBitmapFromView in CoursePDF.java :
public static Bitmap getBitmapFromView2(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
Code for addImage in my CoursePDF.java :
public static void addImage(Document document, byte[] byteArray)
{
Image image = null;
try
{
image = Image.getInstance(byteArray);
}
catch (BadElementException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// image.scaleAbsolute(150f, 150f);
try
{
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Unfortunately I can't past my liste_trou_activity.xml code here or I will reach 157 000 characters.
If you guys really need it just ask and I will try again.
I hope you all understand what I'm asking for, if not, just ask me again and I'll try to explain again.
Thanks for reading, hope you guys can help me.
See what I could think of the best was taking snapshot of complete scrollable activity and then writing a pdf dynamically with its content as snapshot . For snapshot prefer [Taking snapshot of scrollable activity]
Then write dynamic pdf. You will get what u want