I need to print a RTF to PDF in C# WPF.
My code currently uses FlowDocument because it's currently the best system I've found for printing an RTF.
I succeeded, but my ultimate goal is that the program can create the PDF without requiring the operator to enter the file name. It should be inserted automatically by the program.
// load RTF to FlowDocument
FlowDocument flowDocument = new FlowDocument();
TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
using (FileStream fileStream = File.Open(rtfTempName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
textRange.Load(fileStream, DataFormats.Rtf);
}
// Assign Microsoft Printer for PDF output
PrintDocument pdoc = new PrintDocument();
var printers = new LocalPrintServer().GetPrintQueues();
var selectedPrinter = printers.FirstOrDefault(p => p.Name == "Microsoft Print to PDF");
if (selectedPrinter == null)
{
MessageBox.Show("Printer not found!");
return;
}
// setting selected Microsoft PDF printer
printDialog.PrintQueue = selectedPrinter;
printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
// ??? Is it possible to setting something like "filename=..." ???
// ??? and "PrintToFile = true" ???
// Print document
printDialog.PrintDocument(paginator, "Label");