尝试创建一个自定义打印类,应用程序告诉您何时打印页面

时间:2015-02-12 20:21:16

标签: c# printing

我正在为我的营业地点构建一系列应用程序,因此我正在尝试创建自己的打印类,我可以参考所有应用程序。

问题是,我正试图找出一种方法让应用程序告诉班级何时打印页面,我无法找到方法。

例如,这是我到目前为止所做的:

    // 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
    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)
    {

    }

正如您可能会告诉我的那样,我正在尝试在页面外部填充页面,然后让应用程序在“AtEndOfPage”时告诉班级何时打印页面

我知道“printDocument.PrintPage”,但这似乎不是我需要的;它构建方法内部的页面,然后打印它。我不会在该方法中构建打印页面。  我也试图在一个页面上允许多种字体。

有办法做到这一点吗?

提前谢谢大家,  罗伯特

0 个答案:

没有答案