I am trying to send an PDF file to a printer using the File.Copy method in C#. When I reference the printer by name however, it always responds with
'Could not find a part of the path'
The printer name is fully qualified. The user is selecting from a combo box that displays all the systems printers using the PrinterSettings.InstalledPrinters values.
Am I missing something simple?
Example:
File.Copy(FileInfo.FullName, "\\\\ServerName\\PrinterName", true);
The "\\\\ServerName\\PrinterName" is directly one of the names from PrinterSettings.InstalledPrinters collection.
Use one of the printer names that comes out of the printer classes.
foreach (String printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
textBox1.Text += printer.ToString() + System.Environment.NewLine;
}
See all the printers that are listed in this text box, copy and paste the printer name in the file.copy method.
File.Copy(FileInfo.FullName, **Printer name here**, true);
Related
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
I have to write a functionality that takes a PDF-document and sends it to a printer with some PJL commands. So far so good, I take the document, convert it to Postscript, send the postscript file to the printer with the required commands and the printer prints the document.
Now to the actual problem: Most of the document that needs to be printed by our software are invoices, therefore they are carefully made, such that every element is precisely positioned, and if it's off by millimeters the printed document is invalid. When printing the document directly through Adobe or any pdf viewing software, I can select the actual size option and everything is fine. Although if I print it via C# and PJL the document has different margins depending on the printer it has been printed on. Until now we used pdfprinting.net, and that option could be selected through pdfPrint.Scale = PdfPrint.ScaleTypes.None, but how can I do it via PJL?
// This are all the commands that I've tried, none of which achieved what I need
var parameters = new Dictionary<string, string>
{
{ "SET USERNAME",userName},
//{"SET PAPER", "A4" },
//{"SET MEDIATYPE", "PAPER" },
//{"SET TOPMARGIN", "TM6MM" },
//{"SET PRINTAREA", "INKEDAREA" },
{"SET MARGINS", "SMALLER" },
//{ "ENTER LANGUAGE","PDF"},
{ "ENTER LANGUAGE","POSTSCRIPT"},
};
var documentText = "\x1B%-12345X#PJL JOB NAME=" + jobName + " DISPLAY=" + jobDisplay;
foreach (var parameter in parameters)
{
documentText += "\r\n#PJL " + parameter.Key + "=" + parameter.Value;
}
documentText += "\r\n";
documentText += pdfString;
documentText += "\r\n\x0D\x0A\x1B%-12345X\r\n";
RawPrint(printerAddress, documentText, documentName);
// RawPrint() calls the printer methods found in 'winspool.drv', imported via 'DllImport'
Checking the PJL Reference Manual (Edition 12, which is the latest I have seen) there is simply no way to scale the page content in PJL.
Even if there were, I'd be surprised if it carried over to the PostScript (as opposed to PCL) interpreter environment, because PostScript has a rich feature set to handle this sort of setup. So basically you're going to need to get the PostScript correct.
Now, when you take a PDF file and produce PostScript from it, you are almost certainly producing generic PostScript; its device-neutral so it doesn't take account of aspects of the physical device.
Most obviously this will be things like hardware margins and unprintable areas. Many devices have limitations on which parts of the media they can print on, due to the paper handling. These will, of course, be different between different printers.
Of course, when you print from the operating system, the printer device driver knows what the printable area of the media is (because its the specific driver for the printer in question), and so it can arrange for the content to be scaled to the actual media.
Ghostscript certainly can produce PostScript (using the ps2write device) which is suitably scaled and translated for a given printer, provided you know what the characteristics of that printer are. In fact if the printer is sophisticated enough, the PostScript program may be able to interrogate the printer to retrieve some of these characteristics (ImagingBBox, PageOffset, Margins, ImageShift) and its then possible to write a PostScript program to dynamically resize the content of the page, based on these values (the PostScript produced by ps2write does not do this...).
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
I'm trying to send a file to print without opening it trough Adobe as suggested by several Answers here; instead, I'm using the PrintQueue library (from System.Drawing.Printing).
What I've accomplished so far:
I have the correct PrintQueue referenced as pq:
PrintQueue pq; //Assume it's correct. The way to get here it isn't easy and it is not needed for the question.
// Call AddJob
PrintSystemJobInfo myPrintJob = pq.AddJob();
// Write a Byte buffer to the JobStream and close the stream
Stream myStream = myPrintJob.JobStream;
Byte[] myByteBuffer = ObjectIHave.ToArray(); //Ignore the ObjectIhave, it actually is Linq object which is correct as well.
myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
myStream.Close();
As I understood from the Microsoft Library it's correctly done but it is not working. Any ideas?
EDIT: Debugging the code I can see that something is being send to the printer but it seems the file is not sent.
You need to render your PDF to the printer. Calling the shell print verb on the file would be the ideal means to do that. If you insist on doing the low level rendering yourself then I recommend using a library like Ghostscript.NET and choose the mswinpr2 device as output.
The mswinpr2 device uses MS Windows printer drivers, and thus should work with any printer with device-independent bitmap (DIB) raster capabilities. The printer resolution cannot be selected directly using PostScript commands from Ghostscript: use the printer setup in the Control Panel instead.
See SendToPrinterSample.cs for example:
string printerName = "YourPrinterName";
string inputFile = #"E:\__test_data\test.pdf";
using (GhostscriptProcessor processor = new GhostscriptProcessor())
{
List<string> switches = new List<string>();
switches.Add("-empty");
switches.Add("-dPrinted");
switches.Add("-dBATCH");
switches.Add("-dNOPAUSE");
switches.Add("-dNOSAFER");
switches.Add("-dNumCopies=1");
switches.Add("-sDEVICE=mswinpr2");
switches.Add("-sOutputFile=%printer%" + printerName);
switches.Add("-f");
switches.Add(inputFile);
processor.StartProcessing(switches.ToArray(), null);
}
If the file has to be printed in both sides you just need to add:
switches.Add("-dDuplex");
switches.Add("-dTumble=0");
You can not just write PDF bytes to a print job. The printer doesn't know how to handle it. The RAW data you send to the printer must describe the document in a printer language specific to the printer. That's what the printer driver does.
You can not print a PDF by just sending it to the printer. You need some piece of software that renders the PDF and then sends the rendered image to the printer.
As the documentation states:
Use this method to write device specific information, to a spool file, that is not automatically included by the Microsoft Windows spooler.
I enhanced the important part of this information.
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