I am trying to convert a ScrollView
to a Bitmap
, but the resulting image only contains part of the content.
This is how I create the image as a JPEG:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
byte[] img = stream.toByteArray();
I tried to create the bitmap
object the following ways:
Try 1 :
int totalWidth=((ScrollView)contentView).getChildAt(0).getWidth();
int totalHeight=((ScrollView)contentView).getChildAt(0).getHeight();
Bitmap bitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
contentView.layout(contentView.getLeft(), contentView.getTop(), contentView.getRight(), contentView.getBottom());
contentView.draw(c);
Try 2:
Bitmap bitmap = Bitmap.createBitmap(contentView.getWidth(), contentView.getHeight(), Bitmap.Config.ARGB_8888);
contentView.layout(contentView.getLeft(), contentView.getTop(), contentView.getRight(), contentView.getBottom());
contentView.draw(c);
Try 3 :
Bitmap bitmap = Bitmap.createBitmap(contentView.getMeasuredWidth(), contentView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
contentView.layout(contentView.getLeft(), contentView.getTop(), contentView.getRight(), contentView.getBottom());
contentView.draw(c);
Eventually, I add the image to a PDF like this:
public boolean saveReportAsPdf(Bitmap bitmap) {
try {
Image image;
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Reports";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
File file = new File(dir, "audit.pdf");
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
byte[] imageimg = Image.getInstance(stream.toByteArray());
image.setAlignment(Image.MIDDLE);
document.add(image);
document.close();
} catch (Exception i1) {
i1.printStackTrace();
}
return true;
}
This results in the following output:
This only shows the image up to row 7. As you can see below, there is also row 8 tot 13.
Row 8 to 13 are missing.
The problem is not your image. Based on the feedback you gave in the chat, the image is complete. However, your image is bigger than an A4 page, and you create a PDF document with a page that has size A4. This means that the image doesn't fit on the page.
You should create a PDF with a page that has the same size as the image. This is done like this:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
image = Image.getInstance(stream.toByteArray());
image.setAbsolutePosition(0, 0);
Document document = new Document(image);
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
document.add(image);
document.close();
Note the following line:
Document document = new Document(image);
This causes the page of the PDF document to have the same size as the image. As we add the image at position (0, 0)
, it will fit the page exactly.
P.S.: You are using the iText version for Java. If you work on Android, you should use the Android port. The Android port is called iTextG.