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.
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:
convert the RTF fragment into XHTML;
place the XHTML stream into a new iTextSharp document (or directly into the final document, if you wish);
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.
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.
Related
I have data in SQL that is in RTF as it contains a lot of superscript characters. I am trying to print the data on a PDF using PDFsharp (not MigraDoc) using DrawString, however, as I expected, it just shows the RTF string...
I tried putting it in a RichTextBox and then retrieving the Text property, this gives the correct plain text but not in superscript format, which I need.
Can anyone tell me how to correctly output the RTF data?
First from FAQ of pdfsharp:
Can I use PDFsharp to convert HTML or RTF to PDF?
No, not "out of the
box", and we do not plan to write such a converter in the near future.
Yes, PDFsharp with some extra code can do it. But we do not supply
that extra code. On NuGet and other sources you can find a third party
library "HTML Renderer for PDF using PdfSharp" that converts HTML to
PDF. And there may be other libraries for the same or similar
purposes, too. Maybe they work for you, maybe they get you started.
A workaround I think is using DrawToBitmap with a RichTextBox to render the RTF string into an image, then use DrawImage to put it in the pdf file.
Using a RichTextBox would also be my approach, but I would select only a single character in the text and query the relevant properties (subscript, superscript, maybe also bold, italic, underline, and anything else you need). And when any of those properties changes, draw the text you collected so far and continue collecting characters for the new set of properties until any relevant property changes.
I would probably use MigraDoc so I would not have to deal with line-breaks in my code, but that is up to you. I would not create bitmaps for the text as this voids the advantages of the PDF format.
I'm running into a problem that I can't seem to figure out. The scenario is:
Download a bunch of text data as an HTML string
Use an HTML-to-Xaml converter to parse the HTML string and create a List<Block> (Windows.UI.Xaml.Documents.Block).
Display the collection of Blocks as a RichTextBlock control that's available in WinRT.
All of that works well. The problem is, I want to export this data as either a doc/docx or pdf file. Is there a way to export either the RichTextBlock directly or add these Blocks to a RichEditBox or even use the original HTML string to export to the file?
I looked at the Syncfusion Essential Studio, that offers the RichTextBoxAdv control, that's used as:
xmlns:rtba="using:Syncfusion.UI.Xaml.RichTextBoxAdv">
<Grid>
<rtba:SfRichTextBoxAdv>
</rtba:SfRichTextBoxAdv>
</Grid>
But so far as I can tell, there is no way to import Block data or use an HTML string. The latter used to exist in WPF but it's not there anymore. Any suggestions?
Syncfusion SfRichTextBoxAdv control for WinRT supports importing and exporting HTML document, Word document, Rich Text Format document and Text document. With the help of this functionality, you can easily achieve your requirement by using Syncfusion SfRichTextBoxAdv control. Please do find the sample from below link.
Sample link:
http://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample-2055612463
Kindly go through the online KB from following link, to know more information about binding HTML string to SfRichTextBoxAdv control.
https://www.syncfusion.com/kb/5415/how-to-bind-html-text-in-sfrichtextboxadv-control
Regards,
Arumuga Perumal S.
In Google Chrome, when you open an xml file, you get a formatted (pretty) view of the xml if there is no stylesheet referenced in the xml file itself.
I simply want to do this in my application, which uses Awesomium.
I am using the Awesomium.Windows.Forms.WebControl
I don't want to roll my own if I can avoid it.
Thanks!
I'm doing this in an internal tool for my development team. I format the XML with an xsl that colors and indents everything, then update the web control with the resulting HTML.
Check out this link for formatting XML, the CSS styles are built in, so you can update styles colors as you wish
See the "XML to HTML Verbatim Formatter with Syntax Highlighting" project on this page.
http://www2.informatik.hu-berlin.de/~obecker/XSLT/
I have markdown, how i can display formatted text in win 8 apps?
Things i have tried, that may or may not be useful to any answer
I tried converting markdown to rtf and show result in RichTextBlock, but I have problem with markdown to rtf converting.
Hmmm. Trickier than I suspected...
I'd suggest using markdownsharp to derive html from your markdown and to display that in an html view. (of which I'm sure "Windows 8 apps" have at least one)
EDIT
You can then either show this in a WebView or follow this article to convert to rtf.
I wrote this Markdown parser in C#, which exports to HTML and XAML. If you generate XAML, you can access it by using the XamlReader class and display it in a WPF application. (You can also display the HTML version if you choose to.)
https://github.com/PeterWaher/IoTGateway/tree/master/Content/Waher.Content.Markdown
It's part of a larger project:
https://github.com/PeterWaher/IoTGateway/
I creating a C# application that has to create a word document.
I'm using the Microsoft.Office.Interop.Word to do this and I've successfully managed to output some word documents, but creating the content trough the code is a very time consuming work.
I noted that word is able to open html pages and show it as a normal content so I created a simple test table in html and inserted it into the word document. But when I outputted the document the obvious happened: The tags where still there! Word did not format the tags as html. It just outputted exactly what I put in there.
How can I tell word to reformat the text as html?
edit: (trough the C# code of course)
edit 2: Please note that I'm parsing trough some data to make this, so I will end up with about 4 pages of the same table/html, so I will need to be able to tell word to start at the next page each time I've finished a loop. So a html-only method will probably not work.
If you're only wanting to output simple HTML content as a Word document, you could always cheat and write out the HTML content with a .doc extension.
Word will open that just fine.
If you need to add a page break, you can use a CSS page-break-before, like so:
<br style="page-break-before: always;"/>
If you're set on using Interop, having read up a little bit, this post states that you need a converter to insert HTML, and the converters are only accessible when:
you paste HTML from the Clipboard
open/insert HTML from a file
So, this answer looks like it provides a clipboard-based solution : Adding html text to Word using Interop
However, if there's any money to spend on the project, I can heartily recommend Aspose.Words which will do all of this for you.
As requested by the OP, and to make easier for others to find this solution, here it goes the answer I posted as a comment (plus extra results from testing):
When opening an HTML file, MS Word honors the CSS properties page-break-before and page-break-after. There is a caveat, however:
On "Web design" view, page-breaks are never shown (this doesn't mean that they aren't there), just like browsers don't "show" them. And Word opens html files on Web design view by default (which quite makes sense). You need to print the document or switch to some other view (typicall "Print design") to see your breaks in all their glory.
So, saving an HTML file with a .doc extension is a viable solution (also tested: Word opens it properly despite of the extension).
Note: all the testing was done on MS Word 2003 using this snippet: <html>asdf<br style="page-break-before: always;">new page!</html>
Don't build the document in code, create it in Word as template or mail merge template and the use code to merge or replace the fields data.
See this answer here
MS Word Office Automation - Filling Text Form Fields And Check Box Form Fields And Mail Merge
And See this from the mothership:
http://msdn.microsoft.com/en-us/library/ff433638.aspx
If you don't want to use an external lib, Interop is too slow for you and neither pure HTML nor mail merge template are flexible enough, you could write your content as text or HTML into one or more files (using C#), create a VBA macro in a Word document which by itself creates a second Word document, reads the content files and does any formatting you want afterwards.
You can run this macro programmatically by starting Word using the command line switch /m.
Another possible approach, if your html is xhtml (i.e. XML compliant), you could use XSLT to convert it to a Word XML format. But this would take a LOOOOOOOOOOONG time to code.
If you don't have to use HTML as the starting point you could simply build the Word XML document yourself rather than using XSLT, which would be easier. Time consuming but possible - it's something I do quite a lot in my work.
If a third party component is an option I would recommend the stuff from Aspose.
I have been pretty happy with their tools so far. The API is a little messy but everything works as one would expect.