2

Context

What we need is to capture some user input (formatted text) from a WPF application and output a PDF with some stored images AND the user input on the last page.

What we've tried

We create the WPF app, add the iTextSharp library, recover the images from the DB and add it to the PDF. That's working. Now, for the user input we added a RichTextBox control from the Extended WPF Toolkit. We added this control mainly because of its binding properties and formatters. Basically we can bind the rich content of the control to a property. That binding is working. We already have the RTF format, as (in example):

"{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch This is the }{\b\ltrch RichTextBox}\li0\ri0\sa0\sb0\fi0\ql\par}}}"

Problem

The thing is, the actual output of the PDF is precisely that previously shown RTF, but the expected output (for the example) must be:

"This is the **RichTextBox**\r\n"

This is happening obviously because we are inserting the binded RTF from the control as it comes to the PDF, the thing is: How can we add that content and specify its RTF?

PS. If you have other working idea or solution (without using a richtextbox, or something like that) it's welcome. Thanks in advance.

Erre Efe
  • 15,387
  • 10
  • 45
  • 77

2 Answers2

1

Unfortunately, iTextSharp does not directly support RTF format anymore. I would suggest to convert the RTF fragment to XHTML first and then import the resulting XHTML into the final document (it seems that the official HTML support is gone away, so XHTML is the only alternative in this case).

In short, I would suggest to:

  1. convert the RTF fragment into XHTML;
  2. place the XHTML stream into a new iTextSharp document (or directly into the final document, if you wish);
  3. add the content of the aforementioned document into the target document you are going to export as PDF.

UPDATE

There is no built-in mechanism to convert from RTF to XHTML but many open source project exist; I would start coupling this RTF to HTML converter with the HTML Agility Pack (which will in turn convert your HTML to XHTML).

Frankly, however, the whole flow is a bit complex to follow and I would perhaps opt for a simpler solution, maybe by using an HTML editor (alternative) directly in your project or by reverting to the FlowDocument as others have suggested.

Community
  • 1
  • 1
Efran Cobisi
  • 6,138
  • 22
  • 22
  • Sure, I've read about that but the thing is, how can I convert that RTF to XHTML? – Erre Efe Jul 01 '12 at 12:38
  • Finally got it to work using Smith HTML editor (instead of the RichTextBox) and this is, actually, way better than my idea. Thanks. – Erre Efe Jul 04 '12 at 17:19
1

WPF already had a good FlowDocument and it does good rendering. So we created Xaml to PDF converter, its in beta, but most String, Table and Images are converted to PDF successfully, its an open source project available at, http://xamltopdf.codeplex.com/ , RTF can give you FlowDocument and you can convert it to XAML and pass it on to XamlToPDF converter.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • We can't use this. We are using iTextSharp due to its native support for digital signing and code generation features your library lacks of; but thanks. – Erre Efe Jul 01 '12 at 12:27
  • How about using this to create PDF and then use iTextSharp to just sign it? We are going to add signing. What kind of code generation you are talking aout? – Akash Kava Jul 02 '12 at 09:48