|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article describes an approach to embedding and displaying PDF documents in a web page through the use of a simple ASP.NET 2.0 custom server control. The approach indicated herein allows the developer the opportunity to control the web page content surrounding the embedded PDF; this is in contrast to linking directly to a PDF which uses the entire web page to display PDF but does not otherwise permit the developer to control the appearance of the page.
Figure 1. Embedding and Displaying PDFs
Figure 2. Linking Directly to a PDF
Getting StartedThere are two solutions included with this download, one is web custom control library containing a single custom control used to render out the PDF, the other is a test web site used to display a PDF through the use of the control. Figure 3 (below) shows the solution explorer for the project. The project appearing at the top of the solution is the test web site, it contains only a single web page (default) and it includes a PDF file included for testing purposes. The bottom project is the web custom control library with the single control included ( System.Design
Figure 3. Solution Explorer with Both Projects Visible
The Web Custom Control ProjectCode: ShowPdf.csWithin the web custom control project, there is a single custom control provided in this example. The example is entitled, ShowPdf.cs. The code for the project is very simple and should take very little time to implement. The control code starts out with the default imports: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PdfViewer
{
[DefaultProperty("FilePath")]
[ToolboxData("<{0}:ShowPdf
runat="server"></{0}:ShowPdf>")]
public class ShowPdf : WebControl
{
Following the imports is the namespace and class declaration. The class contains a single property called After the class declaration, a declarations region was added and a single local member variable was defined and included within that region. The local member variable is used to retain the path to the PDF document loaded into the control. #region
"Declarations"
privatestring mFilePath;
#endregion
The next bit of code in the class is contained in a new region called #region "Properties"
[Category("Source File")]
Notice that in the set side of the property, the code is written to remove the tilde from in front of the file path if the tilde is present. If the tilde is left intact after setting the property to point to a file using the URL Editor, the tilde would otherwise be included in the HTML rendered to the page and the file would not found. It is necessary to strip this character from the file path in order to use the URL Editor to set this property at design time. The last bit of code needed to finish the control is contained in a region called #region "Rendering"
protected override void RenderContents(HtmlTextWriter
writer)
{
try
{
StringBuilder sb = newStringBuilder();
sb.Append("<iframe src="
+FilePath.ToString() + " ");
sb.Append("width=" +
Width.ToString() + " height=" +
Height.ToString() + " ");
sb.Append("<View PDF: <a
href=" + FilePath.ToString() + "</a></p>
");
sb.Append("</iframe>");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write(sb.ToString());
writer.RenderEndTag();
}
catch
{
// with no properties set, this will render
// "Display PDF Control" in
// a box on the page
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write("Display PDF Control");
writer.RenderEndTag();
} // end try-catch
} //
end RenderContents
#endregion
The Test Web ProjectCode: Default PageThe default page included in the web project is provided to serve as a test bed for the control. The page contains only a panel used as a banner, a hyperlink pointing directly to a PDF file, and the custom control with its file path property also pointing to the PDF. The PDF added to the web site content is also included in the web project. When this site is viewed, the control will display the PDF document in the defined area, selection of the hyperlink will open the same PDF into a separate window; I just included the hyperlink for comparison purposes. SummaryThis article demonstrates an approach that may be used to develop a custom control through which PDFs may be embedded into a web page. The purpose of the control is to allow the PDF to be included within a web page as opposed to the alternative of opening the PDF into a separate page where the PDF consumes the entire available display area and where the user cannot control the appearance of that page. Naturally, the code included in the custom control could be added directly into any page and the same effect could be achieved, however, by adding the code once into a custom control, the developer need only drop the control into the form and set the file path and dimensions to display PDFs without repeating the manual addition of the code each time it is needed.
|
||||||||||||||||||||||