ASP.NET Example

[Top]  [Chapter]  [Previous]  [Next]

File Webform1.aspx.cs



The "using" sequence


using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;


link assembly "wPDF" (from assembly wPDF3.DLL - please add to references)


using wPDF;

using WPDynamic;


namespace RTF2



The event is executed by ASP


public class WebForm1 : System.Web.UI.Page


         private void Page_Load(object sender, System.EventArgs e)




We create a RTF2PDF instance


               RTF2PDF pdf = new RTF2PDF();

              string password = "SomePrivatePassword"



                // Load the license from file which is not in the www path

                // Please make sure the IIS_IUSRS have read access to this file, otherwise the following code will fail.

                 pdf.SetLicense("@FILE@" + password,"C:\\License\\rtflicense.dat",0);


clear all properties and set the default font





create the header text







                 pdf.Memo.CurrObj.ScaleSize(0,567*2,0); // Scale to height = 2 cm

                 pdf.Memo.CurrPar.Alignment = 2; // right

                 pdf.Memo.CurrPar.SpaceAfter = 30; // Distance between image and line

                 pdf.Memo.TextCursor.InputString("\r",0); // one line

                 pdf.Memo.CurrPar.Borders = 2; // Top Border


                        (int)WPDynamic.WPAT.BorderWidth, 30 ); // 1pt border width


create the footer text





                 // Current Date


                        "DATE","\\@ \"dd. mmmm yyyy\"","");

                 pdf.Memo.TextCursor.InputString(", ",0);


                 // right tab in margin, dot filling







                 pdf.Memo.CurrPar.Borders = 2; // Top Border


create some text, in this case just a table.



                 // Fill the body




                          "\rThis Table was created in ASP.NET ",0);

                 pdf.Memo.CurrAttr.IncludeStyles(1); // <<bold




                 pdf.Memo.CurrAttr.ExcludeStyles(1); // >>bold


                 pdf.Memo.TextCursor.AddTable("",3,4,true,0,false, false);



Add text after the table.



                 // Create a new paragraph AFTER the table (mode=2)





                        "Click to see the C# source: ",0);


Create an icon. When user clicks on it the C# source is opened!






                 pdf.Memo.CurrObj.ScaleSize(567,0,0); // Scale to width = 1 cm




Add some paragraphs

store current attributes




                 int save_ca = pdf.Memo.CurrAttr.CharAttrIndex;


add some text and modify the writing mode



                      "This is the RTF data for the table:\r",0);


                 pdf.Memo.CurrAttr.SetFontface("Courier New");


Now save the current text as RTF and insert it here as ANSI text.




                    pdf.Memo.SaveToString(false, "RTF-nobinary"),

                       true, "ANSI"



Restore current writing mode


                 // We restore the character attributes used before

                 pdf.Memo.CurrAttr.CharAttrIndex = save_ca;


and add an icon to open this document in RTF format.asp_6


                 // And create an icon to attach the RTF source

                 pdf.Memo.TextCursor.InputString("\r\rClick to open the document: ",0);



                 pdf.Memo.CurrObj.ScaleSize(567,0,0); // Scale to width = 1 cm


We create a stream, 

save the document to this stream and load it into the "RTF" icon.


                 System.IO.Stream str = new System.IO.MemoryStream();


                      new WPDynamic.Stream2WPStream(str) ,false,"RTF");


                      new WPDynamic.Stream2WPStream(str));


Select the "in memory" PDF creation mode on web server (requires "Internet Server" License)


                 pdf.PdfCreator.PDFFile = "memory";

                 pdf.PdfCreator.FontMode = 0; // embed all fonts!




Reformat the text




And create PDF




Initialize the HTTP headers.


                 // remove any output


                 // Add new header

                 Response.ContentType = "application/pdf";

                 Response.AddHeader("Content-Type", "application/pdf");

                 // Set a file name which is displayed


                 // Write the PDF data



and use "ResultBuffer" to assign the Response

















                 Response.Write("Internal Error in PDF Creation");





                 pdf.Dispose(); // Don't forget!






         #region automatically created code [...]






File test.aspx contains a single line:

<%@ Page Language="c#" Debug="true" Codebehind="/WebForm1.aspx.cs" AutoEventWireup="false" Inherits="RTF2.WebForm1" %>


To work with ASP.NET please copy the engine DLL (RT2PDF03.DLL) and the DLL wPDF.DLL into the directory bin.


Note: With wRTF2PDF V4 You will need the DLLs wPDF4.DLL and wRT2PDF04W.DLL.


In our demo the \bin directory contains:



The file in the root directory (wwwroot) are



Note: Often only the compiled DLL (here RTF2.DLL) is required for the ASP program. The other files which may be created by your developing tool are not created and can confuse the web server. The one line test.aspx mentioned above was enough to start this program!



[asp_net_example.htm]    Copyright © 2007 by WPCubed GmbH