# Syncfusion EJ2 PdfViewer

This sample demonstrate the following key features of PDF Viewer:

* View the PDF document
* Core interactions - Scrolling, Zooming, panning and page navigation
* Built-in toolbar
* Select and copy text from PDF file
* Search a text easily across the PDF document
* Easy navigation with the help of Bookmarks, thumbnails, hyperlinks and table of contents
* View modes - fit to page and fit to width
* Print the entire document or a specific page directly from the browser.

More information on the PDF Viewer instantiation can be found in this [documentation section](https://ej2.syncfusion.com/documentation/pdfviewer/getting-started).

## Source Code

You can download the source code for this project here.

{% embed url="<https://github.com/iceteagroup/Wisej-DemoBrowser/tree/main/Wisej.DemoBrowser.Premium/Wisej.Web.Ext.Syncfusion2.Demo>" %}

## Design-Time

The **EJ2 PdfViewer** control can be added to a Form or Page from the Visual Studio Toolbox. It should look like this when dropped onto the designer.

![EJ2 PDF Viewer at Design-Time](/files/pqc58ECHaJHi6rz2JIu6)

{% hint style="info" %}
The design-time appearance of the **EJ2 PdfViewer** control will not show a PDF document. The logic for this should be implemented in your code (see below).
{% endhint %}

### Sample Implementation

{% code title="PdfViewer.cs" %}

```csharp
/// <summary>
/// PDF Viewer Server Side Configuration Example.
/// https://github.com/SyncfusionExamples/PdfViewer-Server/blob/master/src/ej2-pdfviewer-server/Controllers/PdfViewerController.cs.
/// </summary>
public partial class PDFViewer : TestBase
{
public PDFViewer()
{
	InitializeComponent();
}

private void pdfViewer1_Appear(object sender, EventArgs e)
{
	this.pdfViewer1.Instance.load("Wisej.pdf", null);
}

private void pdfViewer1_WebRequest(object sender, WebRequestEventArgs e)
{
	// decode payload.
	dynamic payload = null;
	using (StreamReader reader = new StreamReader(e.Request.InputStream))
	{
		var text = reader.ReadToEnd();
		payload = JsonConvert.DeserializeObject<Dictionary<string, string>>(text);
	}

	switch(payload["action"])
	{
		case "Load":
			e.Response.Write(LoadPdf(payload));
			break;

		case "RenderPdfPages":
			e.Response.Write(RenderPdfPages(payload));
			break;

		case "RenderPdfTexts":
			e.Response.Write(RenderPdfTexts(payload));
			break;

		case "RenderThumbnailImages":
			e.Response.Write(RenderThumbnailImages(payload));
			break;

		case "Download":
			e.Response.Write(Download(payload));
			break;

		case "PrintImages":
			e.Response.Write(PrintImages(payload));
			break;

		case "RenderAnnotationComments":
			e.Response.Write(RenderAnnotationComments(payload));
			break;

		case "Unload":
			e.Response.Write(PrintImages(payload));
			break;

		default:
			break;

	}
}

private string RenderPdfTexts(dynamic payload)
{
;			var renderer = new PdfRenderer();
	var result = renderer.GetDocumentText(payload);
	return JsonConvert.SerializeObject(result);
}

private string LoadPdf(Dictionary<string, string> payload)
{
	MemoryStream ms;
	PdfRenderer renderer = new PdfRenderer();
	bool isFileName = bool.Parse(payload["isFileName"]);
	if (isFileName)
	{
		var path = Application.MapPath($"Data/PdfViewer/{payload["document"]}");
		var bytes = File.ReadAllBytes(path);
		ms = new MemoryStream(bytes);
	}
	else
	{
		var bytes = Convert.FromBase64String(payload["document"]);
		ms = new MemoryStream(bytes);
	}

	var result = renderer.Load(ms, payload);
	return JsonConvert.SerializeObject(result, Formatting.None);
}

private string RenderPdfPages(Dictionary<string, string> payload)
{
	var renderer = new PdfRenderer();
	var page = renderer.GetPage(payload);
	return JsonConvert.SerializeObject(page);
}

private string RenderThumbnailImages(Dictionary<string, string> payload)
{
	var renderer = new PdfRenderer();
	var result = renderer.GetThumbnailImages(payload);
	return JsonConvert.SerializeObject(result);
}

private string Download(Dictionary<string, string> payload)
{
	var renderer = new PdfRenderer();
	return renderer.GetDocumentAsBase64(payload);
}

private string PrintImages(Dictionary<string, string> payload)
{
	var renderer = new PdfRenderer();
	var result = renderer.GetPrintImage(payload);
	return JsonConvert.SerializeObject(result);
}

private string Unload(Dictionary<string, string> payload)
{
	var renderer = new PdfRenderer();
	renderer.ClearCache(payload);
	return "Cleared Cache.";
}

private string RenderAnnotationComments(Dictionary<string, string> payload)
{
	var renderer = new PdfRenderer();
	var result = renderer.GetAnnotationComments(payload);
	return JsonConvert.SerializeObject(result);
}

private void button1_Click(object sender, EventArgs e)
{
	this.pdfViewer1.Instance.load("Annotations.pdf", null);
}
```

{% endcode %}

## Runtime

The resulting **EJ2 PdfViewer** control will look something like this:

![EJ2 PDF Viewer at Runtime](/files/aBf5QYFR7RmcAhWpXt3x)


---

# Agent Instructions: 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:

```
GET https://docs.wisej.com/examples/premium/syncfusion-ej2-pdfviewer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
