Knowledge Base and examples

Knowledge Base - How to add an image to a pdf? (Android)

This article shows how to add an image, from the device storage, to a pdf.

File file = new File(Environment.getExternalStorageDirectory(), "/test_image.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Page page = document.GetPage(0);
PageContent pageContent = new PageContent();
pageContent.Create();
pageContent.GSSave();
com.radaee.pdf.Matrix mat = new com.radaee.pdf.Matrix(200, 112.5f, 100, 300);
pageContent.GSSetMatrix(mat);
mat.Destroy();
Document.DocImage docimage = document.NewImage(bitmap, true);
ResImage rimage = page.AddResImage(docimage);
pageContent.DrawImage(rimage);
pageContent.GSRestore();
page.AddContent(pageContent, false);
pageContent.Destroy();
page.Close();
document.Save();

When you add a bitmap to a page starting from a dib or bmp image stored in memory, image data are stored as uncompressed stream, so you are experiencing the creation of an huge file.

To directly add .jpg to the pdf you should use Document.NewImageByMem with the below declaration:

/**
     * create an image from JPEG/JPG byte array.<br/>
     * supported image color space:<br/>
     * --GRAY<br/>
     * --RGB<br/>
     * --CMYK<br/>
     * a premium license is needed for this method.
     * @param data byte array include whole jpg file.
     * @param len byte length of data.
     * @return DocImage object or null.
     */
    public DocImage NewImageByMem( byte[] data, int len )
    {
        long ret = newImageJPEGByArray(hand_val, data, len);
        if( ret != 0 )
        {
            DocImage img = new DocImage();
            img.hand = ret;
            return img;
        }
        else return null;
    }
Applies To

RadaeePDF SDK for Android

Related Articles

How to add an image to a pdf? (iOS)

Details

Created : 2015-07-17 11:41:00, Last Modified : 2017-07-05 14:13:05