> 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/core/get-started/samples/addimagetest.md).

# Add Image to PDF - AddImage

Sample 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;  provided in Python, C++, C#, Java, Node.js (JavaS

Sample 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. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our [Server SDK](/core/get-started/get-started.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
//
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
//

using System;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

namespace AddImageTestCS
{
	class Class1
	{
		private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
		static Class1() {}

		/// <summary>
		//-----------------------------------------------------------------------------------
		// This sample illustrates how to embed various raster image formats 
		// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
		//-----------------------------------------------------------------------------------
		/// </summary>
		static void Main(string[] args)
		{
			PDFNet.Initialize(PDFTronLicense.Key);

			// Relative path to the folder containing test files.
			string input_path =  "../../../../TestFiles/";
			string output_path = "../../../../TestFiles/Output/";

			try
			{
				using (PDFDoc doc = new PDFDoc())
				using (ElementBuilder bld = new ElementBuilder())	// Used to build new Element objects
				using (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

					// ----------------------------------------------------------
					// Embed a JPEG image to the output document. 
					Image img = Image.Create(doc, input_path + "peppers.jpg");

					// You can also directly add any .NET Bitmap. The following commented-out code 
					// is equivalent to the above line:
					//	System.Drawing.Bitmap bmp;
					//	System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(input_path + "peppers.jpg");
					//	Image img = Image.Create(doc, bmp);

					Element element = bld.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, input_path + "butterfly.png");
					element = bld.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, input_path + "pdfnet.gif");
					element = bld.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, input_path + "grayscale.tif");
					element = bld.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

					// ----------------------------------------------------------
					// Add a BMP image to the output file
					/*
					bmp = new System.Drawing.Bitmap(input_path + "pdftron.bmp");
					img = Image.Create(doc, bmp);
					element = bld.CreateImage(img, new Matrix2D(bmp.Width, 0, 0, bmp.Height, 255, 700));
					writer.WritePlacedElement(element);
			
					writer.End();	// Finish writing to the page
					doc.PagePushBack(page);
					*/

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

					page = doc.PageCreate(new pdftron.PDF.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();  // Initialize encoder 'hint' parameter 
					enc.PushBackName("JBIG2");
					enc.PushBackName("Lossy");

					img = pdftron.PDF.Image.Create(doc, input_path + "multipage.tif", enc);
					element = bld.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 = pdftron.PDF.Image.Create(doc, input_path + "palm.jp2");

					// Position the image on the page.
					element = bld.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(bld.CreateTextBegin(pdftron.PDF.Font.Create(doc, pdftron.PDF.Font.StandardType1Font.e_times_roman), 32));
					element = bld.CreateTextRun("JPEG2000 Sample");
					element.SetTextMatrix(1, 0, 0, 1, 190, 30);
					writer.WriteElement(element);
					writer.WriteElement(bld.CreateTextEnd());

					writer.End();   // Finish writing to the page
					doc.PagePushBack(page);


					doc.Save(output_path + "addimage.pdf", SDFDoc.SaveOptions.e_linearized);
					PDFNet.Terminate();
					Console.WriteLine("Done. Result saved in addimage.pdf...");
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

		}
	}
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/ElementBuilder.h>
#include <PDF/ElementWriter.h>
#include <PDF/ElementReader.h>
#include <PDF/Image.h>
#include <Filters/MappedFile.h>
#include <Filters/FilterReader.h>
#include <SDF/ObjSet.h>
#include "../../LicenseKey/CPP/LicenseKey.h"

#include <iostream>

using namespace std;

using namespace pdftron;
using namespace Common;
using namespace SDF;
using namespace PDF;

//-----------------------------------------------------------------------------------
// This sample illustrates how to embed various raster image formats
// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
//
// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
// be present in the system path.
//-----------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
	int ret = 0;
	PDFNet::Initialize(LicenseKey);

	// Relative path to the folder containing test files.
	string input_path =  "../../TestFiles/";
	string output_path = "../../TestFiles/Output/";

	try  
	{
		PDFDoc doc;

		ElementBuilder f;		// Used to build new Element objects
		ElementWriter writer;	// 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
		PDF::Image img = PDF::Image::Create(doc, (input_path + "peppers.jpg").c_str());
		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 = PDF::Image::Create(doc, (input_path + "butterfly.png").c_str());
		element = f.CreateImage(img, Matrix2D(100, 0, 0, 100, 300, 500));
		writer.WritePlacedElement(element);

		// ----------------------------------------------------------
		// Add a GIF image to the output file
		img = PDF::Image::Create(doc, (input_path + "pdfnet.gif").c_str());
		element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350));
		writer.WritePlacedElement(element);	

		// ----------------------------------------------------------
		// Add a TIFF image to the output file

		img = PDF::Image::Create(doc, (input_path + "grayscale.tif").c_str());
		element = f.CreateImage(img, 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(PDF::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;
		Obj enc=hint_set.CreateArray();  // Initialize encoder 'hint' parameter 
		enc.PushBackName("JBIG2");
		enc.PushBackName("Lossy");

		img = PDF::Image::Create(doc, (input_path + "multipage.tif").c_str(), enc);
		element = f.CreateImage(img, 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, (input_path + "palm.jp2").c_str());

		// Position the image on the page.
		element = f.CreateImage(img, 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, 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((output_path + "addimage.pdf").c_str(), SDFDoc::e_linearized, 0);
		cout << "Done. Result saved in addimage.pdf..." << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	PDFNet::Terminate();
	return ret;
}
```

{% endcode %}
{% endtab %}

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

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.pdftron.common.Matrix2D;
import com.pdftron.common.PDFNetException;
import com.pdftron.pdf.*;
import com.pdftron.sdf.SDFDoc;
import com.pdftron.sdf.ObjSet;
import com.pdftron.sdf.Obj;

//-----------------------------------------------------------------------------------
// This sample illustrates how to embed various raster image formats
// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
//
// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
// be present in the system path.
//-----------------------------------------------------------------------------------
public class AddImageTest {

	public static void main(String[] args) 
	{
		PDFNet.initialize(PDFTronLicense.Key());

		// Relative path to the folder containing test files.
		String input_path = "../../TestFiles/";
		String output_path = "../../TestFiles/Output/";

		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(), input_path + "peppers.jpg");
			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(), input_path + "butterfly.png");
			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(), input_path + "pdfnet.gif");
			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(), input_path + "grayscale.tif");
			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(), input_path + "multipage.tif", enc);
			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(), input_path + "palm.jp2");
			
			// 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((output_path + "addimage.pdf").c_str(), Doc.e_remove_unused, 0);
			doc.save((output_path + "addimage.pdf"), SDFDoc.SaveMode.LINEARIZED, null);
			System.out.println("Done. Result saved in addimage.pdf...");
		}
		catch (PDFNetException e)
		{
			e.printStackTrace();
			System.out.println(e);
		}

		PDFNet.terminate();
	}
}
```

{% endcode %}
{% endtab %}

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

```go
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------

package main
import (
	"fmt"
	. "pdftron"
)

import  "pdftron/Samples/LicenseKey/GO"

//-----------------------------------------------------------------------------------
// This sample illustrates how to embed various raster image formats
// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
//
// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
// be present in the system path.
//-----------------------------------------------------------------------------------

func main(){
	PDFNetInitialize(PDFTronLicense.Key)
	// Relative path to the folder containing test files.
	var inputPath = "../../TestFiles/"
	var outputPath = "../../TestFiles/Output/"
	doc := NewPDFDoc()
	f := NewElementBuilder()			// Used to build new Element objects
	writer := NewElementWriter()		// Used to write Elements to the page
	page := doc.PageCreate()					// Start a new page
	writer.Begin(page)							// Begin writing to this page
    // ----------------------------------------------------------
    // Add JPEG image to the output file
	img := ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
    element := f.CreateImage(img, 50.0, 500.0, float64(img.GetImageWidth()/2), float64(img.GetImageHeight()/2))
    writer.WritePlacedElement(element)
    
	// ----------------------------------------------------------
    // Add a PNG image to the output file    
    img = ImageCreate(doc.GetSDFDoc(), inputPath + "butterfly.png")
    element = f.CreateImage(img, NewMatrix2D(100.0, 0.0, 0.0, 100.0, 300.0, 500.0))
    writer.WritePlacedElement(element)
    
    //----------------------------------------------------------
    // Add a GIF image to the output file
    img = ImageCreate(doc.GetSDFDoc(), inputPath + "pdfnet.gif")
    element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 50.0, 350.0))
    writer.WritePlacedElement(element)
    
    // ----------------------------------------------------------
    // Add a TIFF image to the output file
    
    img = ImageCreate(doc.GetSDFDoc(), (inputPath + "grayscale.tif"))
    element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 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(NewRect(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.
    hintSet := NewObjSet();
    enc := hintSet.CreateArray();  // Initilaize encoder 'hint' parameter 
    enc.PushBackName("JBIG2");
    enc.PushBackName("Lossy");

    img = ImageCreate(doc.GetSDFDoc(), inputPath + "multipage.tif", enc);
    element = f.CreateImage(img, NewMatrix2D(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 = ImageCreate(doc.GetSDFDoc(), inputPath + "palm.jp2")
    
    // Position the image on the page
    element = f.CreateImage(img, NewMatrix2D(float64(img.GetImageWidth()), 0.0, 0.0, float64(img.GetImageHeight()), 96.0, 80.0))
    writer.WritePlacedElement(element)
    
    // Write 'JPEG2000 Sample' text string under the image
    writer.WriteElement(f.CreateTextBegin(FontCreate(doc.GetSDFDoc(), FontE_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()
	doc.PagePushBack(page)

	doc.Save((outputPath + "addimage.pdf"), uint(SDFDocE_linearized));
    doc.Close()

	PDFNetTerminate()
    fmt.Println("Done. Result saved in addimage.pdf...")
}
```

{% endcode %}
{% endtab %}

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

```js
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------


const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {

  exports.runAddImageTest = () => {

    const main = async() => {
      try {
        // Relative path to the folder containing test files.
        const inputURL = '../TestFiles/';

        const doc = await PDFNet.PDFDoc.create();
        doc.initSecurityHandler();

        const builder = await PDFNet.ElementBuilder.create(); // ElementBuilder, used to build new element Objects
        // create a new page writer that allows us to add/change page elements
        const writer = await PDFNet.ElementWriter.create(); // ElementWriter, used to write elements to the page
        // define new page dimensions
        let page = await doc.pageCreate();

        writer.beginOnPage(page, PDFNet.ElementWriter.WriteMode.e_overlay);

        // Adding a JPEG image to output file
        let img = await PDFNet.Image.createFromFile(doc, inputURL + 'peppers.jpg');
        let imgWidth = await img.getImageWidth();
        let imgHeight = await img.getImageHeight();
        let element = await builder.createImageScaled(img, 50, 500, imgWidth/2, imgHeight/2);
        writer.writePlacedElement(element);

        // Add a PNG to output file
        img = await PDFNet.Image.createFromFile(doc, inputURL + 'butterfly.png');
        matrix = await PDFNet.Matrix2D.create(100, 0, 0, 100, 300, 500);
        element = await builder.createImageFromMatrix(img, matrix);
        writer.writePlacedElement(element);

        // Add a GIF image to the output file
        img = await PDFNet.Image.createFromFile(doc, inputURL + 'pdfnet.gif');
        imgWidth = await img.getImageWidth();
        imgHeight = await img.getImageHeight();
        matrix = await PDFNet.Matrix2D.create(imgWidth, 0, 0, imgHeight, 50, 350);
        element = await builder.createImageFromMatrix(img, matrix);
        writer.writePlacedElement(element);

        // Add a TIFF image to the output file
        img = await PDFNet.Image.createFromFile(doc, inputURL + 'grayscale.tif');
        imgWidth = await img.getImageWidth();
        imgHeight = await img.getImageHeight();
        matrix = await PDFNet.Matrix2D.create(imgWidth, 0, 0, imgHeight, 10, 50);
        element = await builder.createImageFromMatrix(img, matrix);
        writer.writePlacedElement(element);

        writer.end();
        doc.pagePushBack(page);

        // Embed monochrome TIFF compressed using lossy JBIG2 filter
        const pageRect = await PDFNet.Rect.init(0, 0, 612, 794);
        page = await doc.pageCreate(pageRect);
        writer.beginOnPage(page);

        const hintSet = await PDFNet.ObjSet.create();
        const enc = await hintSet.createArray();
        await enc.pushBackName('JBIG2');
        await enc.pushBackName('Lossy');

        img = await PDFNet.Image.createFromFile(doc, inputURL + 'multipage.tif', enc);
        matrix = await PDFNet.Matrix2D.create(612, 0, 0, 794, 0, 0);
        element = await builder.createImageFromMatrix(img, matrix);
        writer.writePlacedElement(element);

        writer.end();
        doc.pagePushBack(page);

        // Add a JPEG200 to output file
        page = await doc.pageCreate();
        writer.beginOnPage(page);

        img = await PDFNet.Image.createFromFile(doc, inputURL + 'palm.jp2');
        imgWidth = await img.getImageWidth();
        imgHeight = await img.getImageHeight();
        matrix = await PDFNet.Matrix2D.create(imgWidth, 0, 0, imgHeight, 96, 80);
        element = await builder.createImageFromMatrix(img, matrix);
        writer.writePlacedElement(element);

        // write 'JPEG2000 Sample' text under image
        const timesFont = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_times_roman);
        writer.writeElement(await builder.createTextBeginWithFont(timesFont, 32));
        element = await builder.createNewTextRun('JPEG2000 Sample');
        matrix = await PDFNet.Matrix2D.create(1, 0, 0, 1, 190, 30);
        await element.setTextMatrix(matrix);
        writer.writeElement(element);
        const element2 = await builder.createTextEnd();
        writer.writeElement(element2);

        await writer.end();
        doc.pagePushBack(page); // add the page to the document

        await doc.save(inputURL + 'Output/addimage.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);

        console.log('Done. Result saved in addimage.pdf...');
      } catch (err) {
        console.log(err);
      }
    };
    PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
  };
  exports.runAddImageTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=AddImageTest.js
```

{% endcode %}
{% endtab %}

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

```php
<?php
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------
if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
include("../../../PDFNetC/Lib/PDFNetPHP.php");
include("../../LicenseKey/PHP/LicenseKey.php");

//-----------------------------------------------------------------------------------
// This sample illustrates how to embed various raster image formats
// (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
//
// Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
// be present in the system path.
//-----------------------------------------------------------------------------------
	
	PDFNet::Initialize($LicenseKey);
	PDFNet::GetSystemFontList();    // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.

	// Relative path to the folder containing the test files.
	$input_path = getcwd()."/../../TestFiles/";
	$output_path = $input_path."Output/";

	$doc = new PDFDoc();
	$builder = new ElementBuilder();	// Used to build new Element objects
	$writer = new ElementWriter();		// Used to write Elements to the page
	
	$page = $doc->PageCreate();		// Start a new page
	$writer->Begin($page);			// Begin writing to this page
	
	// ----------------------------------------------------------
    	// Add JPEG image to the output file
    	$img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
    	$element = $builder->CreateImage($img, 50.0, 500.0, (double)($img->GetImageWidth())/2, (double)($img->GetImageHeight())/2);
    	$writer->WritePlacedElement($element);

   	// ----------------------------------------------------------
    	// Add a PNG image to the output file
    	$img = Image::Create($doc->GetSDFDoc(), $input_path."butterfly.png");
    	$element = $builder->CreateImage($img, new 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->GetSDFDoc(), $input_path."pdfnet.gif");
    	$element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 50.0, 350.0));
    	$writer->WritePlacedElement($element);
    
    	// ----------------------------------------------------------
    	// Add a TIFF image to the output file
  
    	$img = Image::Create($doc->GetSDFDoc(), $input_path."grayscale.tif");
    	$element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 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(new 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.
    	$hint_set = new ObjSet();
    	$enc = $hint_set->CreateArray();  // Initilaize encoder 'hint' parameter 
    	$enc->PushBackName("JBIG2");
    	$enc->PushBackName("Lossy");

    	$img = Image::Create($doc->GetSDFDoc(), $input_path."multipage.tif", $enc);
    	$element = $builder->CreateImage($img, new 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->GetSDFDoc(), $input_path."palm.jp2");
    
    	// Position the image on the page
    	$element = $builder->CreateImage($img, new Matrix2D((double)($img->GetImageWidth()), 0.0, 0.0, (double)($img->GetImageHeight()), 96.0, 80.0));
    	$writer->WritePlacedElement($element);
    
    	// Write 'JPEG2000 Sample' text string under the image
    	$writer->WriteElement($builder->CreateTextBegin(Font::Create($doc->GetSDFDoc(), Font::e_times_roman), 32.0));
    	$element = $builder->CreateTextRun("JPEG2000 Sample");
    	$element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 190.0, 30.0);
    	$writer->WriteElement($element);
    	$writer->WriteElement($builder->CreateTextEnd());
    	
    	$writer->End();                   // Finish writing to the page
    	$doc->PagePushBack($page);
    
    	$doc->Save(($output_path."addimage.pdf"), SDFDoc::e_linearized);
    	$doc->Close();
		PDFNet::Terminate();
    	echo nl2br("Done. Result saved in addimage.pdf...\n");
?>
```

{% endcode %}
{% endtab %}

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

```python
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

import site
site.addsitedir("../../../PDFNetC/Lib")
import sys
from PDFNetPython import *

sys.path.append("../../LicenseKey/PYTHON")
from LicenseKey import *

#-----------------------------------------------------------------------------------
# This sample illustrates how to embed various raster image formats
# (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
#
# Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
# be present in the system path.
#-----------------------------------------------------------------------------------

def main():
    PDFNet.Initialize(LicenseKey)
    
    # Relative path to the folder containing test files.
    input_path = "../../TestFiles/"
    output_path = "../../TestFiles/Output/"
    
    doc = PDFDoc()
    
    f = ElementBuilder()            # Used to build new Element objects
    writer = ElementWriter()        # Used to write Elements to the page
    
    page = doc.PageCreate()         # Start a new page
    writer.Begin(page)              # Begin writing to this page

    # ----------------------------------------------------------
    # Add JPEG image to the output file
    img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
    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(), input_path + "butterfly.png")
    element = f.CreateImage(img, Matrix2D(100, 0, 0, 100, 300, 500))
    writer.WritePlacedElement(element)
    
    # ----------------------------------------------------------
    # Add a GIF image to the output file
    img = Image.Create(doc.GetSDFDoc(), input_path + "pdfnet.gif")
    element = f.CreateImage(img, 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(), (input_path + "grayscale.tif"))
    element = f.CreateImage(img, 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(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.
    hint_set = ObjSet();
    enc = hint_set.CreateArray();  # Initilaize encoder 'hint' parameter 
    enc.PushBackName("JBIG2");
    enc.PushBackName("Lossy");

    img = Image.Create(doc.GetSDFDoc(), input_path + "multipage.tif", enc);
    element = f.CreateImage(img, 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(), input_path + "palm.jp2")
    
    # Position the image on the page
    element = f.CreateImage(img, 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((output_path + "addimage.pdf"), SDFDoc.e_linearized);
    doc.Close()
    PDFNet.Terminate()

    print("Done. Result saved in addimage.pdf...")

if __name__ == '__main__':
    main()
```

{% endcode %}
{% endtab %}

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

```ruby
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

require '../../../PDFNetC/Lib/PDFNetRuby'
include PDFNetRuby
require '../../LicenseKey/RUBY/LicenseKey'

$stdout.sync = true

#-----------------------------------------------------------------------------------
# This sample illustrates how to embed various raster image formats
# (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
#
# Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
# be present in the system path.
#-----------------------------------------------------------------------------------

	PDFNet.Initialize(PDFTronLicense.Key)
    
	# Relative path to the folder containing test files.
	input_path = "../../TestFiles/"
	output_path = "../../TestFiles/Output/"

	doc = PDFDoc.new()
	f = ElementBuilder.new()            # Used to build new Element objects
	writer = ElementWriter.new()        # Used to write Elements to the page

	page = doc.PageCreate()         # Start a new page
	writer.Begin(page)              # Begin writing to this page

	# ----------------------------------------------------------
	# Add JPEG image to the output file
	img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
	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(), input_path + "butterfly.png")
	element = f.CreateImage(img, Matrix2D.new(100, 0, 0, 100, 300, 500))
	writer.WritePlacedElement(element)
    
	# ----------------------------------------------------------
	# Add a GIF image to the output file (This section is not supported on Linux)
	img = Image.Create(doc.GetSDFDoc(), input_path + "pdfnet.gif")
	element = f.CreateImage(img, Matrix2D.new(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350))
	writer.WritePlacedElement(element)
    
	# ----------------------------------------------------------
	# Add a TIFF image to the output file
    
	img = Image.Create(doc.GetSDFDoc(), (input_path + "grayscale.tif"))
	element = f.CreateImage(img, Matrix2D.new(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(Rect.new(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.
	hint_set = ObjSet.new();
	enc = hint_set.CreateArray();  # Initilaize encoder 'hint' parameter 
	enc.PushBackName("JBIG2");
	enc.PushBackName("Lossy");

	img = Image.Create(doc.GetSDFDoc(), input_path + "multipage.tif", enc);
	element = f.CreateImage(img, Matrix2D.new(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(), input_path + "palm.jp2")
    
	# Position the image on the page
	element = f.CreateImage(img, Matrix2D.new(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((output_path + "addimage.pdf"), SDFDoc::E_linearized);
	doc.Close()
	PDFNet.Terminate
	puts "Done. Result saved in addimage.pdf..."
```

{% endcode %}
{% endtab %}

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

```vb
'
' Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
'

Imports System

Imports pdftron
Imports pdftron.Common
Imports pdftron.Filters
Imports pdftron.SDF
Imports pdftron.PDF

Module AddImageTestVB
    Dim pdfNetLoader As PDFNetLoader
    Sub New()
        pdfNetLoader = pdftron.PDFNetLoader.Instance()
    End Sub

    '-----------------------------------------------------------------------------------
    ' This sample illustrates how to embed various raster image formats 
    ' (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
    '-----------------------------------------------------------------------------------
    Sub Main()

        PDFNet.Initialize(PDFTronLicense.Key)

        ' Relative path to the folder containing test files.
        Dim input_path As String = "../../../../TestFiles/"
        Dim output_path As String = "../../../../TestFiles/Output/"

        Try


            Using doc As PDFDoc = New PDFDoc
                Using bld As ElementBuilder = New ElementBuilder       ' Used to build new Element objects
                    Using writer As ElementWriter = New ElementWriter      ' Used to write Elements to the page	

                        Dim page As Page = doc.PageCreate()     ' Start a new page 
                        writer.Begin(page)    ' Begin writing to this page

                        ' ----------------------------------------------------------
                        ' Embed a JPEG image to the output document. 

                        Dim img As Image = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")

                        ' You can also directly add any .NET Bitmap. The following commented-out code 
                        ' is equivalent to the above line:
                        Dim bmp As System.Drawing.Bitmap
                        '   bmp = New System.Drawing.Bitmap(input_path + "peppers.jpg")
                        '   Dim img As Image = Image.Create(doc, bmp)

                        Dim element As Element = bld.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(), input_path + "butterfly.png")
                        element = bld.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(), input_path + "pdfnet.gif")
                        element = bld.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(), input_path + "grayscale.tif")
                        element = bld.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 multi-page TIFF to the output file

                        ' Create a new page 
                        page = doc.PageCreate(New Rect(0, 0, 612, 794))
                        writer.Begin(page)    ' Begin writing to the page

                        ' Embed the first TIFF page. Use JBIG2 Encoding

                        ' Use JBIG2 Encoding


                        Dim hint_set As ObjSet = New ObjSet
                        Dim enc As Obj = hint_set.CreateArray()
                        enc.PushBackName("JBIG2")
            enc.PushBackName("Lossy")
            

                        img = Image.Create(doc.GetSDFDoc(), input_path + "multipage.tif", enc)
                        element = bld.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(), input_path + "palm.jp2")

                        ' Position the image on the page.
                        element = bld.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(bld.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman), 32))
                        element = bld.CreateTextRun("JPEG2000 Sample")
                        element.SetTextMatrix(1, 0, 0, 1, 190, 30)
                        writer.WriteElement(element)
                        writer.WriteElement(bld.CreateTextEnd())

                        writer.End()    ' Finish writing to the page
                        doc.PagePushBack(page)



                    End Using
                End Using

                doc.Save(output_path + "addimage.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
                Console.WriteLine("Done. Result saved in addimage.pdf...")
            End Using

        Catch e As PDFNetException
            Console.WriteLine(e.Message)
        End Try
        PDFNet.Terminate()
    End Sub

End Module
```

{% 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/core/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.
