QuickStart

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

The conversion of a RTF file to PDF does not require much code.

 

(Please note that we have created hyperlinks for the important types to their description.)

 

A) Convert just a RTF, HTML or MSG file into PDF - C# Code

 

rtF2PDF1.SetLicense("","",0); // Your data here ....

rtF2PDF1.FileName = "c:\\newpdf.pdf";

if(!rtF2PDF1.LoadRTF( TemplateName.Text ))

   MessageBox.Show("Cannot load " + TemplateName.Text, "");

else

{

   rtF2PDF1.Export();

}

 

 

B) Use the new TextDynamic Interfaces

 

a) Export a File (VS2008 Demo) (use IWPMemo)

 

using wPDF;

using WPDynamic;

 

namespace TestReport

{

  class Program

   {

      static void Main(string[] args)

       {

          RTF2PDF pdf = null;

          try

           {

               pdf = new RTF2PDF();

              pdf.SetLicense("...""...", xxx);

               pdf.Memo.LoadFromFile("c:\\Description.RTF");

               pdf.Export("c:\\temp\\Description.pdf");

           }

          finally

           {

               pdf.Dispose();

           }

       }

   }

}

 

b) Convert HTML or mime encoded emails (HTM, MSG, MHT, EML)

 

using wPDF;

using WPDynamic;

 

namespace TestReport

{

  class Program

   {

      static void Main(string[] args)

       {

          RTF2PDF pdf = null;

          try

           {

               pdf = new RTF2PDF();

               pdf.SetLicense("...", "...", xxx);

 

              // Activate HTML specific formatting

              pdf.Memo.SetBProp(BPropSel.wpViewOptions, 0, 1); 

              // Load a Mime encode e-mail

              pdf.Memo.LoadFromFile("c:\\Test.EML");

               pdf.Export("c:\\temp\\Mail.pdf");

           }

          finally

           {

               pdf.Dispose();

           }

       }

   }

}

 

c) Create some text "on the fly" (use IWPTextCursor)

 

RtF2PDF1.SetLicense("...","xxxx-yyyy-zzzz-0000",12345);

RtF2PDF1.Memo.LoadFromFile(@"c:\testfile.rtf",false,"AUTO");

         

// requires: using wPDF; using WPDynamic; - we add some text

IWPTextCursor Cursor;

Cursor = RtF2PDF1.Memo.TextCursor;

Cursor.CPPosition = 0;

Cursor.InputString("Some text at the start\r"0);

 

// Now create PDF file

RtF2PDF1.PdfCreator.PDFFile = @"c:\temp\\newfile.pdf";

RtF2PDF1.PdfCreator.Print();

 

 

d) Create Bookmarks

 

PDF Bookmarks are the optional table of content entries. RTF2PDF is able to create those strings automatically from the loaded text. To do so the paragraphs which should appear in this list should be marked with the property WPAT.ParIsOutline.

 

You can use the Memo.TextCommandStr to mark paragraphs which either use a certain style or which start with certain text to be used in the outline.

 

To create the TOC as text (within the document) use

 

Cursor.CPPosition = 0xFFFFFF;

Cursor.InputParagraph(0,"");

rtF2PDF1.Memo.TextCommand(2, 2);

 

C) Simple ASP.NET demo which sends the PDF as response

 

RTF2PDF pdf = new RTF2PDF();

pdf.Memo.LoadFromFile("somtext.rtf"true"AUTO");

pdf.PdfCreator.PDFFile = "memory";

// remove any output

Response.Clear();

// Add new header

Response.ContentType = "application/pdf";

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

// Set a file name which is displayed

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

// Write the PDF data

Response.Clear();

// Finish

pdf.Dispose();

 

D) Activate Format mode optimized for HTML

 

a) Use Memo.SetBProp

Group 7 : wpAsWebPage

 

b) Use RtF2PDF1.Command(1312, 1) // WPCOM_SELECT_HTML_MODE = 1312;

 

E) Use the optional reporting feature

 

using wPDF;

using WPDynamic;

 

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

{    

   // Set License with Reportion Option 

   rtF2PDF2.SetLicense( "---""---"0);

 

   // Load a File

   rtF2PDF2.Memo.LoadFromFile(@"c:\Template_With_Tokens.RTF"false"");

 

   // Use the Token To Template Conversion

   rtF2PDF2.Memo.TextCommandStr(17,0"");

 

   // Create the Report (will be in Memo2)

   rtF2PDF2.Report.CreateReport();

 

   // Set Output Filename

   rtF2PDF2.FileName =  @"c:\test.pdf";

 

   // And convert Memo2 to PDF

   rtF2PDF2.PrintSecond();

}

 

We are also using this event to loop each group 10 times:

 

private void rtF2PDF2_OnReportState(object Sender, string Name, int State, WPDynamic.IWPReportBand Band, ref bool Abort)

{

      if (State==0)

      Abort = Band.Count>10;

}

 

F) VB6 Example using BeginDoc/EndDoc

  Dim Memo As IWPEditor ' TextDynamic uses IWPMemo.  

  PDFControl1.StartEngine "S:\S\wPDFControl\wPDFControlDemo.dll", "lic", "lic", 0

  Set Memo = PDFControl1.Memo

  Memo.LoadFromFile "sometext.rtf", False, "AUTO"  

  ' Create PDF File

  PDFControl1.BeginDoc "new2.pdf", 0

  ' We cannot call "Print" in VB6

  PDFControl1.ExecIntCommand 1305, 0

  PDFControl1.EndDoc

G) VB6 Example: wRTF2PDF using PDFCreator:

  Dim Memo As IWPEditor ' TextDynamic uses IWPMemo.

  Dim PDFCreator As IWPPdfCreator

  ' TODO: Modify license codes

  PDFControl1.StartEngine "wPDFControlDemo.dll", "lic", "lic", 0

  Set Memo = PDFControl1.Memo

  Set PDFCreator = PDFControl1.PDFCreator

  Memo.LoadFromFile "sometext.rtf", False, "AUTO"

  PDFCreator.PDFFile = "new.PDF"

  ' PDFCreator.Print - this cannot be used due to VB "bug"

  ' Use command instead

  PDFControl1.ExecIntCommand 1313, 0


[quickstart2.htm]    Copyright © 2007 by WPCubed GmbH