Solved

PrintDialog, PageSettings and PrintDocument questions

Posted on 2014-11-04
9
302 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

761 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