Last week I blogged about sending the contents of a RichText field (including images and attachments) as an HTML mail. Today’s task was related: get the contents of a RichText item as HTML, knowing that the contents can be stored in the item as either MIME or standard RichText.
With the wrapDocument() function I showed you last week you can easily get MIME contents as HTML. It turns out that with some small changes you can use the same method to let Domino convert ‘standard’ RichText into HTML. You probably saw this in action already: create a document with RichText in the Notes client and edit it on the web. What’s new is that this method allows you to do it programmatically.
The trick is in the DominoRichTextItem class: it has two constructors to create it: using either a MIMEEntity or by giving it a RichTextItem. That’s all the info I needed to update my previous function:
/* * Wraps a lotus.domino.Document as a com.ibx.xsp.model.domino.wrapped.DominoDocument, including a RichText item * * @param doc document to be wrapped * * @param richTextItemName name of the rich text item containing standard RichText or MIME contents that need to be wrapped */ private static DominoDocument wrapDocument(final Document doc, final String richTextItemName) throws NotesException {</code> DominoDocument wrappedDoc = null; Database db = doc.getParentDatabase(); //disable MIME to RichText conversion db.getParent().setConvertMIME(false); //wrap the lotus.domino.Document as a lotus.domino.DominoDocument //see http://public.dhe.ibm.com/software/dw/lotus/Domino-Designer/JavaDocs/DesignerAPIs/com/ibm/xsp/model/domino/wrapped/DominoDocument.html wrappedDoc = DominoDocument.wrap(doc.getParentDatabase().getFilePath(), doc, null, null, false, null, null); //see http://public.dhe.ibm.com/software/dw/lotus/Domino-Designer/JavaDocs/DesignerAPIs/com/ibm/xsp/model/domino/wrapped/DominoRichTextItem.html DominoRichTextItem drti = null; Item itemRT = doc.getFirstItem(richTextItemName); if (null != itemRT) { if (itemRT.getType() == Item.RICHTEXT) { //create a DominoRichTextItem from the RichTextItem RichTextItem rt = (RichTextItem) itemRT; drti = new DominoRichTextItem(wrappedDoc, rt); } else if (itemRT.getType() == Item.MIME_PART) { //create a DominoRichTextItem from the Rich Text item that contains MIME MIMEEntity rtAsMime = doc.getMIMEEntity(richTextItemName); drti = new DominoRichTextItem(wrappedDoc, rtAsMime, richTextItemName); } } wrappedDoc.setRichTextItem(richTextItemName, drti); return wrappedDoc; }
Using this function you can wrap any document and get the HTML from a RichText item:
View view = db.getView("someview"); Document doc = view.getFirstDocument(); DominoDocument ddoc = wrapDocument(doc, "Body"); DominoRichTextItem drti = ddoc.getRichTextItem("Body"); String html = drti.getHTML(); System.out.println(html);