Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • OfficeWriter Enterprise Edition
  • WordWriter dll (SAWW3NET.DLL or SoftArtisans.OfficeWriter.WordWriter.dll) version 3.8.1.665 or higher

    Note

    If you download the binary version of HTML-to-Word, your WordWriter dll must be the current version for which HTML-to-Word is built. Please see the HTML-to-Word open source project homepage for up-to-the-minute WordWriter dll version requirement info.

  • OfficeWriter server requirements as described on the SoftArtisans System Requirements page.

...

You must make sure that your HTML is well-formed. That is, every open tag must have a corresponding close tag or be self-closing (e.g. <br />). If your HTML is older and does not conform to XHTML standards, you can use any number of utilities, such as the Open Source utility HTMLTidy, to clean-up your HTML before passing it to HTMLToWord's InsertHTML method.

Code Block
c#
c#
titleC#

//--- Create a new WordWriter document.
WordApplication wApp = new WordApplication();
Document doc = wApp.Create();

//--- Create the HTMLToWord object.
HTMLToWord h2w = new HTMLToWord();

//--- The XHTML to insert into the WordWriter document.
string xhtml = "<h1>Greetings</h1><p><b>Hello</b>, <i>world</i>!</p>";

//--- Insert the XHTML at the end of the WordWriter document.
Element lastInsertedElement = h2w.InsertHTML(xhtml, doc, null, doc);
Code Block
vb.net
vb.net
titleVB.NET

'--- Create a new WordWriter document.
Dim wApp As New WordApplication()
Dim doc As Document = wApp.Create()

'--- Create the HTMLToWord object.
Dim h2w As HTMLToWord = New HTMLToWord()

'--- The XHTML to insert into the WordWriter document.
Dim xhtml As String = "<h1>Greetings</h1><p><b>Hello</b>, <i>world</i>!</p>"

'--- Insert the XHTML at the end of the WordWriter document.
Dim lastInsertedElement As Element = h2w.InsertHTML(xhtml, doc, null, doc)

...

The example below shows how to create a delegate method that will be called each time an HTML is encountered by HTMLToWord and assign it to the InsertDelegate property. The delegate method does 2 things. First, it handles a custom para tag that is in our HTML by inserting a paragraph into the Word document. Second, it tells HTMLToWord to ignore anything inside a custom private tag so that sensitive information is not written into the document.

Code Block
c#
c#
titleC#

//--- This method will be used as a delegate by HTMLToWord and
//--- is an example of type HTMLToWord.InsertElementDelegate.
 
//--- We have defined 2 custom XHTML tags for our documents: para and private.
//--- If we encounter a <para> tag, we insert a paragraph into the Word document and tell HTMLToWord
//--- to continue processing.  If we encounter a <private> tag, we write a short message into
//--- the Word document and tell HTMLToWord to skip this tag altogether.
HTMLToWord.HTMLTagAction MyInserter(Document doc, Element insertAfterElement, XmlNode node)
{
     //--- Get the name of the HTML tag and convert it to lower case.
     string nodeName = node.Name.ToLower();

     if (nodeName.equals("para"))
     {
          //--- There is a special <para> tag in our markup, but we should just treat it
          //--- like a standard HTML <p> tag.  Insert a paragraph into the document and
          //--- let HTML continue processing.
          insertAfterElement.InsertParagraphAfter(null);
     }
     else if (nodeName.equals("private"))
     {
          //--- The "private" tag is used to indicate information that should not be sent
          //--- to outside clients; therefore, we tell HTMLToWord to skip this tag and its
          //--- contents.
          return HTMLToWord.HTMLTagAction.Skip;
     }

     //--- We have handled all of the special cases.  Let HTMLToWord know that
     //--- it should process this tag.
     return HTMLToWord.HTMLTagAction.Process;
}
Code Block
vb.net
vb.net
titleVB.NET

'--- This method will be used as a delegate by HTMLToWord and
'--- is an example of type HTMLToWord.InsertElementDelegate.

'--- We have defined 2 custom XHTML tags for our documents: para and private.
'--- If we encounter a <para> tag, we insert a paragraph into the Word document and tell HTMLToWord
'--- to continue processing.  If we encounter a <private> tag, we write a short message into
'--- the Word document and tell HTMLToWord to skip this tag altogether.
Function MyInserter(ByVal doc As Document, ByVal insertAfterElement As Element, ByVal xmlNode As XmlNode)
     As HTMLToWord.HTMLTagAction

     '--- Get the name of the HTML tag and convert it to lower case.
     Dim nodeName As String = node.Name.ToLower()

     If (nodeName.Equals("para")) Then
          '--- There is a special <para> tag in our markup, but we should just treat it
          '--- like a standard HTML <p> tag.  Insert a paragraph into the document and
          '--- let HTML continue processing.
          insertAfterElement.InsertParagraphAfter(Nothing)
     Else If (nodeName.Equals("private"))
          '--- The "private" tag is used to indicate information that should not be sent
          '--- to outside clients; therefore, we tell HTMLToWord to skip this tag and its
          '--- contents.
          Return HTMLToWord.HTMLTagAction.Skip
     End If

     '--- We have handled all of the special cases.  Let HTMLToWord know that
     '--- it should process this tag.
     Return HTMLToWord.HTMLTagAction.Process
End Function

You must tell HTMLToWord about your delegate method so that HTMLToWord can call your custom code. To do that, assign your delegate method to the appropriate HTMLToWord property.

Code Block
c#
c#
titleC#
     //--- Create an HTMLToWord instance.
    HTMLToWord h2w = new HTMLToWord();

    //--- Using the code from the example above, tell HTMLToWord that it should
    //--- call our "MyInserter" delegate method whenever it encounters a tag in
    //--- the XHTML string.
    h2w.InsertDelegate = new HTMLToWord.InsertElementDelegate(MyInserter);
Code Block
vb.net
vb.net
titleVB.NET
     '--- Create an HTMLToWord instance.
    Dim h2w As New HTMLToWord()

    '--- Using the code from the example above, tell HTMLToWord that it should
    '--- call our "MyInserter" delegate method whenever it encounters a tag in
    '--- the XHTML string.
    h2w.InsertDelegate = New HTMLToWord.InsertElementDelegate(AddressOf MyInserter)

...