Search code examples
c#.netpdfdocumentation-generation

How can I convert an RTF file to a pdf file?


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


Solution

  • Actually, none of these are terribly reliable or do what I want. The solution is simple, install Adobe Acrobat and just have it open the RTF file using the Process class.

    I also found a more reasonable approach. I save the file as an RTF, the open it in word, and save it as PDF (Word's Print As 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]);
                    }
                }