Programmatically editing a RTF to be compatible with WordPad
I am in the process of transferring data from one document management system to another system.
In the old system they had a bookmark button for inserting replacement fields. I need to replace the syntax for the replacement fields so they will work with the new system (Not the issue I am having).
Old 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}
Which prints in their old system:
TEST {ID} TEST
And {ID}
would be replaced with the correct ID number when printed.
However here is my problem If I just open the RTF in WordPad it looks like
TEST TEST
and after saving the RTF looks like
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss Arial;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\f0\fs20 TEST TEST\par
}
I really don't care about most of the other metadata, but I don't understand is why it is stripping out the {ID}
. From what I can tell by looking on MSDN there is noting malformed about \field\fldlock{\*\fldinst MERGEFIELD ID}{\fldrslt}
.
Should I just write a regular expression to match the field tags and just strip them out or is there a better solution?
EDIT
This also happens if I open up the RTF in Word, but it makes a file too long to post here.
I ended up using regex, if anyone is curious, here is the pattern
\{(?:\s*\\\*)?\s*\\field\s*\\fldlock\s*\{\s*\\\*\s*\\fldinst\s*MERGEFIELD\s*(\\.*?)?([\w\[\]]+)(\s+\w+)?(\s*\\.*?)?\s*\}(?:\s*\{\s*\\fldrslt\s*\})?\s*\}
精彩评论