C# printing problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babaidebnath
    New Member
    • Dec 2009
    • 6

    C# printing problem

    Hello all,
    I am presently having a problem with printing in C#.
    The problem is my customer provided me a pre printed paper and I
    need to print value into some specific positions. But problem is C# for its
    generic autofit to page nature dont let me doing this. Every time it is
    decreasing whole page's size and also altering my value position into some
    specific ratio. Can anyone tell me how can I come over that problem.
    This is my code
    Code:
    //I AM USING PrintDocument COMPONENT
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                  //panelPrintData IS A PANEL CONTAING MY PRINTING PICTUREBOX,TEXTBOX,COMBO AND OTHERS NEED TO PRINT
                  printDoc(panelPrintData, e);
            }
    
    
     public void printDoc(Panel p,PrintPageEventArgs e)
            {
                try
                {
                    foreach (object obj in p.Controls)
                    {
                        if (obj is TextBox)
                        {
                            TextBox txt = (TextBox)obj;
                            e.Graphics.DrawString(txt.Text, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, txt.Left , txt.Top );
    
                        }
                        if (obj is Utility.LSInterfaces.IImagePrintable)
                        {
                            Utility.LSInterfaces.IImagePrintable img = (Utility.LSInterfaces.IImagePrintable)obj;
                            e.Graphics.DrawImage(img.Picture, img.Left , img.Top , img.Width, img.Height);
    
                        }
                        if (obj is LogiSoftUserControl.ResizableComponent.ResizablePictureBox)
                        {
                            LogiSoftUserControl.ResizableComponent.ResizablePictureBox img = (LogiSoftUserControl.ResizableComponent.ResizablePictureBox)obj;
                            e.Graphics.DrawImage(img.Picture, img.Left , img.Top , img.Width, img.Height);
    
                        }
                        if (obj is PictureBox)
                        {
                            PictureBox img = (PictureBox)obj;
                            e.Graphics.DrawImage(img.Image, img.Left , img.Top , img.Width, img.Height);
    
                        }
                    }
                }
    
                catch (ArgumentNullException)
                { }
               
    
            }
    
    
    //THIS IS BUTTON FOR PRINT
    private void btnPrint_Click(object sender, EventArgs e)
            {
                DialogResult r = printDialog1.ShowDialog();
                if (r == DialogResult.OK)
                    printDocument1.Print();
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Quick thoughts
    A) Turn off auto size
    B) Pre-format your document to the printable area of the printer so no scaling is required

    Comment

    • babaidebnath
      New Member
      • Dec 2009
      • 6

      #3
      Quick thoughts
      A) Turn off auto size
      B) Pre-format your document to the printable area of the printer so no scaling is required
      A) Turn off auto size of what ?
      B) If you can provide me . Some code of printable area (on my problem) will be helpful.....

      Thanks

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by bab
        But problem is C# for its
        generic autofit to page nature dont let me doing this.
        Turn off "autofit to page"

        Comment

        • babaidebnath
          New Member
          • Dec 2009
          • 6

          #5
          turn off "autofit to page"
          yes, but how to turn off autofit of a page ?

          Thanks

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Never done this before

            Having never used the PrintDocument component I had to look it up at MSDN
            Defines a reusable object that sends output to a printer, when printing from a Windows Forms application.


            I took their sample and put it in a new project with nothing more than a file browser and a [Print] button.

            Setting the e.Graphics.Page Scale to 100 seemed to work for me.
            I found this property by doing nothing more than typing e.Graphics. then looking at what IntelliSense showed me as available.

            I've tested this on SnagIt printer (print to image), PrimoPDF (Print to PDF) and my Samsung CLP-510

            Code:
            namespace FormPrinting
            {
                public partial class Form1 : Form
                {
                    private Font printFont;
                    private StreamReader streamToPrint;
            
                    public Form1()
                    {
                        InitializeComponent();
                    }
            
                    private void btnPrint_Click(object sender, EventArgs e)
                    {
                        try
                        {
                            streamToPrint = new StreamReader
                               (browseFile1.Text);
                            try
                            {
                                printFont = new Font("Arial", 13);
                                PrintDocument pd = new PrintDocument();
                                pd.PrintPage += new PrintPageEventHandler
                                   (this.pd_PrintPage);
                                pd.Print();
                            }
                            finally
                            {
                                streamToPrint.Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
            
                    }
            
            
                    // The PrintPage event is raised for each page to be printed.
                    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
                    {
                        
                        ev.Graphics.PageScale = 100;// Print at 100% or 1:1 ratio 
                        //ev.PageSettings.PrinterSettings.
                        ev.Graphics.DrawImageUnscaled(System.Drawing.Image.FromFile(browseFile1.Text),0,0);
                        ev.HasMorePages = false;
                        return; // Below here nothing runs. It is direct from the MSDN sample
            
                        float linesPerPage = 0;
                        float yPos = 0;
                        int count = 0;
                        float leftMargin = ev.MarginBounds.Left;
                        float topMargin = ev.MarginBounds.Top;
                        string line = null;
            
                        // Calculate the number of lines per page.
                        linesPerPage = ev.MarginBounds.Height /
                           printFont.GetHeight(ev.Graphics);
            
                        // Print each line of the file.
                        while (count < linesPerPage &&
                           ((line = streamToPrint.ReadLine()) != null))
                        {
                            yPos = topMargin + (count *
                               printFont.GetHeight(ev.Graphics));
                            ev.Graphics.DrawString(line, printFont, Brushes.Black,
                               leftMargin, yPos, new StringFormat());
                            count++;
                        }
            
                        // If more lines exist, print another page.
                        if (line != null)
                            ev.HasMorePages = true;
                        else
                            ev.HasMorePages = false;
                    }
                }
            }

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Doubt about your calculations

              I do have concerns that your placement calculations are off though.

              Code:
              e.Graphics.DrawString(txt.Text, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, txt.Left , txt.Top );
              So you take the text of a text box,and paint it onto a panel at the location of your textbox.

              I don't see how this takes into account the resolution of the printer.
              The textbox location is in pixels from the upper-left corner of the form.
              Coming down 100 pixel and right 200 pixels is meaningless without knowing the resolution of the printer.

              If the printer is 100dpi then down 100 pixels is down 1 inch.
              If the printer is 300dpi then down 100 pixels is down 1/3 inch

              Maybe the issue you are running into is the AutoScale of the printing, but rather your drawing on the panel is actually scaled down.

              Comment

              Working...