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

# AddImage

Sample C# 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 C# 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 [Xamarin SDK](/core/get-started/frameworks/dotnet.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

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

```csharp
//
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
//

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

using NUnit.Framework;

namespace MiscellaneousSamples
{
	[TestFixture]
	public class AddImageTest
	{

		/// <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>
		[Test]
		public static void Sample()
		{

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

			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, Utils.GetAssetTempFile(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(Utils.GetAssetTempFile(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, Utils.GetAssetTempFile(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, Utils.GetAssetTempFile(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, Utils.GetAssetTempFile(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(Utils.GetAssetTempFile(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, Utils.GetAssetTempFile(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, Utils.GetAssetTempFile(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(Utils.CreateExternalFile("addimage.pdf"), SDFDoc.SaveOptions.e_linearized);
					Console.WriteLine("Done. Result saved in addimage.pdf...");
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}

		}
	}
}
```

{% 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
