Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

c# - Convert RTF to PDF format

Any ideas how to convert RTF to Text format and to keep both style and fonts?

NOTE:

I want to avoid using System.Windows.Forms

Update

I have something like this in RTF

<P style="TEXT-ALIGN: center; MARGIN: 0in 0in 0pt" class=MsoNormal align=center><FONT
face=Calibri><SPAN style="LINE-HEIGHT: 115%; FONT-SIZE: 14pt"><STRONG>Questions and
Answers<BR style="mso-special-character: line-break"></STRONG><BR style="mso-specialcharacter:
line-break"></SPAN><SPAN style="LINE-HEIGHT: 115%; FONT-SIZE:
12pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office"
/><o:p></o:p></SPAN></FONT></P>
...
#558ED5; mso-style-textfill-fill-themecolor: text2; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfillfill-
colortransforms: 'lumm=60000 lumo=40000'"><FONT color=#3366ff><SPAN style="LINEHEIGHT:
115%; FONT-SIZE: 14pt"><A title="Context Menu Default Items"
href="http://PG1/FocusOrthoServer/Images/Help/Helps/FocusOrthoHelp.aspx?HelpPageID=368"
target=_blank><FONT size=3 face="Times New Roman">Context Menu Default
Items</FONT></A></SPAN></FONT></SPAN></U><FONT
color=#000000>.<o:p></o:p></FONT></SPAN></P></FONT></FONT><o:p></o:p>

Which actually displays a help text. I need to write that text to a PDF as it is!

P.S

I am using iTextSharp

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edit :

Based on your edit the answer is a lot simpler. Just parse the file and store the individual elements together with their formatting tags.

Then create a pdf document and write out the text elements applying styles that match the original formatting tags. It's time consuming to implement but it will work.

I've done something similar a few years ago.

Alternatively you could use PDFSharp's virtual printer (or any other pdf virtual printer for that matter) to do this hassle free. Just print to PDF :)

Original Post :

You cannot have a plain text format (that's what I can assume from your question) that would have text formatting.

Tags is how formats like RTF maintain formatting in the first place. So getting rid of all tags gives you a plain text document.

However, you could have some sort of a meta file that would store the formatting data about your plain text document.

The idea is simple, you use word positions (line and column numbers) as keys to formatting tags that you will store in an xml or any other file format you want. You will end up with two files plain.txt and plain.meta.

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...