使用OpenXml转换docx内容为RTF

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

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;
        }
    }
}

欢迎访问《许阳的红泥屋

你可能感兴趣的:(open)