Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF application throws an exception after printing to file via doPDF printer

Tags:

printing

wpf

xaml

I have made a wpf standalone application for accounting purposes. The application works and prints well to an external printer or microsoft XPS printer. The problem arises when i select doPDF(an application which outputs a pdf file) as printer. The output is just fine; it creates the pdf file and saves it, but, when i come back to the application and press any button, an XamlParseException was unhandled error is thrown which says

'The invocation of the constructor on type 'RegisterMaintenance.DisplayInvoice' that matches the specified binding constraints threw an exception.' Line number '5' and line position '7'

InnerException: Verify that the file exists in the specified location

InnerException: When using relative paths, make sure the current directory is correct

Why is this happening and what can i do to get rid of this problem.

like image 490
Pankaj Upadhyay Avatar asked Jun 23 '11 06:06

Pankaj Upadhyay


3 Answers

Seems that this pdf printer changes the current directory for the process. I can think of 2 possible workarounds for your problem.

The first and most obvious workaround for your problem would be to use a absolute path for "Transactions.xml" and store this in your app.config. This might however become problematic if your application is ditributed to various different machines and installed in different locations.

The second workaround is to temporarily store your current working directory before calling the print function, and then restore it afterwards, if it has changed. Something like this:

string path = Directory.GetCurrentDirectory();
//Do the print stuff
Directory.SetCurrentDirectory(path);
like image 55
Edwin de Koning Avatar answered Nov 14 '22 16:11

Edwin de Koning


Seems like most probable cause is that some resource is not found or already occupied. Please share your code for Line number '5' and line position '7' with us.

like image 37
Gurpreet Singh Avatar answered Nov 14 '22 15:11

Gurpreet Singh


You can use your debugger and set a breakpoint on line number 5 where the constructor for RegisteMaintenance.DisplayInvoice is called. Look at the file path being used and fix the problem. Alternately, if you're highly confident it's not a problem you can try/catch and ignore the exception but I don't really recommend that.

like image 1
Tod Avatar answered Nov 14 '22 15:11

Tod