本文主要是介绍RTF Process Using TOM,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Using Text Object Model (TOM) to process Rich Text Format (RTF)
使用TOM提取富文本内容及格式,利用RICHEDIT2.0
// 初始化
BOOL CRTF::Init()
{
if (m_hInstRich || m_RichEditCtrl.m_hWnd)
return FALSE;
m_hInstRich = ::LoadLibrary(_T("RICHED20.DLL"));
ASSERT(m_hInstRich != NULL);
m_RichEditCtrl.CreateEx(
NULL,
_T("richedit20W"),
NULL,
WS_POPUP | ES_MULTILINE,
CRect(0,0,800,600),
NULL,
0 );
return TRUE;
}
// RTF 提取,包含主要TOM操作
BOOL CRTF::Process()
{
int mIndex=0,pageNo =0;
static long WHITE_COLOR = RGB(255,255,255);
m_ListPage.RemoveAll();
PAGENODE pageNode;
pageNode.pageNo = pageNo;
CComPtr<IRichEditOle> richOle;
richOle.Attach(m_RichEditCtrl.GetIRichEditOle());
if(!richOle) return FALSE;
CComQIPtr<ITextDocument> textDoc(richOle);
if(!textDoc) return FALSE;
CComVariant varFileName =
m_strFileName.GetBuffer(m_strFileName.GetLength());
HRESULT hr = textDoc->Open(&varFileName, tomReadOnly | tomRTF, 0);
hr &= ~0x40000; // Mask off bit 18
if(hr == STG_E_FILENOTFOUND) return FALSE;
CArray<int,int> arrParas;
arrParas.Add(0);
int lineCount = m_RichEditCtrl.GetLineCount();
for(int i = 0; i < lineCount; i++)
{
int nLineBeginCharIndex = m_RichEditCtrl.LineIndex(i);
int nLineLength = m_RichEditCtrl.LineLength(nLineBeginCharIndex);
int nLineEndCharIndex = nLineBeginCharIndex + nLineLength;
TCHAR szLineText[_MAX_PATH] = {0};
m_RichEditCtrl.GetLine(i,szLineText,nLineLength+2);
if(szLineText[nLineLength] == _T('/r'))
{
arrParas.Add(nLineEndCharIndex+1);
}
}
CTextNodeList textNodeList;
int paraCount = arrParas.GetSize() - 1;
for(i = 0; i < paraCount; i++)
{
CComPtr<ITextRange> range;
CComPtr<ITextFont> font;
CComBSTR text;
if(SUCCEEDED(textDoc->Range(arrParas[i], arrParas[i+1], &range)) && range)
{
if(SUCCEEDED(range->GetText(&text)) && text)
{
CString strLine = text;
strLine.Replace(_T('/r'),_T('/n'));
// If remove empty lines
if (IsEmptyLine(strLine) && m_nFilterEmptyLine)
continue;
if(SUCCEEDED(range->GetFont(&font)) && font)
{
float fontSize = 0;
long bold = 0;
long italic = 0;
long color = 0;
font->GetSize(&fontSize);
font->GetBold(&bold);
font->GetItalic(&italic);
font->GetForeColor(&color);
if (fontSize == tomUndefined || bold == tomUndefined)
{
CComPtr<ITextRange> range_head;
CComPtr<ITextFont> font_head;
if( SUCCEEDED(textDoc->Range(arrParas[i],arrParas[i]+1,&range_head)&&
range_head))
{
if( SUCCEEDED(range_head->GetFont(&font_head)) &&
font_head)
{
font_head->GetSize(&fontSize);
font_head->GetBold(&bold);
font_head->GetItalic(&italic);
font_head->GetForeColor(&color);
}
}
}
bold = bold ? 1 : bold;
italic = italic ? 1 : italic;
// Filter White Characters
if (m_nFilterWhiteChar && color==WHITE_COLOR)
continue;
TEXTNODE *lastNode = NULL;
if (textNodeList.GetSize()>0)
{
lastNode = &textNodeList[textNodeList.GetSize()-1];
}
bool bCat = false;
if (lastNode)
{
bCat = CatText (
lastNode->text,lastNode->size, lastNode->bold,
lastNode->italic,strLine,fontSize,bold,italic,m_nCatFlag,
lastNode->text_sec
);
if (bCat)
{
++lastNode->num;
lastNode->text_sec = strLine;
}
}
if ( !lastNode || !bCat)
{
TEXTNODE textNode;
textNode.text = strLine;
textNode.type = NODETYPE_CONTENT;
textNode.size = fontSize;
textNode.bold = bold;
textNode.italic = italic;
textNodeList.Add(textNode);
}
}
}
}
}
// Category Text
FilterArticle(&pageNode.articleList,&textNodeList);
// Add PageNode to Page List
m_ListPage.Add(pageNode);
// WriteToXml
WriteArticleToXML();
// Detach
richOle.Detach();
return TRUE;
}
// 释放资源
BOOL CRTF::Free()
{
// Destroy
if (m_RichEditCtrl.m_hWnd)
{
m_RichEditCtrl.DestroyWindow();
}
if (m_hInstRich)
{
FreeLibrary(m_hInstRich);
m_hInstRich = NULL;
}
return TRUE;
}
这篇关于RTF Process Using TOM的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!