Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to output in a format readable by WordPerfect

Status
Not open for further replies.

DarwinIT

Programmer
Apr 25, 2008
142
US
I need to generate documents which can be opened in WordPerfect. My first inclination is to use rtf. We don't have WordPerfect in our office but the client insists on using it so I was seeking a middle ground. I need to be able to specify page breaks when I hit a certain number of lines and print new headers on each new page with a Continued next to them. Does anybody have a slick way to do something like this or am I scrambling to use the richtextbox control to create the rtf text and then save it to a file?
Thanks!!
 
You can use XML and XSLT to transform the XML into an RTF document. Here is a simple example:

Code:
private XmlDocument xmlDoc; 
private XmlDocument xslDoc; 
private XmlDocument WriteXML()
{
    string xml = "<Example><RTF>This is a simple example</RTF></Example>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    return doc;
}
private void CreateRTFFromXslt()
{
    xmlDoc = WriteXML();
    StringBuilder sb = new StringBuilder();
    sb.Append("<xsl:stylesheet version='1.0' xmlns:xsl='[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform'[/URL] xmlns:msxsl='urn:schemas-microsoft-com:xslt' xmlns:dt='urn:schemas-microsoft-com:datatypes' xmlns:user='urn:my-scripts'> ");
    sb.Append("<xsl:output method='text' />");

    sb.Append("<xsl:template match='Example'>");
    sb.Append(@"<xsl:text>{\rtf1\ansi{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}{\f1\fnil Arial;}{\f2\fnil\fcharset0 Arial;}{\colortbl;\red255\green0\blue0;\red51\green153\blue102;\red51\green102\blue255;\red0;\green0\blue0;}}</xsl:text> ");
    sb.Append(@"<xsl:text>\viewkind4\uc1\f0\fs20</xsl:text> ");
    sb.Append("<xsl:apply-templates select='RTF' /> ");
    sb.Append(@"<xsl:text>\line </xsl:text>");
    sb.Append(@"<xsl:text>}</xsl:text> ");
    sb.Append("</xsl:template>");

    sb.Append("</xsl:stylesheet>");
    xslDoc = new XmlDocument();
    xslDoc.LoadXml(sb.ToString());
    CreateRTFDocument(); 
}
private void CreateRTFDocument()
{
    DataSet ds;
    XmlDataDocument dataDoc;
    XslCompiledTransform xslTran;
    XmlElement root;
    XPathNavigator nav;
    XmlTextWriter writer;

    try
    {
        ds = new DataSet();
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        xmlDoc.Save(stream);
        stream.Position = 0;
        ds.ReadXml(stream);
        dataDoc = new XmlDataDocument(ds);
        xslTran = new XslCompiledTransform();
        xslTran.Load(xslDoc);
        root = dataDoc.DocumentElement;
        nav = root.CreateNavigator();
        string savePath = "C:\\Example.rtf"; 
        if (System.IO.File.Exists(savePath))
        {
            System.IO.File.Delete(savePath);
        }
        writer = new XmlTextWriter(savePath, System.Text.Encoding.Default);
        xslTran.Transform(nav, writer);
        writer.Close();
        writer = null;
        nav = null;
        root = null;
        dataDoc = null;
        ds = null;
    }
    catch (Exception ex)
    {
        writer = null;
        nav = null;
        root = null;
        dataDoc = null;
        ds = null;
    }
}

Again, this is a simple example so I'm not doing anything special with the text in the rtf doc. You can run this example by copying and pasting the code and calling CreateRTFFromXslt(). The save path is C:\RTFExample.rtf ... For full documentation on rtf check out the specification..
Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top