RTF Process Using TOM

2024-02-10 06:08
文章标签 process using tom rtf

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/696317

相关文章

使用C#实现将RTF转换为PDF

《使用C#实现将RTF转换为PDF》RTF(RichTextFormat)是一种通用的文档格式,允许用户在不同的文字处理软件中保存和交换格式化文本,下面我们就来看看如何使用C#实现将RTF转换为PDF... 目录Spire.Doc for .NET 简介安装 Spire.Doc代码示例处理异常总结RTF(R

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

Unity Post Process Unity后处理学习日志

Unity Post Process Unity后处理学习日志 在现代游戏开发中,后处理(Post Processing)技术已经成为提升游戏画面质量的关键工具。Unity的后处理栈(Post Processing Stack)是一个强大的插件,它允许开发者为游戏场景添加各种视觉效果,如景深、色彩校正、辉光、模糊等。这些效果不仅能够增强游戏的视觉吸引力,还能帮助传达特定的情感和氛围。 文档

出现 E: Sub-process /usr/bin/dpkg returned an error code (1) 解决方法 (全面分析)

目录 前言1. 问题所示2. 原理分析2.1 第一阶段2.2 第二阶段 3. 解决方法4. 彩蛋4.1 错误不提示,直接卸载4.2 卸载后还是无错误提示 前言 3年前遇到过一个类似的,但是轻松解决,推荐阅读:ubuntu:E: dpkg was interrupted, you must manually run ‘sudo dpkg --configure…解决方法 这回发

【Android studio】 unable to start the daemon process

这几天在做一个安卓桌面项目时,突然发现android studio 不能用了。 提示: 网上的一些方法,要不就是: 1、删除C:\Users\<username>\.gradle 文件夹 2、File Menu - > Invalidate Caches/ Restart->Invalidate and Restart 3、C:\Users\<us

论文《Autoencoders for improving quality of process event logs》翻译

论文《Autoencoders for improving quality of process event logs》翻译 《Autoencoders for improving quality of process event logs》翻译

Build Min Heap Using Array

Build min-heap using Array. 思路1:首先明白heap的底层implementation就是array,从0开始的parent和left,right的关系为, 如果现在的node index为i,那么parent index就是 (i-1)/2;   left  为2*i+1, right为 2*i+2;          ( i-1 ) / 2

Implement Set using Array.

参考链接:http://faculty.washington.edu/moishe/javademos/ch03%20Code/jss2/ArraySet.java 被Pivotal的面试官给问到了,trick的地方在于remove的那一块,要把最后的元素跟自己remove的元素进行互换,然后count--;还有,自动扩容那块,构造函数需要两个,一个默认的,一个是可以限定side的。然后扩容的时

Thread VS Process

区别如下: 1) Both process and Thread are independent path of execution but one process can have multiple Threads.   2) Every process has its own memory space, executable code and a unique process i

Implement Rand10() Using Rand7()

Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10. Do NOT use system's Math.ra