Class derived from PrintDocument class C #

2

I have a class to print labels (CPrint), this class is derived from the PrintDocument class. In it it receives an image by calling a method contained in the Form1.cs class.

Here is the code for class CPrint.cs (derived from class PrintDocument ):

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

namespace WindowsApplication1
{
class CPrint : PrintDocument
{
    private Font printFont;
    private TextReader printStream;
    private string fileToPrint;
    private Bitmap imgWatermark, bmpImg;

    public bool Watermark = false;

    public CPrint()
    {
        Form1 principal = new Form1();

        imgWatermark = principal.enviaImg();  //ESSE TEM QUE RECEBER A IMAGEM


    }

    public CPrint(string fileName) : this()
    {
        this.FileToPrint = fileName;
    }

    public string FileToPrint
    {
        get
        {
            return fileToPrint;
        }
        set
        {
            if (File.Exists(value))
            {
                CPrint p = new CPrint();
                fileToPrint = value;
                this.DocumentName = value;
            }
            else
                throw (new Exception("File not found."));
        }
    }

    public Font Font
    {
        get { return printFont; }
        set { printFont = value; }
    }

    protected override void OnBeginPrint(PrintEventArgs e)
    {
        base.OnBeginPrint(e);
        printFont = new Font("Verdana", 10);
        printStream = new StreamReader(FileToPrint);
    }

    protected override void OnEndPrint(PrintEventArgs e)
    {
        base.OnEndPrint(e);
        printFont.Dispose();
        printStream.Close();
    }

    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        base.OnPrintPage(e);

        // Slow down printing for demo.
        System.Threading.Thread.Sleep(200);

        Graphics gdiPage = e.Graphics;
        float leftMargin = e.MarginBounds.Left;
        float topMargin = e.MarginBounds.Top;
        float lineHeight = printFont.GetHeight(gdiPage);
        float linesPerPage = e.MarginBounds.Height / lineHeight;
        int lineCount = 0;
        string lineText = null;

        // Watermark?
        if (this.Watermark)
        {
            int top = Math.Max(0,
                     (e.PageBounds.Height - imgWatermark.Height) / 2);
            int left = Math.Max(0,
                     (e.PageBounds.Width - imgWatermark.Width) / 2);
            gdiPage.DrawImage(imgWatermark, left, top,
                     imgWatermark.Width, imgWatermark.Height);
        }

        // Print each line of the file.
        while (lineCount < linesPerPage &&
              ((lineText = printStream.ReadLine()) != null))
        {
            gdiPage.DrawString(lineText, printFont, Brushes.Black,
            leftMargin, (topMargin + (lineCount++ * lineHeight)));
        }

        // If more lines exist, print another page.
        if (lineText != null)
            e.HasMorePages = true;
        else
            e.HasMorePages = false;
    }
}
}
What is going wrong is that the name of the document that is not acquired and thus in the OnBeginPrint method on this line printStream = new StreamReader(FileToPrint); does not receive the return of the FileToPrint method because it (method FileToPrint ) only executes the get part of the code and not set than where it should acquire the file name.

Thank you in advance!

    
asked by anonymous 26.06.2015 / 19:18

1 answer

1

It seems to me that you are using the wrong builder.

Your button code.

Print printDoc = new CPrint(); 
rintDialog dlgPrint = new PrintDialog(); 
lgPrint.Document = printDoc; 
f (dlgPrint.ShowDialog() == DialogResult.OK) { printDoc.Print(); }

You do not think this should be used:

Print printDoc = new CPrint("NomeDoDocumentoASerImpresso"); 

Because it is the constructor that receives the document name and associates it with private property fileToPrint

    
30.06.2015 / 21:49