473,386 Members | 1,819 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,386 software developers and data experts.

How to force the dimensions of print out to 'overwrite' the printers default setting

147 100+
Hello all,

My application prints my windows form. When they click on the print button it does NOT give the customer the choice to select the printer or alter defaults. In this example, there is only 1 printer they can print too and it is hard coded into the code (we'll ignore the dangers of doing it this way for the moment ;) )

I want to force the height and width of the print out to 'overwrite' the printers default settings.

Expand|Select|Wrap|Line Numbers
  1. PrintDocument pd = new PrintDocument();
  2. PaperSize ps = new PaperSize("Custom", 100, 100); 
  3.  pd.PrintPage += new PrintPageEventHandler(printInfo);
  4.  pd.PrinterSettings.PrinterName = selectedPrinter;
  5.  pd.PrinterSettings.DefaultPageSettings.PaperSize = ps;
  6.  //pd.PrinterSettings.DefaultPageSettings.PaperSize.Height = 100;
  7. // pd.PrinterSettings.DefaultPageSettings.PaperSize.Width = 100;
  8.  
  9.  lb.Text = pd.PrinterSettings.DefaultPageSettings.PaperSize.ToString();
  10.   pd.print()
  11.  
  12.  
Despite my code telling the printer what WxH to use, it seems to revert to the default.

Any ideas?

Dave
Sep 15 '10 #1
5 5002
Plater
7,872 Expert 4TB
You appear to be correct. Much of the default page settings seem to be ignored.

For example:
Expand|Select|Wrap|Line Numbers
  1. string selectedPrinter = "Adobe PDF";
  2. PaperSize ps = new PaperSize("PlaterCustom", 100, 100);
  3.  
  4. PrintDocument pd = new PrintDocument();
  5. pd.DocumentName = "My Custom Document";
  6. pd.PrinterSettings.PrinterName = selectedPrinter;
  7. //pd.PrinterSettings.PrintToFile = true;
  8. string cFilename = pd.PrinterSettings.PrintFileName;
  9. if (pd.PrinterSettings.IsValid)//if printer exists
  10. {
  11.     pd.PrinterSettings.PrintFileName = pd.DocumentName;
  12.     PrinterSettings.PaperSizeCollection psc1 = pd.PrinterSettings.PaperSizes;//allowed sizes
  13.     PrinterResolution pr1 = pd.PrinterSettings.DefaultPageSettings.PrinterResolution;//current resolution
  14.     pd.PrinterSettings.DefaultPageSettings.PaperSize = ps;
  15.  
  16.     pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);//it seems to ignore this too?
  17.     //pd.OriginAtMargins = true;//this shoves the "start" over by margin amount, but not the amount i just assigned??
  18.  
  19.     pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
  20.     pd.QueryPageSettings += new QueryPageSettingsEventHandler(pd_QueryPageSettings);
  21.  
  22.     string pagesize = pd.PrinterSettings.DefaultPageSettings.PaperSize.ToString();//still good here
  23.     pd.Print();
  24. }
  25.  
  26.  
  27. static void pd_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
  28. {
  29.   PrintAction PA = e.PrintAction;
  30.   PageSettings sPage = e.PageSettings;
  31.   int bp = 1;//Settings are still correct at this time
  32. }
  33.  
  34. static void pd_PrintPage(object sender, PrintPageEventArgs e)
  35. {
  36.   Graphics g = e.Graphics;
  37.   Rectangle rMargin = e.MarginBounds;
  38.   Rectangle rPage = e.PageBounds;
  39.   PageSettings sPage = e.PageSettings;
  40.   int bp = 1;
  41.  
  42.   g.DrawRectangle(Pens.Yellow, sPage.Bounds);// This seems to be the papersize from the DefaultPageSettings 
  43.   g.DrawRectangle(Pens.Black, rMargin);//gets drawn with a standard adjustment(100pixels?)
  44.   g.DrawRectangle(Pens.Blue, rPage);//is the absolute outer edge so have to zoom in to see it
  45.  
  46.   Rectangle rPageMinus1 = TransformRectangle(rPage, -1);//= new Rectangle(rPage.X + 1, rPage.Y + 1, rPage.Width - 2, rPage.Height - 2);
  47.   g.DrawRectangle(Pens.Red, rPageMinus1);//will be an outer page boarder of 1px
  48. }
  49. public static Rectangle TransformRectangle(Rectangle r, int AdjustmentValue)
  50. {
  51.   int a=AdjustmentValue;
  52.   return new Rectangle(r.X - a, r.Y - a, r.Width + (2 * a), r.Height + (2 * a));
  53. }
  54.  
I see the same troubles, my page size gets set to 850x1100 which seems roughly 8.5' x 11' multiplied by 100, so 100pixels per inch resolution.
And despite setting the margins to 0, they seem to have reverted back to 100pixels(or 1 inch)
This is odd.
The pd.PrinterSettings.DefaultPageSettings.PrintableAr ea property shows where the 850x1100 comes from, but it is readonly.
Sep 15 '10 #2
Plater
7,872 Expert 4TB
Ok. I think I figured out what is going on.

When you assign the Custom size with 100x100, it flips through pd.PrinterSettings.PaperSources looking for a match.
When it doesn't find one, it picks the first one it finds that can encompass the desired size (850x1100).
It then sets all the printable area/bounds to THAT paper size, but in the PageSettings applies the correct 100x100 settings.
I went to the printer's driver page through the windows dialogs and ADD my papersize to the list. You will need to do some math to get 100pixels since that is not a choice (inches, mm or points)

Now I get 100x100 listed in the PrintableArea, but it still has the 850x1100 size listed in the PrintPage event.

So I guess i'm really not any closer.


Edit: At some point it just started working, I don't know if it was because i set my custom size as the "default" in the printing prefereces or because i started using a new filename or what, but now my page sizes are matching the custom size (139x139 which I guess means 100points=139pixels)
Sep 15 '10 #3
DaveRook
147 100+
Plater,

Thank you for looking into this with/for me. It is appreciated.

In a strange way I'm glad it's difficult because it's always embarrasing to ask a question which has an obvious answer.

The issue I have is I am printing over a terminal server. This means each time I log on, it creates a new session ID which creates a new instance of the printer and default settings. Therefore, I can't specify the paper sizes in the printer diag. box because next time the user logs on, it has a new session ID and therefore default settings. I need to find a way to tell the printer what settings to use regardless of what the printer has in its list of default printer names.

Sadly, I have to find a way to do this so if I do resolve it, I will of course post my findings.

Do you know if there is another way to do this? To recap, because we're printing over a terminal server (TS), we can't set up default printers as the server keeps forgetting the defaults. So, the program I'm writing must send the information to the printer and overwrite the printers defaults. Any ideas?

Again, thank you for your help.
Sep 16 '10 #4
Plater
7,872 Expert 4TB
I did not popup the printing dialog in my program, i did it roughly through control panel.
Even if the printer is located elsewhere, the drivers need to be installed locally (and the port becomes some type of networking port, instead of say LTP1) meaning that you can apply the "computer default" settings of adding that page.
Or do you connect to this printer in some other fashion?
If it appears as a choice in the PrintDocument, I am pretty sure it must have an entry in the Printers section?
Sep 16 '10 #5
DaveRook
147 100+
Hi

Well, it does work for me now but only in Adobe PDF. As soon as I try it with a printer, it fails.

I don't think is going to work over a terminal server, only over a local network. Looks like I'll have to create a VPN before my software will launch... joy!!!

Thank you again for all your help

Dave
Sep 16 '10 #6

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

Similar topics

4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
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...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
4
by: Suzanka | last post by:
Hello, I have an application written in C# on visual studio .NET. It is a web aplication. The application consists of many different forms, that users occassionaly want to print out for filing....
4
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
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...
0
by: nikhilgargi | last post by:
Requirement: I need to provide printing capability in a C# desktop application that I am developing The documents that need to be printed can be in Rich Text Format (RTF) or HTML. Custom...
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
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.