2

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:

Part of the image shown inside a PDF

This only shows the image up to row 7. As you can see below, there is also row 8 tot 13.

enter image description here

Row 8 to 13 are missing.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Rakesh L
  • 1,136
  • 4
  • 18
  • 44
  • Can you clarify your question? Are you saying the PDF should contain a scrollbar? Are you saying the page you defined is too small for the image? It isn't clear. Why are you creating an A4 page in PDF? Why don't you adapt the page size to the image size (or the image size to the page size)? – Bruno Lowagie Mar 23 '15 at 07:05
  • I would like to display full scrollview content into PDF .But , I could able to see only till the view displayed on the screen in the PDF . @BrunoLowagie – Rakesh L Mar 23 '15 at 08:56
  • I have to convert whole view inside Scroll View which is focused or not focused in the display ,into Bitmap and have to convert that bitmap into PDF file .I successfully can create the pdf. But, the view which is not focused is hidden in PDF . If I set scroll in PDF, will it resolve ? . If so, can you give any idea on how to set scroll in pdf ? @BrunoLowagie – Rakesh L Mar 23 '15 at 09:39
  • That is my issue .The Bitmap I convert does not have full image . I have tried in three ways to get it as mentioned in try 1,try 2, try 3 . @BrunoLowagie – Rakesh L Mar 23 '15 at 09:53
  • Then I misunderstood the question: it is **not** an iText question. It is a Bitmap question. I have removed my answer. I'll probably also update your question as it is misleading in its current state. – Bruno Lowagie Mar 23 '15 at 09:56
  • I have removed all the references that would fool people into thinking this is a PDF problem. The image is still misleading as row 8 is probably present in the image, but it is not visible in the PDF because you have create a page that is smaller than the image (which is a problem that was solved in the answer I have now deleted). – Bruno Lowagie Mar 23 '15 at 10:05
  • It would help if you show what is missing in your image. Right now it isn't clear. For instance: I thought that row 8 was missing, but when I look at your image, I am pretty sure it isn't (in spite of the fact I that I can't see it). – Bruno Lowagie Mar 23 '15 at 10:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73571/discussion-between-rakesh-l-and-bruno-lowagie). – Rakesh L Mar 23 '15 at 10:56

1 Answers1

3

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165