2012-02-16 3 views
0

привет Друзья Сегодня я пытаюсь сделать небольшое приложение wpf для моей жены. Я хочу, чтобы она вошла во время выполнения и распечатала то, что она написала в flowdocument. Я могу сделать everthings во время разработки, но я хочу написать ар запустить время здесь мое решение, вероятно, я должен использовать ı ınotifyProperty изменен или связывание текста FlowDocument к PrintDlg Что делать могу вы HELLP пожалуйстаДинамическая запись и печать в RunTime FlowDocument в WPF

/// <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."); 
} 

ответ

1

Я забыл суммарно, как вы сказали FlowDocument в RichTextBox редактируемые здесь ı найти лучшее решение, которое вы можете распечатать и сохранить 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); 
     } 

    } 
Смежные вопросы