Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:15:57+00:00 2026-05-24T11:15:57+00:00

The following code is what I’m trying to use to print data to the

  • 0

The following code is what I’m trying to use to print data to the printer. The data is printing, but tabs are not printing. My string that I’m printing looks like this:

string textToPrint = "Member Number\tAddress\tCity\tState";

But when it prints, it looks like this Member NumberAddressCityState.

I found this printer class on the internet, and it seems to work (characters do print out on the report) except for printing tabs. How can I make sure that tabs print?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;

namespace HighLowReport
{
    public class PCPrint : System.Drawing.Printing.PrintDocument
    {
        //Property variable for the font the user wishes to use
        private Font _font;

        //Property variable for the text to be printed
        private string _text;

        //Property to hold the text that is to be printed
        public string TextToPrint
        {
            get { return _text; }
            set { _text = value; }
        }

        //Property to hold the font the user wishes to use
        public Font PrinterFont
        {
            get { return _font; }
            set { _font = value; }
        }

        // Static variable to hold the current character
        // we're currently dealing with.
        static int curChar;

        // Empty constructor
        public PCPrint()
            : base()
        {
            // set the file stream
            // Instantiate out Text property to an empty string
            _text = string.Empty;
        }

        // Constructor to initialize our printer object
        // and the text it's supposed to be printing
        public PCPrint(string str)
            : base()
        {
            // Set the file stream
            // Set our text property value
            _text = str;
        }

        protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
        {
            // Run base code
            base.OnBeginPrint(e);

            // Check to see if the user provided a font
            // if they didn't the we default to Times New Roman
            if (_font == null)
            {
                _font = new Font("Times New Roman", 10);
            }
        }

        // Override the default OnPrintPage method of the PrintDocument
        protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
        {
            // Run base code
            base.OnPrintPage(e);

            // declare local variables needed
            int printHeight;
            int printWidth;
            int leftMargin;
            int rightMargin;
            Int32 lines;
            Int32 chars;

            // Set print area size and margins
            {                
                printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
                printWidth = base.DefaultPageSettings.PaperSize.Width = base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
                leftMargin = base.DefaultPageSettings.Margins.Left; //X
                rightMargin = base.DefaultPageSettings.Margins.Right; //Y
            }

            // Check if the user selected to print in Landscape mode
            // if they did then we need to swap height/width parameters
            if (base.DefaultPageSettings.Landscape)
            {
                int tmp;
                tmp = printHeight;
                printHeight = printWidth;
                printWidth = tmp;
            }

            // Now we need to determine the total number of lines
            // we're going to be printing
            Int32 numLines = (int)printHeight / PrinterFont.Height;

            // Create a rectangle printing area for our document
            RectangleF printArea = new RectangleF(leftMargin, rightMargin, printWidth, printHeight);

            // Use StringFormat class for the text layout of our document
            StringFormat format = new StringFormat(StringFormatFlags.LineLimit);

            // Fit as many characters as we can into the print area

            e.Graphics.MeasureString(_text.Substring(RemoveZeros(ref curChar)), PrinterFont, new SizeF(printWidth, printHeight), format, out chars, out lines);

            // Print the page
            e.Graphics.DrawString(_text.Substring(RemoveZeros(ref curChar)), PrinterFont, Brushes.Black, printArea, format);

            // Increase current char count
            curChar += chars;

            // Determine if there is more text to print, if
            // there is then tell the printer there is more coming
            if (curChar < _text.Length)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                curChar = 0;
            }
        }

        // Function to replace any zeros in the size to a 1
        // Zeros will mess up the printing area
        public int RemoveZeros(ref int value)
        {
            // Check the value passed into the function
            // If the value is a 0 (zero) then return a 1,
            // otherwise return the value passed in
            while (_text[value] == '\0')
            {
                value++;
            }
            return value;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:15:57+00:00Added an answer on May 24, 2026 at 11:15 am

    For your format StringFormat variable, you have to add an array of tab stops:

    StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
    float[] formatTabs = { 10.0f, 20.0f };
    format.SetTabStops(0.0f, formatTabs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code works great in IE, but not in FF or Safari. I
Following code does NOT work, but it expresses well what I wish to do.
Following code does compile whereas name aNumber is not declared before use. class A
The following code works fine in FF and Chrome, but not in IE8. I
Following code: <?php $test_array = array(); $test_array['string_index'] = data in string index; $test_array[] =
Following code I can use for storing the value of UILabel into a string.
Following code does compile in gcc 4.5 but it is not being compiled in
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
Following code will compile but crash at run time: int main() { try {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.