Ravikumar11
Ravikumar11

Reputation: 449

how to set background image to PDF page using iText?

I am using iText for creating pdf, i need to set background image to first pdf page but image is high resolution, how can set background image with out reduce image quality. Please help me.

Upvotes: 1

Views: 3428

Answers (1)

Ali Imran
Ali Imran

Reputation: 9217

there is work around for doing this by setting the image at absolute position and setting the page size equal to background image and don't forget to put the image in proper dpi folder :)

private void setBackground(Document document) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.background_img);
    bitmap.compress(Bitmap.CompressFormat.JPEG , 100, stream);
    Image img;
    try {
        img = Image.getInstance(stream.toByteArray());
        img.setAbsolutePosition(0, 0);

        document.add(img);
    } 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();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Upvotes: 3

Related Questions