Click here to Skip to main content
6,632,253 members and growing! (19,289 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

PDF creation using C# (and Office) from RTF/DOC files

By Stefan Eberhardt

Converts RTF, DOC to PDF; sample is a part of a big Project that converts nearly everything, parts can be used to convert html, bmp ,Lotus 1-2-3 documents...
C#.NET 1.1, WinXPVS.NET2003, Dev
Posted:25 Feb 2004
Views:186,756
Bookmarked:87 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
34 votes for this article.
Popularity: 4.93 Rating: 3.22 out of 5
9 votes, 26.5%
1
4 votes, 11.8%
2
1 vote, 2.9%
3
8 votes, 23.5%
4
12 votes, 35.3%
5

Introduction

Some time ago I had to write a C# application that was able to convert documents into various formats. The hardest part was to find a way to create PDF files without the use of any third party products. Here is a solution.

Background

The source you see is out of a larger conversion-application. It is a "stand alone" projects, for educational use and describes a possible way of converting documents. I took me a lot of work to figure this out, so please don't copy the code; drop me a line if you wish to use a part of it. My comments are written in German, I had no time to build a proper release, sorry for that.

Using the code

The code listed below describes the main part of the program. I will give you a brief look, at the idea behind. Crystal reports (the .NET reporting system) is able to create PDF files. The only problem is it can only create PDF files out of a database. It requires an ole objects in the Database. But if you have a (very) close look at CR (IDA :-) ) you will find out that it is able to process, bmp, emf, and wmf. So we only have to insert this kind of data in to a table (as blob) and hand it over to CR. Emf can be created by using PowerPoint, PPT can read html, WinWord can create html. The only problem left is the organization of our pages, we have to split the document manually, I did this by using a Richtextbox.

Now that we know the Way we can convert a rtf into a PDF:

  1. We load the rtf into a richtextbox.
  2. We split in into parts. every part is loaded into WinWord, saved as html, the WinWord header is being destroyed, the html page is loaded into PowerPoint and saved as emf. The emf file is written into a Access database as a blob object.
  3. Crystal Reports gets a rpt "template" the database the report is being created and saved as PDF.

I tried to use Ole32 functions, but I didn't find a way to accomplish this, if you know a way in C#.NET please let me know.

   private static void DoRTF2ALL(
      CrystalDecisions.Shared.ExportFormatType outTp)
    {
      int lastsplit = 0;
      int nextsplit = 0;
      int pageheight= 650;  //"L�nge" unserer Seite

      int pcount= 1;
      Point xx;
      object Unknown =Type.Missing;
      Word.Application newApp;
      PowerPoint.Application  app;
      PowerPoint.Presentation ppp;
      string[] TempEnt;
      

      
      RichTextBox rtf = new RichTextBox();
      rtf.Height=25000;// nur page size required

      rtf.Width=4048;// Solle reichen

      //Console.WriteLine(rtf.Bounds.ToString());

      rtf.LoadFile(scrfile, RichTextBoxStreamType.RichText);
      nCoreHlp.EmptyDB(WorkDir + "\\" + Database);
      while ((lastsplit+1)<rtf.Text.Length) ////start page split
      {


        // die ersten paar seiten wegschneiden
        rtf.SelectionStart = 0;
        rtf.SelectionLength =lastsplit;
        rtf.Cut();
        for (int r=0;r<=rtf.Text.Length;r++) ////parse through whole text
        {
          xx = rtf.GetPositionFromCharIndex(r);
          nextsplit = rtf.Text.Length;
          if (int.Parse(xx.Y.ToString()) > pageheight)
              {nextsplit=r-1;r=rtf.Text.Length;}
          
        }
        lastsplit=lastsplit+nextsplit;// ende wegschneiden
        rtf.SelectionStart = nextsplit;
        rtf.SelectionLength =rtf.Text.Length-nextsplit;
        rtf.Cut();
        rtf.SaveFile(WorkDir + \\temp.rtf, 
               RichTextBoxStreamType.RichText);
        //////////////////////////////////////////// insert db
        newApp = new Word.Application(); 
        newApp.Visible = false;

        object Source=WorkDir + "\\temp.rtf";
        object Target=WorkDir + "\\temp.html";

        newApp.Documents.Open(ref Source,ref Unknown, 
          ref Unknown,ref Unknown,ref Unknown, 
          ref Unknown,ref Unknown,ref Unknown, 
          ref Unknown,ref Unknown,ref Unknown, 
          ref Unknown,ref Unknown,ref Unknown,ref Unknown);

        object format = Word.WdSaveFormat.wdFormatHTML;// kein XML, nutzen?
        newApp.ActiveDocument.SaveAs(ref Target,ref format, 
          ref Unknown,ref Unknown,ref Unknown, 
          ref Unknown,ref Unknown,ref Unknown, 
          ref Unknown,ref Unknown,ref Unknown, 
          ref Unknown,ref Unknown,ref Unknown, 
          ref Unknown,ref Unknown);
        newApp.Quit(ref Unknown,ref Unknown,ref Unknown);
      
        //kill word head
        StreamReader sr;
        bool not=true;
        while (not)
        {
          try
          {

            sr = new StreamReader(WorkDir + "\\temp.html");
            not=false;
            StreamWriter sw = new StreamWriter(WorkDir + "\\temp.txt"); 
            String line;

            while ((line = sr.ReadLine()) != null) 
            {
              if (line.CompareTo(
                "<meta name=ProgId content=FrontPage.Editor.Document>")!=0)
                sw.WriteLine(line); else
                //line.Replace //Marina
                sw.WriteLine("<meta name=ProgId content=Word.Documens>");
            }

            sr.Close();
            sw.Flush();
            sw.Close();

          }
          catch (Exception e){e=e;}
        }// kill word head end
      
        File.Delete(WorkDir + "\\temp.html");
        File.Move(WorkDir + "\\temp.txt", WorkDir + "\\temp.html");
        //File.Delete(WorkDir + "\\temp.txt");        


        app = new PowerPoint.Application();
        ppp = app.Presentations.Open(WorkDir + "\\temp.html",
          /*Microsoft.Office.Core.MsoTriState.msoCTrue*/0,
          /*Microsoft.Office.Core.MsoTriState.msoTrue*/0,
          Microsoft.Office.Core.MsoTriState.msoFalse);//visible? immer no
        ppp.SaveAs(WorkDir + "\\temp",
          PowerPoint.PpSaveAsFileType.ppSaveAsEMF,
          Microsoft.Office.Core.MsoTriState.msoFalse);
        app.Quit();

        //output fangen
        TempEnt = Directory.GetFiles(WorkDir + "\\temp\\", "*.emf");
        nCoreHlp.InsertDB1(WorkDir + "\\" + Database,TempEnt[0],pcount);
        pcount++;
        Console.WriteLine("1 Page Converted");// debug
        //Console.ReadLine();
        ////////////////////////////////////////////////// insert db off
        rtf.LoadFile(scrfile, RichTextBoxStreamType.RichText);

      }//////////// page split done
      ///Create PDF
      ReportDocument doc = new ReportDocument();

      doc.Load(WorkDir + "\\DtoD.rpt");
      doc.Database.Tables[0].Location = (WorkDir + "\\DtoD.mdb");

      doc.ExportOptions.ExportFormatType = outTp;
      doc.ExportOptions.ExportDestinationType =
              ExportDestinationType.DiskFile;
      //DiskFileDestinationOptions
      DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
      diskOpts.DiskFileName = dstfile;
      doc.ExportOptions.DestinationOptions = diskOpts;
      doc.Export();
      doc.Close();
      Directory.Delete(WorkDir + "\\temp\\", true);
      File.Delete(WorkDir + "\\temp.rtf");
      File.Delete(WorkDir + "\\temp.html");
    }
      

Points of Interest

MS-Office 2000 or < has to be installed. I included the Word & PowerPoint interfaces in the project , the C# dlls are only in the bin/debug directory, because of the file size.

History

  • 1st version of the demo project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Stefan Eberhardt


Member

Location: Germany Germany

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 37 (Total in Forum: 37) (Refresh)FirstPrevNext
GeneralMy vote of 1 Pinmemberrutstyle21:26 19 Aug '09  
GeneralThis stuff won't compile Pinmemberstapes0:22 6 Feb '09  
GeneralSource Code PinmemberJansen2085:51 23 Sep '08  
GeneralRe: Source Code PinmemberAhmedSuria4:02 10 Oct '08  
GeneralCool things Pinmemberggraham4129:52 20 Jun '08  
GeneralLicense PinmemberAleksei Krassovskikh3:22 27 Apr '08  
GeneralGive the author an award Pinmemberrimblock8:06 15 Nov '07  
GeneralGlad that you are impressed with your code but... Pinmemberhybridguy12:52 27 Jul '07  
RantRe: Glad that you are impressed with your code but... Pinmemberggraham4129:34 20 Jun '08  
General100% Managed HTML and RTF to PDF Converter Libraries Pinmemberwinnovative11:05 8 Jul '07  
Generaldoc conversion Pinmemberjaneeta22:09 30 Mar '07  
GeneralBad Pinmember[V]GreyWolf5:44 15 Feb '07  
GeneralConversion for pdf,word Doc, and ppt PinmemberMohdfaiz22:52 4 Feb '07  
GeneralError in ReportDocument.Export PinmemberMarcos Almeida12:34 29 Sep '06  
GeneralRe: Error in ReportDocument.Export PinmemberZevsky21:54 24 Jun '07  
GeneralWord to PDF creater PinmemberRajput Jitendra22:42 8 May '06  
GeneralRe: Word to PDF creater Pinmemberawen_k6:02 1 Sep '08  
Generalhow to create DtoD.rpt Pinmembersridhar chatla7:08 13 Apr '06  
Generalerror Pinmembersewedy15:51 3 Mar '06  
GeneralRe: error PinmemberDoan Quang Minh21:17 16 Jan '07  
GeneralDemo Code PinmemberDavey P6:31 13 Jan '06  
QuestionI would like to use a part of your implementation Pinmemberbigb_60216:52 15 Nov '05  
Generalbmp to pdf conversion Pinmemberleojose21:24 26 May '05  
GeneralRe: bmp to pdf conversion PinmemberRefre0774:13 25 Oct '05  
GeneralBugs PinmemberZu Luong9:28 29 Apr '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Feb 2004
Editor: Nishant Sivakumar
Copyright 2004 by Stefan Eberhardt
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project