Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi:

I'm building a series of apps for my place of business so I'm trying to create my own printing class that I can refer to for all of my applications.

The issue is, I'm trying to figure out a way for the application to tell the class when to print the page, and I am unable to find a way to do so.

For example, this is what I have so far:
C#
// Report Variables
private bool bPrinting = false;
private int iPage = 0;
private float fOverflow = 0.00F;
private string sPrintLine = null;
private Font fontTmpFont = null;
private PrintPageEventArgs ppeaEv = null;
private Margins mMargins = new System.Drawing.Printing.Margins(25, 25, 25, 25); // Set wide margins

// Clear the print line
public void LineClear()
{
    sPrintLine = null;
}

// Insert a string into the print line at the specified position within the line
public void LineInsert(string _InsertString, int _InsertPosition)
{
    if (sPrintLine.Length <= _InsertPosition)
        sPrintLine = sPrintLine.PadLeft(_InsertPosition) + _InsertString;
    else if (sPrintLine.Length <= (_InsertPosition + _InsertString.Length))
        sPrintLine = sPrintLine.Substring(0, _InsertPosition) + _InsertString;
    else
        sPrintLine = sPrintLine.Substring(0, _InsertPosition) + _InsertString + sPrintLine.Substring(_InsertPosition + _InsertString.Length);
}

// Check to see if the line we're trying to print is at the end of the page
public bool AtEndOfPage()
{
    return AtEndOfPage(new Font("Courier", 10));
}
public bool AtEndOfPage(Font _Font)
{
    if ((fOverflow + _Font.GetHeight(ppeaEv.Graphics)) > ppeaEv.MarginBounds.Height)
        return true;
    else
        return false;
}

// Attempt to print the line, and return 'true' if it is at the end of the page
public void LinePrint()
{
    LinePrint(null, null);
}
public void LinePrint(Font _Font)
{
    LinePrint(_Font, null);
}
public void LinePrint(Font _Font, Brush _Color)
{
    if (_Font == null)
        _Font = new Font("Courier", 10);
    if (_Color == null)
        _Color = Brushes.Black;

    ppeaEv.Graphics.DrawString(sPrintLine, _Font, _Color,
        ppeaEv.MarginBounds.Left, ppeaEv.MarginBounds.Top + fOverflow,
        new StringFormat()); // 'Draw' line on page
    fOverflow += _Font.GetHeight(ppeaEv.Graphics);
}

//  We are done with the report, tell the Print Service to finish up
public void EndReport()
{
    ppeaEv.HasMorePages = false;
}

//  This is what gets called when the user clicks on 'Print'
private void Print_Click(object sender, EventArgs e)
{
    PrintDocument printDocument = new PrintDocument();
    printDocument.DefaultPageSettings.Margins = mMargins;   // Set margins for pages
    printDocument.DefaultPageSettings.Landscape = false;    // Set Portrait mode
    printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
    printDocument.EndPrint += new PrintEventHandler(printDocument_EndPrint);

    PrintDialog printDialog = new PrintDialog();
    printDialog.Document = printDocument;                   // Set the Document for this print dialog
    printDialog.AllowSomePages = true;                      // Allow the user to select only some pages to print
    printDialog.ShowHelp = true;                            // Allow help button

    DialogResult result = printDialog.ShowDialog();
    if (result == DialogResult.OK)
    {
        printDocument.Print(); // Raises PrintPage event
    }

    printDocument.Dispose();
    printDialog.Dispose();
}

void printDocument_BeginPrint(object sender, PrintEventArgs e)
{
    iPage = 0;
    fOverflow = 0.00F;

}

void printDocument_EndPrint(object sender, PrintEventArgs e)
{

}


As you may be able to tell, I'm trying to fill the page externally to the class, and then have the app tell the class when to print the page when "AtEndOfPage"

I know about "printDocument.PrintPage", but that doesn't seem to be what I need; It builds the page internal to the method and then prints it. I'm not going to be building the print page within that method.
I also am trying to allow for multiple fonts on a single page.

Is there a way to do this?

Thank you all in advance,
Robert
Posted

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