Merging rtf documents in C sharp - c#

I am using NRtfTree to merge two rtf documents. The merged document does not retain the same look as the template and it looks to be a problem to do with encoding. How do I set the encoding for NRtfTree. Or is there any other component I can use to merge two documents without using Office automation.
Thanks

Consider using .Net RichTextBox and Class RichTextBox.
Create two System.Windows.Forms.RichTextBox objects set the value from the first and then the second rtf file. Then create another RichTextBox with the two RTF documents put together.
System.Windows.Forms.RichTextBox.Rtf = fromNrOne + fromNrTwo;

Related

MS Office Automation transfer table from excel to word using PublishObjects.Add

For a project I'm working on, we need to copy data from Excel sheets into new tables within a Word document and have a strategy that works... in most cases.
First, we do
string file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() +
".mht");
object sheetObj = sheetName;
object trueObj = true;
Excel.PublishObject obj = workbook.PublishObjects.Add(Excel.XlSourceType.xlSourceSheet, file,
sheetObj);
obj.Publish(trueObj);
then
Document tempDocument = wordApp.Documents.Open(file);
and read templDocument into the targe Word doc.
...
In a couple of cases, we're seeing problems. (The problems are illustrated in the example files at http://thinkscience.us/office/examples.zip)
1) the big text files show text truncation between Excel and the exported .mht
2) the 'nutritional' files show the addition of several lines of white space between the Excel data and the .mht.
I've tried several variations on the parameters to PublishObjects.Add (using a range rather than an entire sheet). The add method includes an optional XlHtmlType parameter that only works with value XlHtmlType.xlHtmlStatic.
Has anyone used PublishObjects.Add or another strategy to transfer sheets from Excel to Word, preserving as much formatting as possible and not interfering with the system clipboard?
I found an article which might meet your needs, but it's not using Office automation, it's using a library called Spire.Office. Check: How to Maintain Formatting of Cells when Copying Cells from Excel to Word.

Add header and footer to an RTF file using c#

We have an MVC app that outputs RTF files based on templates (which themselves are RTF files).
The code that my colleague wrote uses System.Windows.Forms.RichTextBox to convert text to RTF file (to be more excat it uses the Rtf property of RichTextBox). I was thinking of adding headers and footers to the template RTF files, but RichTextBox appears to remove those. Additionally some of the documents that we generate are composed of multiple templates (more often than not, a single template does not equal a single page and one template can be injected in the middle of another), so thats one more reason why including headers and footers in the templates would not work.
Is there any way to add headers and footer in C# to RTF documents created in the way described above?
I tried fishing something on the subject from the internet, but I wasn't able to find anything concrete.
I was searching for a library that could possibly solve my problem and I came across this one:
.NET RTF Writer Library in C#
The library itself doesn't exactly solve my problem on it's own, but the documents generated by it are easy to read and without all the crap Word would put into them. The demo for this library generates a document that has a header and a footer. The code of those two looks more or less like this:
{\header
{\pard\fi0\qd
This is a header
\par}
}
{\footer
{\pard\fi0\qc
{\fs30
This is a footer
}\par}
}
I still need to figure out how to apply correct formating here, but that should be relatively easy to find. So, I can solve my initial problem by injecting the code above to the RTF code generated by RichTextBox. I'm not sure if the position of those two tags matters, but I guess I will find that out soon enough...
Here is the code that I use to inject the header and footer:
public string AddHeaderAndFooter(string rtf)
{
// Open file that stores header and footer
string headerCode = System.IO.File.ReadAllText(Server.MapPath("~/DocTemplates/header.txt"));
// Inject header and footer code before the last "}" character
return rtf.Insert(rtf.LastIndexOf('}') - 1, headerCode);
}
Note I have the header and footer in a static txt file, because it actually contains images in RTF readable format and that would be too big to put in the code. I haven't noticed any problems related to the fact that header and footer are defined at the end of the RTF file.

Selecting text by font in Word

Is there a way of extracting all lines that are using a particular font (size, is it bolded, font name, etc) in word via C#?
In addition, is there a way to find out what is the font for some text that is in the document?
My hunch is that there are functions in the Microsoft.Office.Interop.Word namespace that can do this, but I cannot seem to find them.
Edit: I am using word 2010.
You can loop through the document using the Find object from Word Interop. You can set the Find.Font.Name property for a Selection or Range from your document. Note that the Font interface has several Name* properties for various encodings.
EDIT
Here's the equivalent VBA code:
Dim selectionRange As Range
Set selectionRange = Application.ActiveDocument.Range
With selectionRange.Find
.ClearFormatting
.Format = True
.Font.NameBi = "Narkisim" //for doc without bidirectional script, use Name
Do While .Execute
MsgBox selectionRange.Text
Loop
End With
The object model from Word Interop is the same, see the link above.
Don't go asking me for C# code now... this is SO, we don't do silver platters. And if you're ever going to do serious work with the Office Interop API, you will need to be able to read VBA code.

Rtf to WordML Convert in C#

I have a windows application to generate report.
It has templates in RTF as "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}\r\n\\viewkind4\\uc1\\pard\\fs20\\tab\\tab\\tab\\tab af\\par\r\n}\r\n", which is written to word doc file. then the word is Saved-As XML and close. Then, tags like (say) are extracted and some new
The problem here is Word, which is used as converter in the process and it consumes valuable time in Loop, where it opens word instance, save, close, delete.
Please correct any mistake if i have made and help me with an alternative to convert to WordML .
Use Aspose .Words
//your rtf string
string rtfStrx = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}\r\n\\viewkind4\\uc1\\pard\\fs20\\tab\\tab\\tab\\tab af\\par\r\n}\r\n"
//convert string to bytes for memory stream
byte[] rtfBytex = Encoding.UTF8.GetBytes(rtfStrx);
MemoryStream rtfStreamx = new MemoryStream(rtfBytex);
Document rtfDocx = new Document(rtfStreamx);
rtfDocx.Save(#"C:\Temp.xml", SaveFormat.WordML);
This saves your RTF text in new document as WordML. I cannot say about time it will take in loop. But it will surely have much less time then MS Word being physically opened and closed.
Unless I am missing something, I assume that you are trying to create Office XML file from RTF template? I think you can use Open XML SDK for creation of the xml file. Specifically, DocumentReflector that comes with that SDK seems to a good fit for that. See this example. Also, there is a http://www.codeguru.com/cpp/controls/richedit/conversions/article.php/c5377/ which shows how to convert from RTF to HTML that might guide you.
use wpf richtextbox. Rtf => xaml. Since xaml is xml_ use xslt or linq to convert it to your desired xml structure

iText - how to do search/replace on existing RTF document

Currently I'm working on a simple Mail-Merge module.
I need to load plain *.RTF template, then replace all words enclosed in [[field]] tags and at the end and print them out.
I found the iText library which is free and capable of loading/saving pdfs and rtf.
I managed to load rtf, merge a few copies to one huge doc but I have no idea how to replace [[field]] by custom data like customer name/address.
Is that feature present, and if yes - how to do it?
The solution platform is c#/.NET
I don't think that pdf is the way you want to go.
According to this article it is extremely difficult at best and not possible at worst.
Would something like RTFLib work better for you?
G-Man
Finally I decided to use *.docx and "Open XML SDK 2.0 for Microsoft Office" .NET strongly typed wrapper.
You can use RichTextBox control to find/replace placeholders.
RichTextBox rtb = new RichTextBox();
rtb.LoadFile("template.rtf");
string placeHolder = "[[placeholder_name]]";
int pos = rtb.Find(placeHolder);
rtb.Select(pos, placeHolder.Length);
rtb.SelectedText = "new value";
After this you can get rtf formatted text with:
rtb.Rtf;

Categories