Solved

PrintDialog, PageSettings and PrintDocument questions

Posted on 2014-11-04
9
285 Views
Last Modified: 2014-11-07
I want to display a print dialog (first setting some attributes like the printer name). When the user clicks OK, I want to pass the printer attributes specified in the dialog to PrintDocument objects later in the program. The code looks something like this:

PrintDialog pdlg = new PrintDialog();
pdlg.PrinterSettings.PrinterName = cbPrt.SelectedItem.ToString(); // Selected from list
pdlg.ShowDialog();

static PageSettings PgSets = new PageSettings();
PgSets = pdlg.PrinterSettings.DefaultPageSettings;

In base class:

Dim WithEvents pDoc As New PrintDocument
... (many attributes set)
pDoc.DefaultPageSettings.PrinterSettings.PrintFileName = PgSets .PrinterSettings.PrintFileName
...

When the specified PrinterName is my HP printer, I don't have a problem. When the printer is an Adobe PDF writer, I get an exception when I try to do anything with the PrintFileName attribute:

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Illegal characters in path.

I basically want to display a print dialog and pass the parameters to PrintDocument objects. The print document objects are in a section of code that can't have any user intervention. Is the PageSettings object the right way to do this? Is there a better way to do this?

Also, I am moving each attribute from the PageSettings object to the PrintDocument individually (aaa.attr = bbb.attr). Is there a way to set them in a block?

Thank you
0
Comment
Question by:MikeBroderick
  • 5
  • 4
9 Comments
 
LVL 40
ID: 40423469
Try the following. It provides more than the PrinterName, so it might solve your problem:

pDoc.PrinterSettings = pdlg.PrinterSettings
0
 

Author Comment

by:MikeBroderick
ID: 40425239
I get the same error, An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll.

Additional information: Illegal characters in path.
0
 
LVL 40
ID: 40425441
What is the value of PrintFileName?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:MikeBroderick
ID: 40426088
It will not display. When I try to look at it using quickwatch, it says "invalid characters...". If I try to access the value, I.e. string aaa = pdoc.PrinterSettings.PrintFileName I get an exception.
0
 
LVL 40
ID: 40426567
Going further up the chain, what is the value of cbPrt.SelectedItem.ToString().

If it contains one of the following characters, they are not permitted: "!", "\", ",".

According to the documentation for PrinterSettings.PrinterName:

After setting the printer name, call IsValid to determine if the printer name is recognized as a valid printer on the system.

You can also use the InstalledPrinters property to get a list of printers installed on the system.
0
 

Author Comment

by:MikeBroderick
ID: 40427055
It contains Adobe PDF. I used the InstalledPrinters property to build a list and put them in a combo box. Only the PDF gives me problems.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40427109
Have you tried the call to IsValid that is suggested in the documentation?

Other than that, then it might simply be that PDF Writer is still has hard to use from code as it was 10 years ago. At that time, I was involved in a project where I spent a great deal of time making PDF Writer work through VBA code. We were never able to solve your problems, so we ended up using a freeware that did the job without a hitch.

Since then, I have completely dropped PDF Writer and use the free Cute PDF that works correctly with the PrintDocument. If all you need is to print to a pdf file without any extra, you might want to explore that option.
0
 

Author Comment

by:MikeBroderick
ID: 40427327
The IsValid didn't say anything was wrong.

Thanks for your help.
0
 

Author Closing Comment

by:MikeBroderick
ID: 40428204
After a lot of trial and error, I found a work-around. I should note that this app is a C#, but the print function is done via a VB base class called by C# derived classes. When I tried a very simple C# base class in a test app, I could not replicate the problem. I also found that the problem only occurs when I display a print dialog (with a printer other than Adobe PDF selected), then the user changes the printer named Adobe PDF. To get around the problem I:

+ Do not touch the PrinterFileName attribute (get or set).
+ Check the PC for printers named Adobe PDF
+ If there is one, make it the selected printer instead of the Default printer
+ Display the Print Dialog and process as one might expect

My guess is either there is some corruption or incompatibility somewhere in my Acrobat installation (it is part of CS5), or there is a problem marshaling parameters between C# and VB, even maybe I am doing something wrong (no, never) somewhere. It works so its time to move on.

Thanks for all of your help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question