Solved

PrintDialog, PageSettings and PrintDocument questions

Posted on 2014-11-04
9
311 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…

734 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