468,539 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  1.  
  2. //I AM USING PrintDocument COMPONENT
  3. private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  4.         {
  5.               //panelPrintData IS A PANEL CONTAING MY PRINTING PICTUREBOX,TEXTBOX,COMBO AND OTHERS NEED TO PRINT
  6.               printDoc(panelPrintData, e);
  7.         }
  8.  
  9.  
  10.  public void printDoc(Panel p,PrintPageEventArgs e)
  11.         {
  12.             try
  13.             {
  14.                 foreach (object obj in p.Controls)
  15.                 {
  16.                     if (obj is TextBox)
  17.                     {
  18.                         TextBox txt = (TextBox)obj;
  19.                         e.Graphics.DrawString(txt.Text, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, txt.Left , txt.Top );
  20.  
  21.                     }
  22.                     if (obj is Utility.LSInterfaces.IImagePrintable)
  23.                     {
  24.                         Utility.LSInterfaces.IImagePrintable img = (Utility.LSInterfaces.IImagePrintable)obj;
  25.                         e.Graphics.DrawImage(img.Picture, img.Left , img.Top , img.Width, img.Height);
  26.  
  27.                     }
  28.                     if (obj is LogiSoftUserControl.ResizableComponent.ResizablePictureBox)
  29.                     {
  30.                         LogiSoftUserControl.ResizableComponent.ResizablePictureBox img = (LogiSoftUserControl.ResizableComponent.ResizablePictureBox)obj;
  31.                         e.Graphics.DrawImage(img.Picture, img.Left , img.Top , img.Width, img.Height);
  32.  
  33.                     }
  34.                     if (obj is PictureBox)
  35.                     {
  36.                         PictureBox img = (PictureBox)obj;
  37.                         e.Graphics.DrawImage(img.Image, img.Left , img.Top , img.Width, img.Height);
  38.  
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             catch (ArgumentNullException)
  44.             { }
  45.  
  46.  
  47.         }
  48.  
  49.  
  50. //THIS IS BUTTON FOR PRINT
  51. private void btnPrint_Click(object sender, EventArgs e)
  52.         {
  53.             DialogResult r = printDialog1.ShowDialog();
  54.             if (r == DialogResult.OK)
  55.                 printDocument1.Print();
  56.         }
  57.  
  58.  
Dec 24 '09 #1
6 9651
tlhintoq
3,525 Expert 2GB
Quick thoughts
A) Turn off auto size
B) Pre-format your document to the printable area of the printer so no scaling is required
Dec 24 '09 #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
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
Dec 25 '09 #3
tlhintoq
3,525 Expert 2GB
But problem is C# for its
generic autofit to page nature dont let me doing this.
Turn off "autofit to page"
Dec 25 '09 #4
turn off "autofit to page"
yes, but how to turn off autofit of a page ?

Thanks
Dec 25 '09 #5
tlhintoq
3,525 Expert 2GB
Having never used the PrintDocument component I had to look it up at MSDN
http://msdn.microsoft.com/en-us/libr...nt(VS.80).aspx

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.PageScale 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

Expand|Select|Wrap|Line Numbers
  1. namespace FormPrinting
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         private Font printFont;
  6.         private StreamReader streamToPrint;
  7.  
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.  
  13.         private void btnPrint_Click(object sender, EventArgs e)
  14.         {
  15.             try
  16.             {
  17.                 streamToPrint = new StreamReader
  18.                    (browseFile1.Text);
  19.                 try
  20.                 {
  21.                     printFont = new Font("Arial", 13);
  22.                     PrintDocument pd = new PrintDocument();
  23.                     pd.PrintPage += new PrintPageEventHandler
  24.                        (this.pd_PrintPage);
  25.                     pd.Print();
  26.                 }
  27.                 finally
  28.                 {
  29.                     streamToPrint.Close();
  30.                 }
  31.             }
  32.             catch (Exception ex)
  33.             {
  34.                 MessageBox.Show(ex.Message);
  35.             }
  36.  
  37.         }
  38.  
  39.  
  40.         // The PrintPage event is raised for each page to be printed.
  41.         private void pd_PrintPage(object sender, PrintPageEventArgs ev)
  42.         {
  43.  
  44.             ev.Graphics.PageScale = 100;// Print at 100% or 1:1 ratio 
  45.             //ev.PageSettings.PrinterSettings.
  46.             ev.Graphics.DrawImageUnscaled(System.Drawing.Image.FromFile(browseFile1.Text),0,0);
  47.             ev.HasMorePages = false;
  48.             return; // Below here nothing runs. It is direct from the MSDN sample
  49.  
  50.             float linesPerPage = 0;
  51.             float yPos = 0;
  52.             int count = 0;
  53.             float leftMargin = ev.MarginBounds.Left;
  54.             float topMargin = ev.MarginBounds.Top;
  55.             string line = null;
  56.  
  57.             // Calculate the number of lines per page.
  58.             linesPerPage = ev.MarginBounds.Height /
  59.                printFont.GetHeight(ev.Graphics);
  60.  
  61.             // Print each line of the file.
  62.             while (count < linesPerPage &&
  63.                ((line = streamToPrint.ReadLine()) != null))
  64.             {
  65.                 yPos = topMargin + (count *
  66.                    printFont.GetHeight(ev.Graphics));
  67.                 ev.Graphics.DrawString(line, printFont, Brushes.Black,
  68.                    leftMargin, yPos, new StringFormat());
  69.                 count++;
  70.             }
  71.  
  72.             // If more lines exist, print another page.
  73.             if (line != null)
  74.                 ev.HasMorePages = true;
  75.             else
  76.                 ev.HasMorePages = false;
  77.         }
  78.     }
  79. }
Dec 26 '09 #6
tlhintoq
3,525 Expert 2GB
I do have concerns that your placement calculations are off though.

Expand|Select|Wrap|Line Numbers
  1. 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.
Dec 26 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Programatix | last post: by
reply views Thread by Programatix | last post: by
5 posts views Thread by Stefania Scott | last post: by
5 posts views Thread by C-Services Holland b.v. | last post: by
2 posts views Thread by Teemu | last post: by
2 posts views Thread by Sukh | last post: by
18 posts views Thread by Brett | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.