473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 10173
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

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

Similar topics

0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
1
by: DCraig | last post by:
I'm having problems printing to a line printer from both Crystal Reports and SQL Server reporting services using dotnet. When I try and print a report from an application with Crystal I get the...
5
by: Stefania Scott | last post by:
I am trying to print a word document from Access. The code I've written works well in my computer but does not in the one were it is needed. Here the piece of code: 'doc path strObjectPath =...
5
by: C-Services Holland b.v. | last post by:
Hi all, I've run into a problem trying to print from vb.net (2002) in Windows 98. To test it I've setup a single form with a button and the following code: 'the form has a button called...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
2
by: Teemu | last post by:
I have an application created with VB6 and now I'm converting it to VB 2005. Conversion is not so simple because printing is so much different. In my app I have Timer-component, which is creates...
2
by: Sukh | last post by:
Hi I am stuck with a problem Can anyone help me out from this... I am printing a report on pre-printed continue paper using dot-matrix printer using vb.net. Data is printing on all the...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.