【问题标题】:Programmatically editing a RTF to be compatible with WordPad以编程方式编辑 RTF 以与写字板兼容
【发布时间】:2011-06-25 23:24:29
【问题描述】:

我正在将数据从一个文档管理系统传输到另一个系统。

在旧系统中,他们有一个用于插入替换字段的书签按钮。我需要替换替换字段的语法,以便它们可以与新系统一起使用(不是我遇到的问题)。

旧的 RTF

{\rtf1\ansi\deflang1033\ftnbj\uc1\deff1
{\fonttbl{\f0 \froman \fcharset0 Times New Roman;}{\f1 \fswiss Arial;}}
{\colortbl ;\red255\green255\blue255 ;\red0\green0\blue0 ;}
{\stylesheet{\f1\fs20\cf2\cb1\ulc2 Normal;}{\cs1\cf2\cb1\ulc2 Default Paragraph Font;}}
{\*\revtbl{Unknown;}}
\paperw12240\paperh15840\margl1440\margr1440\margt1440\margb1440\headery720\footery0\deftab720\formshade\aendnotes\aftnnrlc\pgbrdrhead\pgbrdrfoot
\sectd\pgwsxn12240\pghsxn15840\marglsxn1440\margrsxn1440\margtsxn1440\margbsxn1440\headery720\footery0\sbkpage\pgncont\pgndec
\plain\plain\f1\fs20\ql\plain\f1\fs20 TEST\lang1033\f1  {\field\fldlock{\*\fldinst MERGEFIELD ID}{\fldrslt}} TEST\plain\f1\fs20\par}

在他们的旧系统中打印:

测试 {ID} 测试

{ID} 在打印时将被替换为正确的 ID 号。

但是这是我的问题如果我只是在写字板中打开 RTF,它看起来像

测试测试

保存 RTF 后的样子

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss Arial;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\f0\fs20 TEST  TEST\par
}

我真的不关心大多数其他元数据,但我不明白为什么它会剥离 {ID}。从looking on MSDN 可以看出,\field\fldlock{\*\fldinst MERGEFIELD ID}{\fldrslt} 的格式不正确。

我应该只写一个正则表达式来匹配字段标签并将它们去掉还是有更好的解决方案?

编辑

如果我在 Word 中打开 RTF 也会发生这种情况,但它会生成一个文件 too long to post here

【问题讨论】:

    标签: ms-office rtf wordpad


    【解决方案1】:

    我最终使用了正则表达式,如果有人好奇,这里是模式

    \{(?:\s*\\\*)?\s*\\field\s*\\fldlock\s*\{\s*\\\*\s*\\fldinst\s*MERGEFIELD\s*(\\.*?)?([\w\[\]]+)(\s+\w+)?(\s*\\.*?)?\s*\}(?:\s*\{\s*\\fldrslt\s*\})?\s*\}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2020-10-16
      • 2010-09-21
      • 1970-01-01
      相关资源
      最近更新 更多