Click here to Skip to main content
14,692,055 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
Please give the code to print form directly to the printers.

Thanks
Surender
Posted
Updated 2-Jul-14 3:03am
v2
Comments
ZurdoDev 2-Jul-14 7:58am
   
Google has the code. Where are you stuck?
johannesnestler 2-Jul-14 10:39am
   
which form? which printer(S) (more than one)? What you mean with directly? Btw. your repeated "give code" makes me "angry" (maybe read forum rules?), how about WRITE SOME CODE? Or at least try to formulate a real question with needed information - so "give me question"!
Marc Gabrie 5-Jul-14 7:05am
   
Are you talking about a desktop app or ASP.NET website?

1 solution

;)
Just change System.Drawing.Image.FromFile("logomega.png"); for a picture of your own ^^

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;

namespace OlhaPC
{
    public partial class Principal : Form
    {

        private PrintDocument printDoc = new PrintDocument();

        String osInfo1 = "";
        String osInfo3 = "";
        String osInfo4 = "";
        String osInfo5 = "";
        String osInfo6 = "";
        String osInfo7 = "";
        String osInfo8 = "";
        String osInfo9, osInfo10 = "";
        System.OperatingSystem osInfo2;


        public Principal()
        {
            InitializeComponent();
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

            DateTime saveNow = DateTime.Now;

            osInfo2 = System.Environment.OSVersion;
            osInfo1 = System.Environment.MachineName;
            osInfo3 = System.Environment.OSVersion.Platform.ToString();
            osInfo4 = System.Environment.OSVersion.ServicePack.ToString();
            osInfo5 = System.Environment.ProcessorCount.ToString();
            osInfo6 = System.Environment.SystemDirectory.ToString();
            osInfo7 = System.Environment.TickCount.ToString();
            MessageBox.Show(osInfo2.ToString() + Environment.NewLine +
                            osInfo3.ToString() + Environment.NewLine +
                            osInfo4.ToString() + Environment.NewLine +
                            osInfo5.ToString() + Environment.NewLine +
                            osInfo1.ToString() + Environment.NewLine +
                            osInfo7.ToString() + Environment.NewLine +
                            osInfo6.ToString() + Environment.NewLine +
                            saveNow.ToString()
                            );

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        //Impressao (Dados e Tipo de letra etc)
        private void printDoc_PrintPage(Object sender,
           PrintPageEventArgs e)
        {
            
            //System.Drawing.Image img = System.Drawing.Image.FromFile("D:\\Foto.jpg");
            System.Drawing.Image img = System.Drawing.Image.FromFile("logomega.png");
            Point loc = new Point(0, 0);
            e.Graphics.DrawImage(img, loc); 

            String textToPrint = ".NET Printing is easy: ";
            Font printFont1 = new Font("Courier New", 16);
            e.Graphics.DrawString(textToPrint, printFont1,
                  Brushes.Black, 0, 100);

            String textToPrint2 = "So much much easy";
            Font printFont2 = new Font("Courier New", 14);
            e.Graphics.DrawString(textToPrint2, printFont2,
                  Brushes.Red, 310, 101);

            e.Graphics.DrawString(osInfo1, printFont2, Brushes.Red, 20, 141);
            e.Graphics.DrawString(osInfo2.ToString(), printFont2, Brushes.Black, 20, 181);
            e.Graphics.DrawString(osInfo3, printFont2, Brushes.Red, 20, 221);
            e.Graphics.DrawString(osInfo4, printFont2, Brushes.Black, 20, 261);
            e.Graphics.DrawString(osInfo5, printFont2, Brushes.Red, 20, 301);
            e.Graphics.DrawString(osInfo6, printFont2, Brushes.Black, 20, 341);
            e.Graphics.DrawString(osInfo7, printFont2, Brushes.Red, 20, 381);
            //MessageBox.Show(textToPrint.Length.ToString());

        }

        //Butao imprimir com chamada para printDoc.Print
        private void button1_Click(object sender, EventArgs e)
        {
            printDoc.Print();
        }

        
    }
}
   
Comments
Member 10918392 2-Jul-14 8:08am
   
i want form to print directly
so give another code
Jorge Colaco 2-Jul-14 9:04am
   
This print directly to the default printer

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