This article provides an example of how to convert HTML to RVFz and set it as Description text on an Item using the API. The HTML converter supports HTML 4.
Example
IswItem i = SWConnection.Instance.Broker.GetItem(SWHandleUtility.ToHandle(s)); // The file to import string tempFile = "C:\\Temp\\example.html"; // Default font for the conversion string FontStyle = "Arial"; // Convert the HTML file to RVFz. byte[] desc = SystemWeaver.Common.SWUtility.HtmlToRvfz(tempFile, FontStyle); // Set it as a description on an Item i.Description = SWDescription.MakeDescription(desc);
Other Options
If the HTML to be converted is not standardized, e.g., no header, encoding specifier, etc., it may have been converted from RTF. In such a case, we recommend trying one of the many available alternatives to do the conversion before doing the import into SystemWeaver. One example is to use DevExpress. Below is example code.
DevExpress Example
using SystemWeaverAPI;
using SystemWeaver.Common;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;
namespace FixHtml
{
public class WriteHtmlToItemDescription
{
IswBroker _broker;
public WriteHtmlToItemDescription(IswBroker broker)
{
_broker = broker;
}
public void SetImportedDescription()
{
// Parent (maybe create new items?): x040000000003984E
// For Imported_w_ClientAPI: x0400000000039855
// Attempt2: x040000000003985C
// Test_html1 x0400000000039869
// Test_html2 x040000000003986D
// The file to import
string htmlFile =
"C:\\jghome\\work\\git_sw_general\\issues\\issue_html_import\\"
+ "ABC-SysE-12345[1].html";
SetItemByIdDescFromHTML("x0400000000039855",
htmlFile);
}
private void SetItemByIdDescFromHTML(string itemId, string htmlFile)
{
long handle = SWHandleUtility.ToHandle(itemId);
IswItem item = SWConnection.Instance.Broker.GetItem(handle);
SetItemDescFromHTML(item, htmlFile);
}
private void SetItemDescFromHTML(IswItem i, string htmlFile)
{
byte[] desc = GetRTFDataFromHTML_DevExpress(htmlFile);
i.Description = SWDescription.MakeDescription(desc);
}
// This functiion produces Description formatting very close or identical to the browser
// rendered version of the provided sample
private byte[] GetRTFDataFromHTML_DevExpress(string htmlFile)
{
byte[] desc;
// DevExpress.XtraRichEdit.RichEditDocumentServer
using (var wordProcessor = new RichEditDocumentServer())
{
wordProcessor.LoadDocument(htmlFile, DocumentFormat.Html);
string rtf = wordProcessor.RtfText;
desc = SystemWeaver.Common.SWUtility.RtfToRvfz(
rtf, _broker);
}
return desc;
}
private byte[] GetRTFDataFromHTML_SWUtility(string htmlFile)
{
string FontStyle = "Arial";
// Convert the HTML file to RVFz.
byte[] desc = SystemWeaver.Common.SWUtility.HtmlToRvfz(htmlFile, FontStyle);
return desc;
}
}
}