Demos
Thursday, 31 May 2012 09:07

PDFViewer Quick Start for WPF

Written by support iceblue

The sample demonstrates how to get started with Spire.PDFViewer for WPF.

 

Monday, 05 March 2012 07:29

Export PDF Document to images

Written by Administrator

The sample demonstrates how to export PDF pages as images by PdfDocumentViewer Component.

Download PDFViewer.pdf

Monday, 14 April 2014 03:09

.NET - How to add Controls to Toolbox

Written by support iceblue

There are three ways to add Spire.PDFViewer controls to Toolbox for Windows Forms application.

In this document, these ways are introduced.

The First Solution

Set the CheckBox below to the CheckState "Checked" during installing Spire.PDFViewer.

How to add Controls to Toolbox

After installment is completed, controls will be added to the Toolbox.

The Second Solution

If you have already installed Spire.PDFViewer, you can also add controls this way:

"Start" -"Programs" -"e-iceblue" -"Spire.PDFViewer" : Click "Add Controls into VS Toolbox".

How to add Controls to Toolbox

Click "Add" to add controls.

How to add Controls to Toolbox

The Third Solution

Right-click on the blank part of the Toolbox - "Add Tab" - name the new Tab "Spire Controls" :

How to add Controls to Toolbox

Right-click on the blank part below "Spire Controls" - "Choose Items" - ".NET Framework Components" - "Browse" to the "Bin" folder – find the file "Spire.PdfViewer.Forms.dll"- "Open".

How to add Controls to Toolbox

Click “OK”. Then you have added controls to Toolbox successfully.

How to add Controls to Toolbox

Introduction

Spire.PDFViewer.Web can be specially designed for developers to load one or more PDF, view PDF in full window or in iframe, which doesn’t need to install PDF Reader on the machine. You just need to use Spire.Pdf.dll file and install IE browser which supports Microsoft .NET Framework 3.0 on your PC.
This article discusses how can create Spire.PdfViewer.Web.

How To Create Spire.PDFViewer.Web

You only need to create three classes below to view PDF without installing PDF Reader in browser:
  • PdfViewer
  • PdfViewerHandler
  • PDFViewerControlDesigner

Create PdfViewer Class

First create an asp.net server control called Spire.PdfViewer.Web, and then add PdfViewer class to this server control. This class is used to design PdfViewer control and view your PDF content, which inherits WebControl class.
Then add a String property called File to PdfViewer calss. You can use File to call PdfViewer to load your PDF file.
The code as follows:
[C#]
[DefaultProperty("File")]
[ToolboxData("<{0}:PDFViewer runat=server></{0}:PDFViewer>")]
public class PdfViewer : WebControl
{
	[Bindable(true)]
	[Category("Appearance")]
	[DefaultValue("")]
	[Localizable(true)]
	[EditorBrowsable(EditorBrowsableState.Advanced)]
	[Editor(typeof(PdfUrlEditor), typeof(UITypeEditor))]
	[Description("The pdf file which will be displayed in the browser.")]
	public string File
	{
		get
		{
			String s = (String)ViewState["File"];
			return ((s == null) ? String.Empty : s);
		}
		set
		{
			ViewState["File"] = value;  
		}
	}
}
And add a Boolean property called FullWindow for PdfViewer calss. By using FullWindow, you can view PDF file in different mode. If set it to true, PDF file will be viewed in full window. Otherwise, it will be viewed in in iframe.
[C#]
[Bindable(true)]
[Category("Appearance")]
[DefaultValue(false)]
[Localizable(true)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[Description("Indicates whether display the PDF file in full window instead of in iframe element.")]
public bool FullWindow
{
	get
	{
		object o = ViewState["FullWindow"];
		return o != null && (bool)o == true;
	}
	set
	{
		ViewState["FullWindow"] = value;
	}
}
In browser, source tags in .aspx file will be converted to HTML tags. So you need to override System.Web.UI.HtmlTextWriterTag to specify HtmlTextWriterTag value for PdfViewer control according to the value of FullWindow and DesignMode.
[C#]
protected override HtmlTextWriterTag TagKey
{
	get
	{
		if (!this.DesignMode && this.FullWindow)
		{
			return HtmlTextWriterTag.Script;
		}
		else
		{
			return HtmlTextWriterTag.Iframe;
		}
	}
}
Now, you have designed PdfViewer control. Next, you need to add some operations to make PdfViewer control can display your PDF content.
Firstly, add GetViewerHandler() method for PdfViewr class, which is used to get file path. And you also need to add System.Configuration.dll to your server control.
In that method, there is a PdfViewerHandler class is used. The PdfViewerHandler class will be introduced in Create PdfViewerHandler Class later.
[C#]
private String GetViewerHandler()
{
	String path = PdfViewerHandler.Name;
	HttpHandlersSection handlersSection
		= this.Context.GetSection("system.web/httpHandlers") as HttpHandlersSection;
	if (handlersSection != null)
	{
		foreach (HttpHandlerAction h in handlersSection.Handlers)
		{
			if (h.Type.StartsWith(PdfViewerHandler.Name))
			{
				path = h.Path;
				break;
			}
		}
	}
	return path;
}
Then add GetUrl() method for PdfViewer class to convert file path to a url.
[C#]
private String GetUrl()
{
	String path = this.GetViewerHandler();
	return String.Format("{0}?File={1}", path, HttpUtility.UrlEncode(this.File));
}
Lastly, add AddAttributesToRender (HtmlTextWriter writer) method to assign url value to Src attribute of iframe. Add RenderContents(HtmlTextWriter writer) method to write render content from url into Script.
[C#]
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
	base.AddAttributesToRender(writer);
	if (!this.DesignMode && !this.FullWindow)
	{
		String path = this.GetUrl();
		writer.AddAttribute(HtmlTextWriterAttribute.Src, path);
	}
}

protected override void RenderContents(HtmlTextWriter writer)
{
	base.RenderContents(writer);
	if (!this.DesignMode && this.FullWindow)
	{
		writer.WriteLine("window.location.href = \"" + this.GetUrl() + "\";");
	}
}
You could add a PDF Filter to search all your PDF files quickly under your project.
To create a class called PdfUrlEditor, which needs to inherit UrlEditor class. So you should add Systeam.Design.dll into your server control.
[C#]
public class PdfUrlEditor : UrlEditor
{
	protected override string Filter
	{
		get
		{
			return "PDF File|*.pdf";
		}
	}
}

Create PdfViewerHandler Class

You may have seen the use of PdfViewerHandler in Create PdfViewer class.
It seems that PdfViewerHandler class is very important here. Before adding the class, let us first know what PdfViewerHandler class is used to do.
In fact, Spire.PdfViewer.Web’ s working principle is that firstly converting PDF file to XPS file, and then view XPS data via XPS Viewer in IE browser. The XPS Viewer is included with .NET Framework 3.0. That is why you should ensure that install IE browser which supports Microsoft .NET Framework 3.0 on your PC.
To do the task, you need a HTTP handler. It’s PdfViewerHandler class.
Add a new class file called PdfViewerHandler to your server control. Let the class implement IHttpHandler interface and IRequiresSessionState interface.
Firstly, add two internal String fields for the class: Name is name of PdfViewerHandler, and FullName is full name of PdfViewerHandler.
[C#]
public class PdfViewerHandler : IHttpHandler, IRequiresSessionState
{
	internal static String Name = "PdfViewerHandler";
	internal static String FullName = "PdfViewerHandler";
	static PdfViewerHandler()
	{
		Type type = typeof(PdfViewerHandler);
		Name = type.FullName;
		FullName = String.Format("{0}, {1}", Name, type.Assembly.FullName);
	}
}
You could create a Boolean property called IsReusable to make PdfViewerHandler object can be reused.
[C#]
public bool IsReusable
{
	get { return true; }
}
Next, you need to add ProcessRequest (HttpContext context) method for PdfViewerHandler class to do the conversion task.
[C#]
public void ProcessRequest(HttpContext context)
{
	String file = context.Request.Params.Get("File");
	if (String.IsNullOrEmpty(file))
	{
		return;
	}
	try
	{
		file = context.Request.MapPath(file);
	}
	catch (Exception e)
	{
	}

	if (!File.Exists(file))
	{
		return;
	}
	context.Response.ContentType = "application/vnd.ms-xpsdocument";
	using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
	{
		using (PdfDocument doc = new PdfDocument(stream))
		{
			String tempFile = Path.GetTempFileName();
			doc.SaveToFile(tempFile, FileFormat.XPS);
			byte[] buffer = File.ReadAllBytes(tempFile);
			File.Delete(tempFile);
			context.Response.OutputStream.Write(buffer, 0, buffer.Length);
		}
	}
}

Create PDFViewerControlDesigner Class

You have created a HTTP handler class called PdfViewerHandler. Next, you must register it in the Web.config file. If you don’t want to add the configuration code below to Web.config file manually, you need to create PDFViewerControlDesigner class to help you do it.
<httpHandlers>
<add path="PdfViewer" verb="*" type="Spire.PdfViewer.Web.PdfViewerHandler, Spire.PdfViewer.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" validate="true" />
</httpHandlers>
This class also is written into PdfViewer.cs, which needs to inherit ControlDesigner class. You need only to create one member for it: a method called Initialize(IComponent component).
[C#]
public class PDFViewerControlDesigner : ControlDesigner
{
	public override void Initialize(IComponent component)
	{
		IWebApplication webApp = (IWebApplication)component.Site.GetService(typeof(IWebApplication));
		Configuration config = webApp.OpenWebConfiguration(false);
		SystemWebSectionGroup systemWeb = config.GetSectionGroup("system.web") as SystemWebSectionGroup;

		bool hasConfig = false;
		foreach (HttpHandlerAction h in systemWeb.HttpHandlers.Handlers)
		{
			if (h.Type.StartsWith(PdfViewerHandler.Name))
			{
				hasConfig = true;
				break;
			}
		}
		if (!hasConfig)
		{
			HttpHandlerAction handler
				= new HttpHandlerAction(PdfViewerHandler.Name,
					PdfViewerHandler.FullName, "*");
			systemWeb.HttpHandlers.Handlers.Add(handler);
			config.Save(ConfigurationSaveMode.Modified);
		}
		base.Initialize(component);
	}
}
After finish the class, you need to specify the class used to implement design-time services for PdfViewer.
[C#]
[DesignerAttribute(typeof(PDFViewerControlDesigner))]
[DefaultProperty("File")]
[ToolboxData("<{0}:PDFViewer runat=server></{0}:PDFViewer>")]
public class PdfViewer : WebControl
{
	//......
}
And add the code below in AssemblyInfo.cs.
[C#]
[assembly: TagPrefix("Spire.PdfViewer.Web", "spire")]

How to use Spire.PdfViewer.Web

So far, you have created these three classes, and your server control called Spire.PdfViewer.Web has been finished. Now, you can try to view a PDF file.
Here are the steps:
The first step is to create an asp.net web application called WebPDFViewer under the same solution as Spire.PdfViewer.Web.
Then add Spire.PdfViewer.Web as a project reference to your WebPDFViewer application.

Here you can see the PdfViewer control in Toolbox.

Next, drag a pdfViewer control out of the toolbox and drop it on the Design of Default. aspx. You can check the File, FullWindow properties are contained in PdfViewer control. Select a PDF file and give File property a url.

In the first example, I will set FullWindow to true.
You will look at the result in IE browser after run your web application.

In the second example, I will set FullWindow to false. Here is the result.

To check the html code of these two result files separately. You will see two different tags.
The first one shows that PdfViewer control is converted to Script in html if set FullWindow to true.

The second one shows that PdfViewer control is converted to Iframe in html if set FullWindow to false.

Monday, 14 April 2014 03:23

WPF - How to add Controls to Toolbox

Written by support iceblue

In this document, I will introduce you how to add Spire.PDFViewer controls to Toolbox for WPF application.

How To

Right-click on the blank part of the Toolbox - "Add Tab" - name the new Tab "Spire WPF Controls":

How to add Controls to Toolbox

Right-click on the blank part below "Spire WPF Controls" - "Choose Items" - "WPF Components" - "Browse" to the "Bin" folder - find the file "Spire.PdfViewer.Wpf.dll" - "Open".

How to add Controls to Toolbox

Click "OK". Then you have added controls to Toolbox successfully.

How to add Controls to Toolbox