使用OpenXml转换docx内容为RTF

63 篇文章 0 订阅
26 篇文章 0 订阅

实际上这是个非常蛋疼的命题。它需要你有两个方面的能力:

1.      你实际RTF格式。

2.      你知道在OpenXml格式的文档中各种Style存在于哪个部份。

由于完整的RTF style非常多。我这里只写了一个最简单的例子,希望它能起排线的作用:

假设我有一个docx文档如下:

Hello!

This is a test text.

用Open Xml转换成RTF代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SD = System.Drawing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using com.mksword.Net.OpenXmlTools;
using System.Text.RegularExpressions;

namespace ConsoleApplication10
{
    class Class1 : WordprocessingDocumentUtil 
    {
        private static Dictionary<char, int> map = new Dictionary<char, int>{
            {'A',10},{'B',11},{'C',12},{'D',13},{'E',14},{'F',15},{'9',9},
            {'8',8},{'7',7},{'6',6},{'5',5},{'4',4},{'3',3},{'2',2},{'1',1},
            {'0',0}};

        public void Action()
        {
            OpenedDocumentHandler(ConvertDoc2Rtf);
        }

        private bool ConvertDoc2Rtf(WordprocessingDocument WPD)
        {
            bool result = false;
            string source1 = "{\\rtf1\\ansi";
            string colortable = null;
            int colorIndex = 0;
            string strsource = "";
            MainDocumentPart MDP = WPD.MainDocumentPart;
            Document Doc = MDP.Document;
            foreach (Paragraph P in Doc.Descendants<Paragraph>().ToList())
            {
                foreach (Run R in P.Descendants<Run>().ToList())
                {
                    if (R.RunProperties == null)
                    {
                        Text T = R.Descendants<Text>().FirstOrDefault();
                        strsource += T.Text;
                    }
                    else
                    {
                        RunProperties RPs = R.RunProperties;
                        Color C = RPs.Descendants<Color>().FirstOrDefault();
                        Text T = R.Descendants<Text>().FirstOrDefault();
                        bool flag = false;
                        if (C != null)
                        {
                            if (colortable != null)
                            {
                                colortable += RTFColorTable(C);
                            }
                            else
                            {
                                colortable = "\n{\\colortbl ;";
                                colortable += RTFColorTable(C);
                            }
                            colorIndex++;
                            strsource += "\\cf" + colorIndex.ToString()+" ";
                            flag = true;
                        }
                        Underline UL = RPs.Descendants<Underline>()
                            .FirstOrDefault();
                        if (UL != null)
                        {
                            strsource += "\\ul " + T.Text + "\\ulnone";
                        }
                        else
                        {
                            strsource += T.Text+ " ";
                        }
                        if (colortable != null && flag)
                        {
                            strsource += "\\cf0 ";
                        }
                    }
                }
                strsource += "\\par\n";
            }
            if (colortable != null)
            {
                colortable += "}\n";
                source1 += colortable + strsource + "\n}";
            }
            else
            {
                source1 += strsource + "\n}";
            }
            SetLog(source1, OXULogType.INFO);
            Clipboard.SetText(source1);
            return result;
        }

        private string RTFColorTable(Color Color)
        {
            string result = null;
            Regex regex = new Regex("([0-9 a-f A-F]{2})([0-9 a-f A-F]{2})"
                + "([0-9 a-f A-F]{2})");
            Match match = regex.Match(Color.Val);
            result += "\\red" + Hex2Dec(match.Groups[1].Value);
            result += "\\green" + Hex2Dec(match.Groups[2].Value);
            result += "\\blue" + Hex2Dec(match.Groups[3].Value) + ";";
            return result;
        }

        private string Hex2Dec(string Hex)
        {
            string result = null; 
            char[] buffer = Hex.ToUpper().ToCharArray();
            int r = 0;
            for (int i = 0; i < 2; i++)
            {
                r += i == 0 ? 16 * map[buffer[i]] : map[buffer[i]];
            }
            result = r.ToString();
            return result;
        }
    }
}

欢迎访问《许阳的红泥屋

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值