在WPF中的RunTime FlowDocument中动态编写和打印

时间:2012-02-16 18:01:32

标签: c# wpf

你好朋友今天我正在尝试为我的妻子制作小型的wpf应用程序。我希望她能在运行时打字并打印她在flowdocument中写的内容。 ı可以在设计时做everthings但是我想在这里写ar运行时间我的解决方案可能ı必须使用ıınotifyProperty更改或绑定文本的flowdocument printDlg我该怎么办才能帮助你呢

/// <summary>
/// This method creates a dynamic FlowDocument. You can add anything to this
/// FlowDocument that you would like to send to the printer
/// </summary>
/// <returns></returns>
private FlowDocument CreateFlowDocument()
{
// Create a FlowDocument
FlowDocument doc = new FlowDocument();
// Create a Section
Section sec = new Section();
// Create first Paragraph
Paragraph p1 = new Paragraph();
// Create and add a new Bold, Italic and Underline
Bold bld = new Bold();
bld.Inlines.Add(new Run("First Paragraph"));
Italic italicBld = new Italic();
italicBld.Inlines.Add(bld);
Underline underlineItalicBld = new Underline();
underlineItalicBld.Inlines.Add(italicBld);
// Add Bold, Italic, Underline to Paragraph
p1.Inlines.Add(underlineItalicBld);
// Add Paragraph to Section
sec.Blocks.Add(p1);
// Add Section to FlowDocument
doc.Blocks.Add(sec);
return doc;
}









private void print(object sender, RoutedEventArgs e)
{
// Create a PrintDialog
PrintDialog printDlg = new PrintDialog();
// Create a FlowDocument dynamically. 
FlowDocument doc = CreateFlowDocument();
doc.Name = "FlowDoc";
// Create IDocumentPaginatorSource from FlowDocument
IDocumentPaginatorSource idpSource = doc;
// Call PrintDocument method to send document to printer
printDlg.PrintDocument(idpSource.DocumentPaginator, "Hello WPF Printing."); 
}

2 个答案:

答案 0 :(得分:1)

对于WYSIWYG编辑,WPF提供了现成的控件。 WPF RichTextBox控件可以本机编辑XAML流文档。

http://msdn.microsoft.com/en-us/magazine/cc163371.aspx

答案 1 :(得分:1)

我完全忘记了你说RichTextBox中的FlowDocument在这里可以编辑ıFınd你可以打印的最佳解决方案并保存pdf,xps.format

/ <强> * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** /

// Handle "Save RichTextBox Content" button click.
private void SaveRTBContent(Object sender, RoutedEventArgs args)
        {

        // Clone the source document's content into a new FlowDocument. 
        // This is because the pagination for the printer needs to be 
        // done differently than the pagination for the displayed page. 
        // We print the copy, rather that the original FlowDocument. 
        MemoryStream s = new MemoryStream(); 
        TextRange source = new TextRange(document.ContentStart, document.ContentEnd); 
        source.Save(s, DataFormats.Xaml); 
        FlowDocument copy = new FlowDocument(); 
        TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd); 
        dest.Load(s, DataFormats.Xaml); 

        // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, 
        // and allowing the user to select a printer. 

        // get information about the dimensions of the seleted printer+media. 
        PrintDocumentImageableArea ia = null; 
        System.Windows.Xps.XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); 

        if (docWriter != null && ia != null) 
        { 
            DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator; 

            // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. 
            paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); 
            Thickness t = new Thickness(72);  // copy.PagePadding; 
            copy.PagePadding = new Thickness( 
                             Math.Max(ia.OriginWidth, t.Left), 
                               Math.Max(ia.OriginHeight, t.Top), 
                               Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), 
                               Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); 

            copy.ColumnWidth = double.PositiveInfinity; 
            //copy.PageWidth = 528; // allow the page to be the natural with of the output device 

            // Send content to the printer. 
            docWriter.Write(paginator); 
        } 

    }