How to default the file name when printing to XPS file in .NET

别来无恙 提交于 2019-12-24 04:06:37

问题


When the Microsoft XPS Document Writer is selected for printing from my .NET application, the user is presented with a file dialog where the file name is initially "*.XPS". I'd like it to default to a more useful name instead (ideally, using the document name I am providing).

I read the following question:

Way to default the name of the generated XPS file?

...and tried setting the PrinterSettings.PrintFileName as suggested in the answers, but it does not seem to work...

        PrintDialog printDlg = new PrintDialog();
        PrintDocument printDoc = new PrintDocument();

        printDoc.DocumentName = name;
        printDlg.Document = printDoc;
        printDlg.AllowSelection = true;
        printDlg.AllowSomePages = true;

        //Call ShowDialog 

        if (printDlg.ShowDialog() == DialogResult.OK)
        {
            if (!printDoc.PrinterSettings.IsValid)
            {
                throw new Exception("Error: cannot find the default printer.");
            }
            else
            {
                if (printDoc.PrinterSettings.PrinterName.Contains("XPS"))
                {
                    printDoc.PrinterSettings.PrintFileName = name + ".XPS";
                }

                // Actual printing code from this point onward...

If I print to Adobe PDF, it defaults the file name to the print document name + ".PDF" (ideal behavior), but the built-in XPS print driver seems to lack this feature, and even seems to be ignoring the PrintFileName property. Am I doing something wrong, or is this an issue with the XPS print driver?

BTW, I am using VS 2010 / .NET 4.0 (both SP1) on Vista Business SP2


回答1:


I believe this is a limitation of the XPS Document Writer. The Win2PDF printer driver can save as XPS, and defaults to using the print document name. You could try using it instead of the Microsoft XPS Document Writer.



来源:https://stackoverflow.com/questions/18064538/how-to-default-the-file-name-when-printing-to-xps-file-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!