Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在运行时在WPF中动态写入和打印FlowDocument_C#_Wpf - Fatal编程技术网

C# 在运行时在WPF中动态写入和打印FlowDocument

C# 在运行时在WPF中动态写入和打印FlowDocument,c#,wpf,C#,Wpf,朋友们好,今天我想给我的妻子做一个小wpf应用。我希望她可以在运行时重新编写并打印她在flowdocument中编写的内容。ı可以在设计时做任何事情,但ı想在这里写ar运行时我的解决方案可能ı必须使用ınotifyProperty changed或flowdocument的绑定文本来打印DLG我该怎么办 /// <summary> /// This method creates a dynamic FlowDocument. You can add anything to this

朋友们好,今天我想给我的妻子做一个小wpf应用。我希望她可以在运行时重新编写并打印她在flowdocument中编写的内容。ı可以在设计时做任何事情,但ı想在这里写ar运行时我的解决方案可能ı必须使用ınotifyProperty changed或flowdocument的绑定文本来打印DLG我该怎么办

/// <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."); 
}
//
///此方法创建一个动态流文档。你可以添加任何内容
///要发送到打印机的流程文档
/// 
/// 
私有FlowDocument CreateFlowDocument()
{
//创建流程文档
FlowDocument文档=新的FlowDocument();
//创建一个节
Section Section=新的Section();
//创建第一段
第p1段=新的第()段;
//创建并添加新的粗体、斜体和下划线
粗体bld=新粗体();
添加(新运行(“第一段”);
Italic italicBld=新的Italic();
斜体显示。内联线。添加(bld);
Underline underlineAlicBLD=新下划线();
下划线alicld.Inlines.Add(斜体);
//在段落中添加粗体、斜体和下划线
p1.Inlines.Add(下划线为BLD);
//在第节中添加段落
第二节块添加(p1);
//将节添加到流程文档
文件块添加(秒);
退货单;
}
私有作废打印(对象发送者、路由目标)
{
//创建打印对话框
PrintDialog printDlg=新建PrintDialog();
//动态创建流程文档。
FlowDocument文档=CreateFlowDocument();
doc.Name=“FlowDoc”;
//从FlowDocument创建IDocumentPaginatorSource
IDocumentPaginatorSource idpSource=doc;
//调用PrintDocument方法将文档发送到打印机
printDlg.PrintDocument(idpSource.DocumentPaginator,“Hello WPF Printing.”);
}

对于所见即所得编辑,WPF提供了一个现成的控件。WPF RichTextBox控件可以本地编辑XAML流文档


我完全忘记了你说过RichTextBox中的FlowDocument在这里是可编辑的,这是你可以打印和保存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); 
        } 

    }