> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/get-started/samples/addimagetest.md).

# AddImage

Sample Java code to use Apryse SDK for inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document.

Sample Java code to use Apryse SDK for programmatically inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document. Learn more about our [Android SDK](/core/get-started/languages/java.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples;

import com.pdftron.android.pdfnetsdksamples.OutputListener;
import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
import com.pdftron.android.pdfnetsdksamples.R;
import com.pdftron.android.pdfnetsdksamples.util.Utils;
import com.pdftron.common.Matrix2D;
import com.pdftron.common.PDFNetException;
import com.pdftron.pdf.Element;
import com.pdftron.pdf.ElementBuilder;
import com.pdftron.pdf.ElementWriter;
import com.pdftron.pdf.Font;
import com.pdftron.pdf.Image;
import com.pdftron.pdf.PDFDoc;
import com.pdftron.pdf.Page;
import com.pdftron.pdf.Rect;
import com.pdftron.sdf.Obj;
import com.pdftron.sdf.ObjSet;
import com.pdftron.sdf.SDFDoc;

import java.util.ArrayList;

public class AddImageTest extends PDFNetSample {

	private static OutputListener mOutputListener;

	private static ArrayList<String> mFileList = new ArrayList<>();

    public AddImageTest() {
        setTitle(R.string.sample_addimage_title);
        setDescription(R.string.sample_addimage_description);
    }

	@Override
	public void run(OutputListener outputListener) {
		super.run(outputListener);
		mOutputListener = outputListener;
		mFileList.clear();
		printHeader(outputListener);

		try (PDFDoc doc = new PDFDoc())
		{
			ElementBuilder f = new ElementBuilder(); // Used to build new Element objects
			ElementWriter writer = new ElementWriter(); // Used to write Elements to the page
			
			Page page = doc.pageCreate(); // Start a new page
			writer.begin(page); // Begin writing to this page
			
			// ----------------------------------------------------------
			// Add JPEG image to the output file
			Image img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath());
			Element element = f.createImage(img, 50, 500, img.getImageWidth()/2, img.getImageHeight()/2);
			writer.writePlacedElement(element);

			// ----------------------------------------------------------
			// Add a PNG image to the output file
			img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "butterfly.png").getAbsolutePath());
			element = f.createImage(img, new Matrix2D(100, 0, 0, 100, 300, 500));
			writer.writePlacedElement(element);
			
			// ----------------------------------------------------------
			// Add a GIF image to the output file
			img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "pdfnet.gif").getAbsolutePath());
			element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 50, 350));
			writer.writePlacedElement(element);
			
			// ----------------------------------------------------------
			// Add a TIFF image to the output file
			img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "grayscale.tif").getAbsolutePath());
			element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 10, 50));
			writer.writePlacedElement(element);

			writer.end();           // Save the page
			doc.pagePushBack(page); // Add the page to the document page sequence
			
			// ----------------------------------------------------------
			// Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.

			page = doc.pageCreate(new Rect(0, 0, 612, 794));
			writer.begin(page); // begin writing to this page
			
			// Note: encoder hints can be used to select between different compression methods.
			// For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
			ObjSet hint_set = new ObjSet();
			Obj enc = hint_set.createArray();  // Initilaize encoder 'hint' parameter
			enc.pushBackName("JBIG2");
			enc.pushBackName("Lossy");

			img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "multipage.tif").getAbsolutePath());
			element = f.createImage(img, new Matrix2D(612, 0, 0, 794, 0, 0));
			writer.writePlacedElement(element);

			writer.end();           // Save the page
			doc.pagePushBack(page); // Add the page to the document page sequence

			// ----------------------------------------------------------
			// Add a JPEG2000 (JP2) image to the output file

			// Create a new page
			page = doc.pageCreate();
			writer.begin(page); // Begin writing to the page

			// Embed the image.
			img = Image.create(doc.getSDFDoc(), Utils.getAssetTempFile(INPUT_PATH + "palm.jp2").getAbsolutePath());
			
			// Position the image on the page.
			element = f.createImage(img, new Matrix2D(img.getImageWidth(), 0, 0, img.getImageHeight(), 96, 80));
			writer.writePlacedElement(element);

			// Write 'JPEG2000 Sample' text string under the image.
			writer.writeElement(f.createTextBegin(Font.create(doc.getSDFDoc(), Font.e_times_roman), 32));
			element = f.createTextRun("JPEG2000 Sample");
			element.setTextMatrix(1, 0, 0, 1, 190, 30);
			writer.writeElement(element);
			writer.writeElement(f.createTextEnd());
			
			writer.end(); // Finish writing to the page
			doc.pagePushBack(page);

			// ----------------------------------------------------------
			// doc.Save((Utils.createExternalFile("addimage.pdf", mFileList).getAbsolutePath()).c_str(), Doc.e_remove_unused, 0);
			doc.save((Utils.createExternalFile("addimage.pdf", mFileList).getAbsolutePath()), SDFDoc.SaveMode.LINEARIZED, null);
			mOutputListener.println("Done. Result saved in addimage.pdf...");
		}
		catch (PDFNetException e)
		{
			mOutputListener.printError(e.getStackTrace());
			mOutputListener.printError(e.getStackTrace());
		}

		for (String file : mFileList) {
			addToFileList(file);
		}
		printFooter(outputListener);
	}

}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples

import com.pdftron.android.pdfnetsdksamples.OutputListener
import com.pdftron.android.pdfnetsdksamples.PDFNetSample
import com.pdftron.android.pdfnetsdksamples.R
import com.pdftron.android.pdfnetsdksamples.util.Utils
import com.pdftron.common.Matrix2D
import com.pdftron.common.PDFNetException
import com.pdftron.pdf.*
import com.pdftron.sdf.ObjSet
import com.pdftron.sdf.SDFDoc
import java.util.*

class AddImageTest : PDFNetSample() {
    init {
        setTitle(R.string.sample_addimage_title)
        setDescription(R.string.sample_addimage_description)
    }

    override fun run(outputListener: OutputListener?) {
        super.run(outputListener)
        mOutputListener = outputListener
        mFileList.clear()
        printHeader(outputListener!!)

        try {

            PDFDoc().use { doc ->
                val f = ElementBuilder() // Used to build new Element objects
                val writer = ElementWriter() // Used to write Elements to the page

                var page = doc.pageCreate() // Start a new page
                writer.begin(page) // Begin writing to this page

                // ----------------------------------------------------------
                // Add JPEG image to the output file
                var img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "peppers.jpg")!!.absolutePath)
                var element = f.createImage(img, 50.0, 500.0, (img.imageWidth / 2).toDouble(), (img.imageHeight / 2).toDouble())
                writer.writePlacedElement(element)

                // ----------------------------------------------------------
                // Add a PNG image to the output file
                img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "butterfly.png")!!.absolutePath)
                element = f.createImage(img, Matrix2D(100.0, 0.0, 0.0, 100.0, 300.0, 500.0))
                writer.writePlacedElement(element)

                // ----------------------------------------------------------
                // Add a GIF image to the output file
                img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "pdfnet.gif")!!.absolutePath)
                element = f.createImage(img, Matrix2D(img.imageWidth.toDouble(), 0.0, 0.0, img.imageHeight.toDouble(), 50.0, 350.0))
                writer.writePlacedElement(element)

                // ----------------------------------------------------------
                // Add a TIFF image to the output file
                img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "grayscale.tif")!!.absolutePath)
                element = f.createImage(img, Matrix2D(img.imageWidth.toDouble(), 0.0, 0.0, img.imageHeight.toDouble(), 10.0, 50.0))
                writer.writePlacedElement(element)

                writer.end()           // Save the page
                doc.pagePushBack(page) // Add the page to the document page sequence

                // ----------------------------------------------------------
                // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.

                page = doc.pageCreate(Rect(0.0, 0.0, 612.0, 794.0))
                writer.begin(page) // begin writing to this page

                // Note: encoder hints can be used to select between different compression methods.
                // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
                val hint_set = ObjSet()
                val enc = hint_set.createArray()  // Initilaize encoder 'hint' parameter
                enc.pushBackName("JBIG2")
                enc.pushBackName("Lossy")

                img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "multipage.tif")!!.absolutePath)
                element = f.createImage(img, Matrix2D(612.0, 0.0, 0.0, 794.0, 0.0, 0.0))
                writer.writePlacedElement(element)

                writer.end()           // Save the page
                doc.pagePushBack(page) // Add the page to the document page sequence

                // ----------------------------------------------------------
                // Add a JPEG2000 (JP2) image to the output file

                // Create a new page
                page = doc.pageCreate()
                writer.begin(page) // Begin writing to the page

                // Embed the image.
                img = Image.create(doc.sdfDoc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "palm.jp2")!!.absolutePath)

                // Position the image on the page.
                element = f.createImage(img, Matrix2D(img.imageWidth.toDouble(), 0.0, 0.0, img.imageHeight.toDouble(), 96.0, 80.0))
                writer.writePlacedElement(element)

                // Write 'JPEG2000 Sample' text string under the image.
                writer.writeElement(f.createTextBegin(Font.create(doc.sdfDoc, Font.e_times_roman), 32.0))
                element = f.createTextRun("JPEG2000 Sample")
                element.setTextMatrix(1.0, 0.0, 0.0, 1.0, 190.0, 30.0)
                writer.writeElement(element)
                writer.writeElement(f.createTextEnd())

                writer.end() // Finish writing to the page
                doc.pagePushBack(page)

                // ----------------------------------------------------------
                // doc.Save((Utils.createExternalFile("addimage.pdf", mFileList).getAbsolutePath()).c_str(), Doc.e_remove_unused, 0);
                doc.save(Utils.createExternalFile("addimage.pdf", mFileList).absolutePath, SDFDoc.SaveMode.LINEARIZED, null)
                mOutputListener!!.println("Done. Result saved in addimage.pdf...")
            }
        } catch (e: PDFNetException) {
            mOutputListener!!.printError(e.stackTrace)
            mOutputListener!!.printError(e.stackTrace)
        }

        for (file in mFileList) {
            addToFileList(file)
        }
        printFooter(outputListener)
    }

    companion object {

        private var mOutputListener: OutputListener? = null

        private val mFileList = ArrayList<String>()
    }

}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/get-started/samples/addimagetest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
