Solved

PrintDialog, PageSettings and PrintDocument questions

Posted on 2014-11-04
9
270 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now