Class derived from Printdocument class C#

Asked

Viewed 385 times

2

I have a class to print labels (Cprint), this class is derived from the class PrintDocument. It takes an image calling a method contained in the class Form1.cs.

Follow the code of the class CPrint.cs (derived from the 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 with that in the method OnBeginPrint in this line printStream = new StreamReader(FileToPrint); does not receive the return of the method FileToPrint because it (method FileToPrint) only executes the part get of the code and not the set that where it should acquire the file name.

Thanks in advance!

  • Who wrote this class PrintDocument?

  • The class PrintDocument is C# own for printing.

  • @kaamis desktop or web application ? and objectively describe what functionality you want to accomplish at the click of that button !?

  • @Thiagofalcão is desktop. I want the Windows print dialog box to appear when the button is clicked and make an impression from the Cprint class derived from the Printdocument. However, I was able to find a code to call the class, which is quite simple after all, my problem now is that it does not print because the document name is not acquired by the Filetoprint method. I’ll even edit the question.

  • Paste here the code you use to invoke the print.

  • @Fernandonomellini follows the code that is on a button to invoke the print: CPrint printDoc = new CPrint();&#xA; PrintDialog dlgPrint = new PrintDialog();&#xA;&#xA; dlgPrint.Document = printDoc;&#xA;&#xA; if (dlgPrint.ShowDialog() == DialogResult.OK)&#xA; {&#xA; printDoc.Print();&#xA; }

Show 1 more comment

1 answer

1


Seems to me you’re 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 don’t think this should be used :

Print printDoc = new CPrint("NomeDoDocumentoASerImpresso"); 

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

  • I understood what you meant, I have to send the image too because it’s going to be printed. Or just with the name of the generated bitmap I can already do it? Could you leave the constructor that is and add this one that you talked about? One to call and receive the image and the other with the document name?

  • Kaamis, you need to print a certain file, and along with it, the image, correct ? Try to change the program using the code I gave you. Just change the line I indicated by passing the name of a file. see if it works. From what I saw, the image will already be loaded even using the constructor that receives the file name.

  • Actually I need to print only the image. Sorry ignorance but how can I get the name of the generated image? Or would it be the name of the bitmap variable?

  • has some forms, such as this: Filestream Fs = new Filestream(openFileDialog1.Filename , Filemode.Open, Fileaccess.Read); take a look: http://www.macoratti.net/09/c_prn1.htm

  • 1

    @Fernandonomellini I saved the image and took her name. Anyway, it worked, but then another mistake came and I found out that the derivative class couldn’t print images. So I decided instead of generating an image to generate a text file, because I want to print labels that contain barcodes, and I was able to use a specific source. So it worked. Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.