使用OpenXml转换docx内容为RTF

2024-04-30 07:32

本文主要是介绍使用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;}}
}

欢迎访问《许阳的红泥屋

这篇关于使用OpenXml转换docx内容为RTF的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#中Trace.Assert的使用小结

《C#中Trace.Assert的使用小结》Trace.Assert是.NET中的运行时断言检查工具,用于验证代码中的关键条件,下面就来详细的介绍一下Trace.Assert的使用,具有一定的参考价值... 目录1、 什么是 Trace.Assert?1.1 最简单的比喻1.2 基本语法2、⚡ 工作原理3

C# IPAddress 和 IPEndPoint 类的使用小结

《C#IPAddress和IPEndPoint类的使用小结》本文主要介绍了C#IPAddress和IPEndPoint类的使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录一、核心作用网络编程基础类二、IPAddress 类详解三种初始化方式1. byte 数组初始化2. l