> 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/uwp/get-started/samples/pdfdocmemorytest.md).

# PDFDocMemory

Sample C# code for using Apryse SDK to read/write a PDF document from/to memory buffer. This is useful for applications that work with dynamic PDFdocuments that don't need to be saved/read from a disk

Sample C# code for using Apryse SDK to read/write a PDF document from/to memory buffer. This is useful for applications that work with dynamic PDFdocuments that don't need to be saved/read from a disk. Learn more about our [UWP SDK](/core/get-started/frameworks/dotnet.md).

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

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

using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Foundation;

using pdftron.PDF;
using pdftron.SDF;

using PDFNetUniversalSamples.ViewModels;

namespace PDFNetSamples
{
    public sealed class PDFDocMemoryTest : Sample
    {
        public PDFDocMemoryTest() :
            base("PDFDocMemory", "The sample illustrates how to read/write a PDF document from/to memory buffer. This is useful for applications that work with dynamic PDFdocuments that don't need to be saved/read from a disk.")
        {
        }

        public override IAsyncAction RunAsync()
        {
            return Task.Run(new System.Action(async () => {
                WriteLine("--------------------------------");
                WriteLine("Starting PDFDocMemory Test...");
                WriteLine("--------------------------------\n");
			    try  
			    {
				    // Read a PDF document from a IRandomAccessStream or pass-in a memory buffer...
                    PDFDoc doc = new PDFDoc(Path.Combine(InputPath, "tiger.pdf"));
				    doc.InitSecurityHandler();

				    int num_pages = doc.GetPageCount();

				    ElementWriter writer = new ElementWriter();
				    ElementReader reader = new ElementReader();
				    Element element;

				    // Perform some document editing ...
				    // Here we simply copy all elements from one page to another.
				    for(int i = 1; i <= num_pages; ++i)
				    {
                        pdftron.PDF.Page pg = doc.GetPage(2 * i - 1);

					    reader.Begin(pg);
                        pdftron.PDF.Page new_page = doc.PageCreate(pg.GetMediaBox());
					    doc.PageInsert(doc.GetPageIterator(2*i), new_page);

					    writer.Begin(new_page);
					    while ((element = reader.Next()) != null) 	// Read page contents
					    {
						    writer.WriteElement(element);
					    }

                        writer.End();
					    reader.End();
				    }

                    string output_file_path = Path.Combine(OutputPath, "doc_memory_edit.pdf");
                    await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_remove_unused);
                    WriteLine("Done. Results saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    // Read some data from the file stored in memory
				    reader.Begin(doc.GetPage(1));
				    while ((element = reader.Next()) != null) {
					    if (element.GetType() == ElementType.e_path)
						    WriteLine("Path, ");
				    }
				    reader.End();
				    doc.Destroy();
			    }
			    catch (Exception e) 
                {
                    WriteLine(GetExceptionMessage(e));
                }

                WriteLine("\n--------------------------------");
                WriteLine("Done PDFDocMemory Test.");
                WriteLine("--------------------------------\n");
            })).AsAsyncAction();
		}
	}
}
```

{% 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/uwp/get-started/samples/pdfdocmemorytest.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.
