A Customizable Printing Text Class

CK1820 
Created at Aug 27, 2007 23:04:34 
40   0   0   0  
At least once,in the life of a programmer,it succeeds that the moment arrives to
print a text document. The task is quite simple for a .NET programmer,but even
more with PrintTextDocument class. This class was written to help in printing
text documents,adding customizable header and footer and line numbers to the
printed text. The text can be printed wrapping the exceeding text to the next
line,truncating it,or spanning it through many pages. The last mode is useful
when printing text banners like the ones coming from ASCII Art or from text
tables with many columns. PrintTextDocumen is derived from
System.Drawing.Printing.PrintDocument and provides some extra properties that
set the class behavior during printing.

//
// Simple use of PrintTextDocument class
//
// assume filename a string that contains the full path of
// an ASCII file.
//

PrintTextDocument toPrint = new PrintTextDocument(filename);
toPrint.PrintMode = PrintTextMode.WrapText;
toPrint.PrintLineNumbers = true;

toPrint.Print();

Points of interest

When I wrote the code,the first version was quite simple. The OnPrintPage override was made of a loop that scanned through the text file and called the MeasureString and DrawText methods. After some trials,I noticed that there was a bug in the text wrapping. It was as if MeasureString did not take care of the spaces at the end of the line. This was because of the way I wrote the GetLineBreakCharPosition function. This function checks the result of MeasureString and makes the adjustment according to the number of spaces that came at the end of the measured text. I don’t know if this is a bug of MeasureString. I did not find any useful information regarding this strange behavior. I even tried to use StringFormat with the MeasureString method but it did not get better. I’d like to know if there is another way to do what GetLineBreakCharPosition does.

Collapse

private int GetLineBreakCharPosition(Graphics gfx,string text,RectangleF rectangle)
{
    StringFormat fmt =
      new StringFormat(StringFormatFlags.MeasureTrailingSpaces);

    int charactersOnLine = 0;
    int lines = 0;

    // Sets the value of charactersOnPage to the number of characters
    // of stringToPrint that will fit within the bounds of the rectangle.
    SizeF sizeText = gfx.MeasureString(text,_printFont,rectangle.Size,fmt,out charactersOnLine,out lines);

    string rem = text.Substring(0,charactersOnLine);

    // Fix MeasureString issue with trailing spaces
    if(rem.EndsWith(" ")



Tags: C# C# Print GetLineBreakCharPosition Print PrintLineNumbers PrintTextDocument Substring WrapText Share on Facebook Share on X

◀ PREVIOUS
Simplified .NET Printing In C#
▶ NEXT
Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
  Comments 0
Login for comment
SIMILAR POSTS

Simplified .NET Printing In C# (created at Aug 27, 2007)

Print HTML In C# With Or Without The Web Browser Control And The Print Dialog (created at Aug 27, 2007)

How To Print Text In C# (created at Aug 27, 2007)

Adding Custom Paper Sizes To Named Printers (created at Aug 27, 2007)

C Sharp Lists (created at Aug 27, 2007)

Simple Example Of IF/THEN Using C# (created at Aug 27, 2007)

Simple C# CGI working on Apache (created at Aug 27, 2007)

How to send binary data through C# CGI app? (created at Aug 27, 2007)

Interacting With TinyPic From C# (created at Aug 26, 2007)

Creating A Watched Folder With Assigned Events (created at Aug 26, 2007)

Calling Your Main Thread From A Worker Thread In C# (created at Aug 26, 2007)

HashTable Tutorial In C# (created at Aug 26, 2007)

Stack Tutorial In C# (created at Aug 26, 2007)

Queue Tutorial In C# (created at Aug 26, 2007)

Create PDF Files On the Fly In C Sharp (created at Aug 26, 2007)

How to print character in C# by ASCII code (created at Aug 28, 2007)

String Replace Function For C# (created at Aug 28, 2007)

How to call Visual C/C++ implemented DLL functions in C# - Simple DLLImport (created at Aug 26, 2007)

Send Email Using C# (created at Aug 26, 2007)

How To Read And Write A Cookie (created at Aug 26, 2007)

Using C# With Cookies (created at Aug 26, 2007)

Printing A Page (created at Aug 26, 2007)

Get A Print Dialog Box By Clicking A Button (created at Aug 26, 2007)

How To Show The Print Dialog And Print Text Files (created at Aug 25, 2007)

Extracting the Country from IP Address (created at Sep 25, 2007)

Graphics in C# - Irregular Forms (created at Sep 25, 2007)

Compress and decompress strings in C# (created at Sep 25, 2007)

How to restrict a program to a single instance (created at Sep 25, 2007)

How to exit from a console application (created at Sep 25, 2007)

The best way to enable and disable buttons, menu items and toolbar buttons (created at Sep 25, 2007)

OTHER POSTS IN THE SAME CATEGORY

How to restrict a program to a single instance (created at Sep 25, 2007)

Compress and decompress strings in C# (created at Sep 25, 2007)

Graphics in C# - Irregular Forms (created at Sep 25, 2007)

Extracting the Country from IP Address (created at Sep 25, 2007)

How to run shell command by MFC ? (created at Sep 09, 2007)

Binary Search Sample Code (created at Sep 08, 2007)

Double Linked List based in C++ (created at Sep 08, 2007)

String Replace Function For C# (created at Aug 28, 2007)

How to print character in C# by ASCII code (created at Aug 28, 2007)

MFC based World Wide Web HTTP Server Source Code (created at Aug 28, 2007)

Creating and Using a CAsyncSocket Object to use CAsyncSocket (created at Aug 28, 2007)

UDP Send and Receive Using CAsyncSocket (created at Aug 28, 2007)

Using the shell to receive notification of removable media being inserted or removed (created at Aug 28, 2007)

Adding Custom Paper Sizes To Named Printers (created at Aug 27, 2007)

Print HTML In C# With Or Without The Web Browser Control And The Print Dialog (created at Aug 27, 2007)

Simplified .NET Printing In C# (created at Aug 27, 2007)

How To Print Text In C# (created at Aug 27, 2007)

C Sharp Lists (created at Aug 27, 2007)

Simple Example Of IF/THEN Using C# (created at Aug 27, 2007)

Fast and Good Keyboard/Mouse Test without the message handler (created at Aug 27, 2007)

Simple CGI Programming in C (created at Aug 27, 2007)

C++ CGI Example working on Apache (created at Aug 27, 2007)

Simple C# CGI working on Apache (created at Aug 27, 2007)

How to send binary data through C# CGI app? (created at Aug 27, 2007)

Interacting With TinyPic From C# (created at Aug 26, 2007)

Creating A Watched Folder With Assigned Events (created at Aug 26, 2007)

Calling Your Main Thread From A Worker Thread In C# (created at Aug 26, 2007)

HashTable Tutorial In C# (created at Aug 26, 2007)

Stack Tutorial In C# (created at Aug 26, 2007)

Queue Tutorial In C# (created at Aug 26, 2007)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)