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

# ElementEdit

Here is a complete PDF and DOCX editing guide using C#. Modify properties, edit text, bookmarks,annotation and lot more using COS API

Sample C# code for using Apryse SDK to programmatically edit an existing PDF document's page display list and the graphics state attributes on existing elements. In particular, this sample strips all images from the page and changes the text color to blue. You can also build a GUI with [interactive PDF editor widgets](/xamarin/get-started/samples.md#pdfview). Some of Apryse SDK's other functions for programmatically editing PDFs include the [Cos/SDF low-level API](/xamarin/get-started/samples.md#sdf), [page manipulation](/xamarin/get-started/samples.md#pdfpage), and more. 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 System.Collections.Generic;

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

using XSet = System.Collections.Generic.List<int>;

using NUnit.Framework;

namespace MiscellaneousSamples
{
	/// <summary>
	/// The sample code shows how to edit the page display list and how to modify graphics state 
	/// attributes on existing Elements. In particular the sample program strips all images from 
	/// the page, changes path fill color to red, and changes text fill color to blue. 
	/// </summary>
	[TestFixture]
	public class ElementEditTest
	{
		
		static void ProcessElements(ElementReader reader, ElementWriter writer, XSet visited)
		{
			Element element;
			while ((element = reader.Next()) != null) // Read page contents
			{
				switch (element.GetType())
				{
					case Element.Type.e_image:
					case Element.Type.e_inline_image:
							// remove all images by skipping them
							break;
					case Element.Type.e_path:
						{
							// Set all paths to red color.
							GState gs = element.GetGState();
							gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
							gs.SetFillColor(new ColorPt(1, 0, 0));
                            writer.WriteElement(element);
							break;
						}
					case Element.Type.e_text:
						{
							// Set all text to blue color.
							GState gs = element.GetGState();
							gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
							gs.SetFillColor(new ColorPt(0, 0, 1));
                            writer.WriteElement(element);
							break;
						}
					case Element.Type.e_form:
						{
							writer.WriteElement(element); // write Form XObject reference to current stream

                            Obj form_obj = element.GetXObject();
							if (!visited.Contains(form_obj.GetObjNum())) // if this XObject has not been processed
							{
								// recursively process the Form XObject
								visited.Add(form_obj.GetObjNum());
								ElementWriter new_writer = new ElementWriter();

								reader.FormBegin();
								new_writer.Begin(form_obj, true);

								reader.ClearChangeList();
								new_writer.SetDefaultGState(reader);

                                ProcessElements(reader, new_writer, visited);
								new_writer.End();
								reader.End();
							}
							break;
						}
                    default:
                        writer.WriteElement(element);
                        break;
				}
			}
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[Test]
		public static void Sample()
		{

			// Relative path to the folder containing test files.
			const string input_path =  "TestFiles/";
			string input_filename = "newsletter.pdf";
			string output_filename = "newsletter_edited.pdf";

			try
			{
				Console.WriteLine("Opening the input file...");
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename)))
				{
					doc.InitSecurityHandler();

					ElementWriter writer = new ElementWriter();
					ElementReader reader = new ElementReader();
					XSet visited = new XSet();

					PageIterator itr = doc.GetPageIterator();

					while (itr.HasNext())
					{
						try
						{
							Page page = itr.Current();
							visited.Add(page.GetSDFObj().GetObjNum());

							reader.Begin(page);
							writer.Begin(page, ElementWriter.WriteMode.e_replacement, false, true, page.GetResourceDict());

                            ProcessElements(reader, writer, visited);
							writer.End();
							reader.End();

							itr.Next();
						}
						catch (PDFNetException e)
						{
							Console.WriteLine(e.Message);
							Assert.True(false);
						}
					}

					doc.Save(Utils.CreateExternalFile(output_filename), SDFDoc.SaveOptions.e_remove_unused);
					Console.WriteLine("Done. Result saved in {0}...", output_filename);
				}
			}
			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/elementedittest.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.
