RTF2PDF in ASP.NET

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

1) In your ASP.NET C# project please first add a reference to the wPDF4.DLL. This DLL is a wrapper for the engine. The full version includes the source code for this wrapper, it is written in C#. You may need to recompile it for the .NET frame work you use.

 

2) In the file module you want to use RTF2PDF add a link to wPDF:

using wPDF;

using WPDynamic;

 

3) Within the Page_Load event you create and instance of RTF2PDF - at the end don't forget Dispose()!

 

   RTF2PDF wpdllint1 = new RTF2PDF();

   try

   {

   // insert the following code here 

   }

   finally

   {

      wpdllint1.Dispose();

   }

 

4) Please set the license code. In ASP project please load the license from an encrypted file - this avoids that the keys become public in case the asp code is displayed by the server in case of an error. You can use "Demo.EXE" to create such an encrypted file. We place the license file outside of wwwroot, so it is not possible to access it.

 

     

      wpdllint1.SetLicense("@FILE@mysecretpassword""C:\\Windows\\rtflic.dat",0);

 

5) You can use code to load data, or to create text. Here we simply create a table.

 

           wpdllint1.Memo.Clear(false,false);

           wpdllint1.TextCursor.AddTable("",2,30,true,0,true,true);

 

6) When the document was created you can send it as response either as HTML or as PDF:

 

a) as HTML:

 

           Response.Write( wpdllint1.Memo.SaveToString(false"HTML") );

 

b) as PDF:

 

      // Create PDF in memory

      wpdllint1.PdfCreator.PDFFile = "memory";

      wpdllint1.PdfCreator.FontMode = 0;

      wpdllint1.Memo.ReformatAll(false,false);

      wpdllint1.Print();

   

      // Set ASP result

      Response.Clear();

      Response.ContentType = "application/pdf";

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

      Response.AddHeader("Content-Disposition","inline;filename=PortableDocument.pdf");

      Response.BinaryWrite(wpdllint1.ResultBuffer);

 

Tip: In our demos we use this code to either create PDF or HTML output:

 

   if (Request.QueryString.Get("pdf")!="true")

      // method a)

   else

      // method b)

 

 

You can also create the output as RTF text:

 

   if (Request.QueryString.Get("rtf")=="true")

   {        

      Response.Clear();

      // Add new header  for RTF 

      Response.ContentType = "application/rtf";

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

      Response.AddHeader("Content-Disposition","inline;filename=" + afile + ".rtf");

                  object buf = wpdllint1.Memo.SaveToVar(false"RTF");

                  if(buf!=null) Response.BinaryWrite((byte[])buf);

      Response.BinaryWrite(buf);

   }


[rtf_to_pdf_in_asp_net.htm]    Copyright © 2007 by WPCubed GmbH