How to convert the RTF file to the PDF file?

How to convert the RTF file to a PDF file? I have an Adobe PDF printer, should I use it? If so, how can I programmatically access it?

+3
source share
6 answers

In fact, none of them are terribly reliable or do what I want. The solution is simple, install Adobe Acrobat and just run the RTF file using the Process class.

I also found a more reasonable approach. I save the file as RTF, open it in a word and save it as a PDF (Word Print, since the PDF plugin must be installed)

SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Personal Document File (*.pdf)|*.pdf"; if (sfd.ShowDialog() == DialogResult.OK) { String filename = Path.GetTempFileName() + ".rtf"; using (StreamWriter sw = new StreamWriter(filename)) { sw.Write(previous); } Object oMissing = System.Reflection.Missing.Value; //null for VB Object oTrue = true; Object oFalse = false; Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document(); oWord.Visible = false; Object rtfFile = filename; Object saveLoc = sfd.FileName; Object wdFormatPDF = 17; //WdSaveFormat Enumeration oWordDoc = oWord.Documents.Add(ref rtfFile, ref oMissing, ref oMissing, ref oMissing); oWordDoc.SaveAs(ref saveLoc, ref wdFormatPDF, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing); oWord.Quit(ref oFalse, ref oMissing, ref oMissing); //Get the MD5 hash and save it with it FileStream file = new FileStream(sfd.FileName, FileMode.Open); MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); using (StreamWriter sw = new StreamWriter(sfd.FileName + ".md5")) { sw.WriteLine(sfd.FileName + " - " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + " md5: " + BinaryToHexConverter.To64CharChunks(retVal)[0]); } } 
-1
source

You can use a PDF printer, but you still have a few problems to solve.

To process text spanning multiple pages, you need this article to create a RichTextbox descendant that processes the EM_FORMATRANGE message.

There is a lot of (free) PDF printer, but I found that only BioPdf will allow you to control the output file name. They also have reasonable rates for licensed versions.

I used this to create complex reports (combinations of several RTF segments and custom graphics) as email attachments.

+2
source

You can use the doPdf virtual print driver http://www.dopdf.com/ if allowed on the production machine. This converts more or less any type of file to pdf format, not just rtf. It just appears as another printer in the print manager after installation.

To use it in winforms code, I adapted the code found in the msdn print example http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

 private void button1_Click(object sender, EventArgs e) { try { streamToPrint = new System.IO.StreamReader (@"F:\temp\labTest.txt"); try { printFont = new Font("Arial", 10); PrintDocument pd = new PrintDocument(); pd.PrinterSettings.PrinterName = "doPDF v6";//<-------added pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage); pd.Print(); } finally { streamToPrint.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 

The only part of the code that I needed to add was noted above, for example. pd.PrinterSettings.PrinterName = "doPDF v6";

There might be a printer enumeration method that would be more elegant and reliable, and against this one could check if the print driver exists, possibly in setting up the configuration file.

Update: This method processes several pages: this.pd_PrintPage according to the msdn sample. PrintDocument supports printing and printing pages. DoPdf will automatically open the fileSaveAsDialog field so that files can be saved as a PDF document.

How about rtf though ? The Microsoft format is not supported very well, so it would seem. This demo article http://msdn.microsoft.com/en-us/library/ms996492.aspx uses RichTextBox as a starting point and uses P / Invoke to use Win32 power to print RTF as WYSIWG. The control defines its own page length method, replacing the one used above in the code snippet, and still uses PrintDocument, so it should be easy to use. You can assign any rtf using the Rtb.rtf method.

+1
source

See this article . It looks like you can use it without any changes. It uses open office.

+1
source

An RTF document must be read and interpreted by some application that can understand this format. You will need to programmatically launch this application, download the RTF file and send it to the PDF printer. Word will be good for this, since it has a good .NET interface. Overview of steps:

 ApplicationClass word = new ApplicationClass(); Document doc = word.Documents.Open(ref filename, ...); doc.PrintOut(...); 

You will need to use the Microsoft.Office.Interop.Word namespace and add the link to the Microsoft.Office.Interop.Word.dll assembly.

0
source

ITextSharp should do the trick for you.

0
source

All Articles