Change Printer preferences / Driver Settings in C# - c#

I am trying to print an existing XPS file in GrayScale .
I tried to add a PrintTicket inside the XPS file, and also tried to change the PrintQueue property of the printer , like :
LocalPrintServer server = new LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer);
PrintQueue pq = server.GetPrintQueue("MyPrinterName");
pq.DefaultPrintTicket.OutputColor = OutputColor.Grayscale;
PrintSystemJobInfo print = pq.AddJob("myPrintJob", "MyFileToPrint.xps",false);
But still printing the file in colors ...
So , I thinking about changing the print setting in the driver settings itself , like :
So , Is it possible to change this option programmatically ??

Changing global state to solve a local problem is usually a bad idea. Submitting a print ticket with the job should allow the sort of control you are looking for. Have you tried something like this?
LocalPrintServer server = new LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer);
PrintQueue pq = server.GetPrintQueue("MyPrinterName");
var jobTicket = pq.DefaultPrintTicket;
jobTicket.OutputColor = OutputColor.Grayscale;
PrintSystemJobInfo print = pq.AddJob("myPrintJob", "MyFileToPrint.xps", false, jobTicket);

Related

Printing Excel file

xlWorkSheet.PrintOut();
This should work but according to the documentation, this is going to use the printer by default. Is it possible to give the user the ability to choose a printer like this:
In WinForms, I believe It's possible to achieve such a thing by using a PrintDialog like this:
var pd = new PrintDialog();
var settings = pd.PrinterSettings;
var name = settings.PrinterName
What's the equivalent for that in WPF? I don't see PrinterSettings/PrinterName
EDIT (more details):
I'm creating an excel file using Interop. The first code I posted will print the file using the default printer. I want from the user to be able to choose a printer. I realize there is ActivePrinter option but I will have to know the name of the printer in this case I believe.
What I want to do is make a printer dialog and get the Selected printer name and set it to ActivePrinter

MSProject Add-In to convert .mpp in .pdf with given name and date

I am developing an Add-In for MSProject 2013 and higher.
I want to safe/convert the opened .mpp file into an .pdf file without having an additional dialog for the user. He just presses the button an gets an notification when everything is done.
I need to save it in a spacific path and user a defined start and end date.
I tried the SaveAs-methode, but since it takes an MSProject.PjFileType as input and the is no option for pdf, I can't use this. PjFileFormat Enumeration
An other approche was using the DocumentExport-methode.
app.DocumentExport(#"D:/doc_exportqwre.pdf", MSProject.PjDocExportType.pjPDF, true, true, false, System.DateTime.Now, System.DateTime.Now.AddDays(42));
But in this case i only see 3 weeks at once. It is zoomed in and haven't found a way to change this. Changing the View in MSProject, before exporting does not help.
A third way is using the Windows10 pdf-printer:
// generate a file name as the current date/time in unix timestamp format
string file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
// the directory to store the output.
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// initialize PrintDocument object
PrintDocument doc = new PrintDocument()
{
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// tell the object this document will print to file
PrintToFile = true,
// set the filename to whatever you like (full path)
PrintFileName = Path.Combine(directory, file + ".pdf"),
}
};
doc.Print();
but this way I can not give the starting and end date. This results in having way to many pages I do not need.
Is there any chance to achieve my goal with changing one of my solutions or is there any other option?
After not finding a proper solution, i used the windows 10 Printer and simulated keyboard events to insert the path in the dialog, which opens. With Enter i start the printing.
I know it is not a nice solution, but it is the onlyway i got it to work

Print to PDF with Microsoft Print to PDF printer

I try to print a ms project file to pdf without any additional action needed.
I have tried several solutions without success.
PrintDocument doc = new PrintDocument()
{
//DocumentName = safeDir + fileName,
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// tell the object this document will print to file
PrintToFile = true,
// set the filename to whatever you like (full path)
PrintFileName = safeDir + fileName,
}
};
doc.Print();
If i try this approach like showed here, I get an empty pdf file.
Manually printing to PDF works fine.
Any suggestions to solve this problem?
My Spidey Senses tells me this is most likely caused by commas in the file name
This is a known bug when printing to PDF. Use a different driver or
don't put a comma in the file name
Note : Its not only from code, its in general in certain situations.
Disclaimer : this is a complete and utter guess
Check these other links out
Bug in "Print to PDF" and "Print to XPS" in Windows 10? comma in filename results in zero-byte file
Microsoft Print to PDF not working
Found a bug in Microsoft Print to PDF
Microsoft Print to PDF in Windows 10

Always print to default printer even the printervname is specified

I created one windows application in which I need to print PDF files silently.
string printername="jn-01";
if (printername != "NULL")
{
using (PrintDialog pd = new PrintDialog())
{
pd.PrinterSettings.PrinterName = printername;
MessageBox.Show(printername);
pd.PrinterSettings.Copies = 1;
if (pd.PrinterSettings.IsValid)
{
ProcessStartInfo info = new ProcessStartInfo(e.FullPath);
info.Verb = "PrintTo";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
}
}
I use above code for printing. But the system always print to default ptinter.
How can I solve this issue?
You are setting the printer name on a print dialog, but you aren't using the print dialog to print. Notice how you never use pd when printing. You are instead executing the PDF file (effectively using ShellExecute) with PrintTo verb. Now PrintTo verb supports providing the printer name as a command line argument. You can set it on info.Arguments. However for this to work, the default application that handles the PDF files needs to support it. See this page for more info on these verbs.
If the default PDF application has no support for this, then your alternative could be to call SetDefaultPrinter before printing and then restore it to its previous value after printing (Use GetDefaultPrinter to find which one is default first). Note that this changes the default printer for the user, so should not be the default choice when doing this.

how to print on a particular printer [duplicate]

I am making one application where user will print invoices which I am displaying using Crystal Report.
The user showed me his current application made using ForPro. In that application, under Printer Options form, one can see all the printers currently installed and the user could select default printer. When the invoice is made, the user presses the print button, then there is one dialog asking for no. of copies. When it's entered, the invoice gets printed directly, without any Print Dialog Box. If the user wants to change the printer again he/she will change it in the Printer Option form.
I want to know if similar thing is possible in Crystal Report and need guidance on how to approach for it.
Take a look at the ReportDocument.PrintToPrinter SAP Docs or MSDN Docs for how to specify the PrinterName and then Print using the ReportDocument object.
If you can try and get away from how the FoxPro app UI for printer selection. Instead use the standard print dialog box to select the printer.
You should note that if you don't set the PrinterName before sending the report to the printer it will use the default on the crystal file. Not to be confused with the user's OS default printer.
Here's an example of showing the PrintDialog settings some parameters using the SetParameterValue method and then sending the report document to a printer
// Note: untested
var dialog = new PrintDialog();
Nullable<bool> print = dialog.ShowDialog();
if (print.HasValue && print.Value)
{
var rd = new ReportDocument();
rd.Load("ReportFile.rpt");
rd.SetParameter("Parameter1", "abc");
rd.SetParameter("Parameter2", "foo");
rd.PrintOptions.PrinterName = dialog.PrinterSettings.PrinterName;
rd.PrintToPrinter(1, false, 0, 0);
}
The code above no longer works as advertised which has been admitted by SAP
You need to set the report document to an ISCDReportClientDocument and then print it. This is a more robust way of making sure the print job doesn't go to the default printer. The last two lines can be replaced with this code.
CrystalDecisions.ReportAppServer.Controllers.PrintReportOptions printReportOptions = new CrystalDecisions.ReportAppServer.Controllers.PrintReportOptions();
CrystalDecisions.ReportAppServer.Controllers.PrintOutputController printOutputController = new CrystalDecisions.ReportAppServer.Controllers.PrintOutputController();
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc;
rptClientDoc = cryRtp.ReportClientDocument;
printReportOptions.PrinterName = pDialog.PrinterSettings.PrinterName;
rptClientDoc.PrintOutputController.PrintReport(printReportOptions);
Here is another good link
http://mattruma.azurewebsites.net/?p=258

Categories