Marek
Marek

Reputation: 496

.NET Print Any File To XPS With File Name


I am trying to create method that prints any file to XPS using its default aplication, in my Windows forms app. This works fine, but I am unable to pass destination XPS path to the printer and open file dialog always pops out. Any suggestion without using FindWindow interrop will be helpfull.
Thank you!

private void PrintXps(string printFilePath, string destinationXps){
        var printJob = new Process();
        printJob.StartInfo.FileName = filePath;
        printJob.StartInfo.UseShellExecute = true;
        printJob.StartInfo.Verb = "printto";
        printJob.StartInfo.CreateNoWindow = true;
        printJob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        var xps = PrinterSettings.InstalledPrinters.Cast<string>().First(p => p.ToLower().Contains("xps"));
        printJob.StartInfo.Arguments = string.Format("\"{0}\"", xps); 
        printJob.StartInfo.WorkingDirectory = Path.GetDirectoryName(filePath);
        try
        {
            printJob.Start();
            printJob.WaitForExit(); 
        }
        catch (Win32Exception ex)
        {
            MessageBox.Show("File is not supported. "); 
        }
    }

Upvotes: 1

Views: 380

Answers (1)

Marek
Marek

Reputation: 496

I've found commerical solution that works. It installs its own driver and provides .NET api for setting file name. It converts directly to PDF, but then can be exported to XPS. Link here: Amyuni PDF Converter

Upvotes: 1

Related Questions