如何在.NET中打印到XPS文件时默认文件名

时间:2013-08-05 17:54:27

标签: .net printing xps

当从我的.NET应用程序中选择Microsoft XPS Document Writer进行打印时,会向用户显示一个文件对话框,其中文件名最初为“* .XPS”。我希望它默认为更有用的名称(理想情况下,使用我提供的文档名称)。

我读了以下问题:

Way to default the name of the generated XPS file?

...并尝试按照答案中的建议设置PrinterSettings.PrintFileName,但它似乎不起作用......

        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...

如果我打印到Adobe PDF,则默认文件名为打印文档名称+“。PDF”(理想行为),但内置XPS打印驱动程序似乎缺少此功能,甚至似乎忽略了PrintFileName属性。我做错了什么,或者这是XPS打印驱动程序的问题?

BTW,我在Vista Business SP2上使用VS 2010 / .NET 4.0(均为SP1)

1 个答案:

答案 0 :(得分:0)

我相信这是XPS Document Writer的局限性。 Win2PDF打印机驱动程序可以save as XPS,并且默认使用打印文档名称。您可以尝试使用它代替Microsoft XPS Document Writer。