> 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/stampertest.md).

# Stamper

Sample C# code for using Apryse SDK to programmatically stamp PDF pages with text, images, or with other PDF pages.

Sample C# code for using Apryse SDK to programmatically stamp PDF pages with text, images, or with other PDF pages. ElementBuilder and ElementWriter should be used for more complex PDF stamping operations. Learn more about our [Xamarin SDK](/core/get-started/frameworks/dotnet.md).

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

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

using System;
using System.IO;

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

using NUnit.Framework;

namespace MiscellaneousSamples
{
	class StamperSample
	{
		static StamperSample() {}
		
		/// <summary>
		// The following sample shows how to add new content (or watermark) PDF pages
		// using 'pdftron.PDF.Stamper' utility class. 
		//
		// Stamper can be used to PDF pages with text, images, or with other PDF content 
		// in only a few lines of code. Although Stamper is very simple to use compared 
		// to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you 
		// need full control over PDF creation use ElementBuilder/ElementWriter to add 
		// new content to existing PDF pages as shown in the ElementBuilder sample project.
		/// </summary>
		[Test]
		public static void Sample()
		{

			const string input_path =  "TestFiles/";
			string input_filename = "newsletter";

			//--------------------------------------------------------------------------------
			// Example 1) Add text stamp to all pages, then remove text stamp from odd pages. 
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
				using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 0.5, 0.5))
				{
					doc.InitSecurityHandler();

					s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_center);
					s.SetFontColor(new ColorPt(1, 0, 0)); // set text color to red                
					s.StampText(doc, "If you are reading this\nthis is an even page", new PageSet(1, doc.GetPageCount()));
					//delete all text stamps in odd pages
					Stamper.DeleteStamps(doc, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_odd));

					doc.Save(Utils.CreateExternalFile(input_filename + ".ex1.pdf"), SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}

			//--------------------------------------------------------------------------------
			// Example 2) Add Image stamp to first 2 pages. 
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
				using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .05, .05))
				{
					doc.InitSecurityHandler();

					Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
					s.SetSize(Stamper.SizeType.e_relative_scale, 0.5, 0.5);
					//set position of the image to the center, left of PDF pages
					s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_center);
					s.SetFontColor(new ColorPt(0, 0, 0, 0));
					s.SetRotation(180);
					s.SetAsBackground(false);
					//only stamp first 2 pages
					s.StampImage(doc, img, new PageSet(1, 2));

					doc.Save(Utils.CreateExternalFile(input_filename + ".ex2.pdf"), SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}

			//--------------------------------------------------------------------------------
			// Example 3) Add Page stamp to all pages. 
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
				using (PDFDoc fish_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "fish.pdf")))
				using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .5, .5))
				{
					doc.InitSecurityHandler();

					fish_doc.InitSecurityHandler();

					Page src_page = fish_doc.GetPage(1);
					Rect page_one_crop = src_page.GetCropBox();
					// set size of the image to 10% of the original while keep the old aspect ratio
					s.SetSize(Stamper.SizeType.e_absolute_size, page_one_crop.Width() * 0.1, -1);
					s.SetOpacity(0.4);
					s.SetRotation(-67);
					//put the image at the bottom right hand corner
					s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom);
					s.StampPage(doc, src_page, new PageSet(1, doc.GetPageCount()));

					doc.Save(Utils.CreateExternalFile(input_filename + ".ex3.pdf"), SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}

			//--------------------------------------------------------------------------------
			// Example 4) Add Image stamp to first 20 odd pages. 
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
				using (Stamper s = new Stamper(Stamper.SizeType.e_absolute_size, 20, 20))
				{
					doc.InitSecurityHandler();

					s.SetOpacity(1);
					s.SetRotation(45);
					s.SetAsBackground(true);
					s.SetPosition(30, 40);
					Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
					s.StampImage(doc, img, new PageSet(1, 20, PageSet.Filter.e_odd));

					doc.Save(Utils.CreateExternalFile(input_filename + ".ex4.pdf"), SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}

			//--------------------------------------------------------------------------------
			// Example 5) Add text stamp to first 20 even pages
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
				using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .05, .05))
				{
					doc.InitSecurityHandler();

					s.SetPosition(0, 0);
					s.SetOpacity(0.7);
					s.SetRotation(90);
					s.SetSize(Stamper.SizeType.e_font_size, 80, -1);
					s.SetTextAlignment(Stamper.TextAlignment.e_align_center);
					s.StampText(doc, "Goodbye\nMoon", new PageSet(1, 20, PageSet.Filter.e_even));

					doc.Save(Utils.CreateExternalFile(input_filename + ".ex5.pdf"), SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}

			//--------------------------------------------------------------------------------
			// Example 6) Add first page as stamp to all even pages
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
				using (PDFDoc fish_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "fish.pdf")))
				using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .3, .3))
				{
					doc.InitSecurityHandler();

					fish_doc.InitSecurityHandler();

					s.SetOpacity(1);
					s.SetRotation(270);
					s.SetAsBackground(true);
					s.SetPosition(0.5, 0.5, true);
					s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_bottom);
					Page page_one = fish_doc.GetPage(1);
					s.StampPage(doc, page_one, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_even));

					doc.Save(Utils.CreateExternalFile(input_filename + ".ex6.pdf"), SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}

			//--------------------------------------------------------------------------------
			// Example 7) Add image stamp at top right corner in every pages
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
				using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .1, .1))
				{
					doc.InitSecurityHandler();

					s.SetOpacity(0.8);
					s.SetRotation(135);
					s.SetAsBackground(false);
					s.ShowsOnPrint(false);
					s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_top);
					s.SetPosition(10, 10);

					Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
					s.StampImage(doc, img, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_all));

					doc.Save(Utils.CreateExternalFile(input_filename + ".ex7.pdf"), SDFDoc.SaveOptions.e_linearized);
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}

			//--------------------------------------------------------------------------------
			// Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
			//          Because text stamp is set as background, the image is top of the text
			//          stamp. Text stamp on the first page is not visible.
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
				using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 0.07, -0.1))
				{
					doc.InitSecurityHandler();

					s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom);
					s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_top);
					s.SetFont(Font.Create(doc, Font.StandardType1Font.e_courier, true));
					s.SetFontColor(new ColorPt(1, 0, 0, 0)); //set color to red
					s.SetTextAlignment(Stamper.TextAlignment.e_align_right);
					s.SetAsBackground(true); //set text stamp as background
					s.StampText(doc, "This is a title!", new PageSet(1, 2));

					Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
					s.SetAsBackground(false); // set image stamp as foreground
					s.StampImage(doc, img, new PageSet(1));

					doc.Save(Utils.CreateExternalFile(input_filename + ".ex8.pdf"), SDFDoc.SaveOptions.e_linearized);
				}
			}
			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 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/xamarin/get-started/samples/stampertest.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.
