?
Solved

PrintDialog, PageSettings and PrintDocument questions

Posted on 2014-11-04
9
Medium Priority
?
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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