Click here to Skip to main content
15,867,983 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My starting point is that I have a Side Panel in my MainWindow, in this Panel are my Buttons with my formatting Comand. which looks like that:

C#
<Button Tag="{DynamicResource Add}" Command="IncreaseFontSize" ToolTip="Größer" Content=" Fett" Style="{StaticResource ButtonStyle}"
                                       Background="Transparent" Foreground="Gray" HorizontalAlignment="Center"  Width="240" />


And beside the Panel I have an Frame in which I open my Pages. These Pages has RichTextBoxes in it. After I made the text Bold I want to print it. But I only get the Text without the formatting.

What I have tried:

With my Print Button click on my MainWindow there comes this Code:

C#
string richText = new TextRange((PagesNavigation.Content as A4Page).rtb0.Document.ContentStart, (PagesNavigation.Content as A4Page).rtb0.Document.ContentEnd).Text;
string richText2 = new TextRange((PagesNavigation.Content as A4Page).rtb1.Document.ContentStart, (PagesNavigation.Content as A4Page).rtb1.Document.ContentEnd).Text;
string richText3 = new TextRange((PagesNavigation.Content as A4Page).rtb2.Document.ContentStart, (PagesNavigation.Content as A4Page).rtb2.Document.ContentEnd).Text;
string richText4 = new TextRange((PagesNavigation.Content as A4Page).rtb3.Document.ContentStart, (PagesNavigation.Content as A4Page).rtb3.Document.ContentEnd).Text;

if (richText != "")
    vm.TextBox1 = richText;
if (richText2 != "")
    vm.TextBox2 = richText2;
if (richText3 != "")
    vm.TextBox3 = richText3;
if (richText4 != "")
    vm.TextBox4 = richText4;

window.ShowDialog();


A4Page is here the name of my Page which I want to Print. After that I open an little Print dialog. I have there an another Print button which looks like that:

C#
A4Page page = new A4Page();
          
            var vm = (ViewModel)this.DataContext;
            if (inputQuantity.Text == "0")
                MessageBox.Show("Der wert darf nicht 0 sein!!");

            else if (vm.selected_printer == null)
                MessageBox.Show("Bitt Geben Sie einen Drucker an!!");
            else if (vm.selected_printer != null && inputQuantity.Text != "0")
                page.print();


And thats opening my Page in which is an Print method where I want to print my Page.

C#
var vm = (ViewModel)this.DataContext;

            vm.printcontrol = true;
            PrintDialog printDlg = new PrintDialog();

            printDlg.PrintQueue = new PrintQueue(new PrintServer(), vm.selected_printer);
            printDlg.PrintTicket.CopyCount = vm.PrintCount;

            PageMediaSize pageSize = new PageMediaSize(PageMediaSizeName.NorthAmericaLetter);


            ///leert die RichTextBox und platziert denn neuen Text
            rtb0.Document.Blocks.Clear();
            rtb0.Document.Blocks.Add(new Paragraph(new Run(vm.TextBox1)));

            rtb1.Document.Blocks.Clear();
            rtb1.Document.Blocks.Add(new Paragraph(new Run(vm.TextBox2)));

            rtb2.Document.Blocks.Clear();
            rtb2.Document.Blocks.Add(new Paragraph(new Run(vm.TextBox3)));

            rtb3.Document.Blocks.Clear();
            rtb3.Document.Blocks.Add(new Paragraph(new Run(vm.TextBox4)));
            printDlg.PrintTicket.PageMediaSize = pageSize;///Setzt die Richtige Größe
            printDlg.PrintTicket.PageOrientation = PageOrientation.Portrait;///Setzt das Document auf Vertical Drucken

            printDlg.PrintVisual(previewprint, "Border Printing.");
            vm.printcontrol = false;



So the Text in the Preview where the User can type in Data would look like that:

Test

but the text would look like this in the Print output:

Test

without any Formatting
Posted
Updated 8-Aug-22 6:49am

1 solution

You're just extracting raw text; the most obvious route (though not the most elegant), is to save the contents of the RTB to an RTF file and print that.

RichTextBox.SaveFile Method (System.Windows.Forms) | Microsoft Docs[^]

RichTextBoxStreamType Enum (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900