Automatically Word-Wrapping Text To A Print Page?

15,198

Solution 1

Yes there is the DrawString has ability to word wrap the Text automatically. You can use MeasureString method to check wheather the Specified string can completely Drawn on the Page or Not and how much space will be required.

There is also a TextRenderer Class specially for this purpose.

Here is an Example:

         Graphics gf = e.Graphics;
         SizeF sf = gf.MeasureString("shdadj asdhkj shad adas dash asdl asasdassa", 
                         new Font(new FontFamily("Arial"), 10F), 60);
         gf.DrawString("shdadj asdhkj shad adas dash asdl asasdassa", 
                         new Font(new FontFamily("Arial"), 10F), Brushes.Black,
                         new RectangleF(new PointF(4.0F,4.0F),sf), 
                         StringFormat.GenericTypographic);

Here i have specified maximum of 60 Pixels as width then measure string will give me Size that will be required to Draw this string. Now if you already have a Size then you can compare with returned Size to see if it will be Drawn properly or Truncated

Solution 2

I found this : How to: Print a Multi-Page Text File in Windows Forms

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    int charactersOnPage = 0;
    int linesPerPage = 0;

    // Sets the value of charactersOnPage to the number of characters 
    // of stringToPrint that will fit within the bounds of the page.
    e.Graphics.MeasureString(stringToPrint, this.Font,
        e.MarginBounds.Size, StringFormat.GenericTypographic,
        out charactersOnPage, out linesPerPage);

    // Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
        e.MarginBounds, StringFormat.GenericTypographic);

    // Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage);

    // Check to see if more pages are to be printed.
    e.HasMorePages = (stringToPrint.Length > 0);
}
Share:
15,198
sooprise
Author by

sooprise

I like to program, but am a noob, which is why I'm here. <-My sexy fatbooth picture

Updated on June 04, 2022

Comments

  • sooprise
    sooprise about 2 years

    I have some code that prints a string, but if the string is say: "Blah blah blah"... and there are no line breaks, the text occupies a single line. I would like to be able to shape the string so it word wraps to the dimensions of the paper.

    private void PrintIt(){
        PrintDocument document = new PrintDocument();
        document.PrintPage += (sender, e) => Document_PrintText(e, inputString);
        document.Print();
    }
    
    static private void Document_PrintText(PrintPageEventArgs e, string inputString) {
        e.Graphics.DrawString(inputString, new Font("Courier New", 12), Brushes.Black, 0, 0);
    }
    

    I suppose I could figure out the length of a character, and wrap the text manually, but if there is a built in way to do this, I'd rather do that. Thanks!

  • sooprise
    sooprise over 13 years
    Is there a free and easy to use package I can integrate into my printing class for PDF support? I'm having similar difficulties with printing HTML documents.
  • John Alexiou
    John Alexiou over 13 years
    Can you link and example or reference page?
  • Jonathan
    Jonathan over 13 years
    Used a couple a while back but generally a bit of plumbing, installing distiller on server or actually trying to generate the format GOOGLE. I am using reporting services to setup the report and then under the hood calling it from my webapp with the PDF export setting. The user of the system has no idea that its calling reporting services and it works like a charm, PDF pops up and user prints it off! HTML was never designed with the intention of printing. Its a pain in the ass dont even offer it to users.
  • sooprise
    sooprise over 13 years
    I explored the DrawString method and couldn't figure out how to do the automatic word wrap. Can you please post some sample code?
  • Shekhar_Pro
    Shekhar_Pro over 13 years
    it does that by default instead you need to specify NOWRAP in StringFormat parameter if you dont want to wordwrap the contents
  • sooprise
    sooprise over 13 years
    How do you handle when the text is too long for one page?
  • Jitendra Pancholi
    Jitendra Pancholi over 11 years
    Its not wrapping the string.