问题
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