Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Using C#, XAML, WPF, but NOT! Windows Forms, is there a simple
code example of sending a .txt file to a printer?


What I have tried:

Every example I could find used Windows Forms.
Updated 7-May-24 14:57pm
Dave Kreskowiak 7-May-24 20:58pm    
NEVER post your real email address in a public forum. All you're doing is attracting spam by doing that.

Share this answer
A simple way to do this is to create a FlowDocument and send that to the printer. The following code sample should give you idea about how easy it is to do this:
public void PrintText(string text)
  PrintDialog printDialog = new PrintDialog();
  IDocumentPaginatorSource documentPaginator = CreateDocument(text, "SampleTextDocument");
  printDialog.PrintDocument(documentPaginator, "Print sample document");

private FlowDocument CreateDocument(string text, string documentName)
  FlowDocument flowDocument = new FlowDocument() { Name = documentName };
  Section section = new Section();
  Paragraph paragraph = new Paragraph();
  paragraph.Inlines.Add(new Run(text));
  return flowDocument;
I leave the part about reading the text file and wiring this to your front end up to you. As you can see, this is a trivial amount of code to write.
Share this answer
I cannot thank you enough for your help.
Using Visual Studio 2022 your code would not build due to type conversion errors.
After some fiddling I got a working version which I include below.
For some strange reason, when I pasted to this solution box, some lines were red underlined.
Tony (email removed)

using System.Windows.Controls;
using System.Windows.Documents;
using System.IO;
using System.Windows;

namespace Streaming
    internal class PrintFactory
        public string textfilename;
        public string textitself;
        public string texttitle;

        public PrintFactory(string textfilename, string texttitle) 
            this.textfilename = textfilename;
            this.textitself = File.ReadAllText(textfilename);
            this.texttitle = texttitle;

        public void PrintText()
            PrintDialog printDialog = new PrintDialog();
            if ((bool)printDialog.ShowDialog().GetValueOrDefault())
                FlowDocument flowDocument = new FlowDocument();
                foreach (string line in textitself.Split('\n'))
                    Paragraph myParagraph = new Paragraph();
                    // Without next line, initial character is not printed
                    myParagraph.Margin = new Thickness(5);
                    myParagraph.Inlines.Add(new Run(line));
                DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
                printDialog.PrintDocument(paginator, texttitle );


Share this answer
Pete O'Hanlon 10-May-24 2:57am    
I'm glad you got this working. Nicely done Anthony.
CHill60 10-May-24 4:00am    
Tony - I've removed your email from your solution in the same way Dave removed it from your Question. This is an open site, often trolled by bots. A plain email is likely to be targetted by spam.

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

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