Print a document passing arguments to Process - c#

I'm working on sending documents to a specific printer, in this case the "Microsoft to PDF" one.
Everything work fine except that a prompt comes up asking for the folder where to save the pdf and its name.
Process printJob = new Process();
printJob.StartInfo.FileName = pathToFile;
printJob.StartInfo.UseShellExecute = true;
printJob.StartInfo.Verb = "printto";
printJob.StartInfo.CreateNoWindow = true;
printJob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
printJob.StartInfo.Arguments = "\"" + pdfPrinter + "\"";
printJob.StartInfo.WorkingDirectory = Path.GetDirectoryName(destinationPath);
printJob.Start();
Is there a way to pass any paramenter to printJob.StartInfo.Arguments such destination path and filename and avoid the prompt?

Related

.NET webservice attemp to print on a network printer not working

I am working on a c# web service using .net framwork 4.0
and I have a file (print.exe) which executes printing, the file works when I double click it manually, it prints, but when using the webservice it give an error that No printer installed.
this the code of the webmethod:
[WebMethod]
public String Print_In_Kitchen(Int32 OrderID, String Lang)
{
System.Security.SecureString secPass = new System.Security.SecureString();
string paswd = "96321";
for (int i = 0; i < paswd.Length; i++)
{
secPass.AppendChar(paswd[i]);
}
Process proc = new Process();
proc.StartInfo.FileName = #"C:\EXE_Print.exe";
proc.StartInfo.Arguments = #"" + OrderID + " " + Lang + " " + "\"" + ConfigurationManager.ConnectionStrings["constr"].ConnectionString + "\"";
proc.StartInfo.UserName = "omar";
proc.StartInfo.Password = secPass;
proc.StartInfo.Domain = "Futec";
proc.StartInfo.Verb = "runas";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Verb = "runas";
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
string s = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
return s;
}
I have searched for a solution but haven't found any useful link or explanation for this, saw this question here but with no answer:pdf print through .net process
and I am new to .NET
My gut feeling is that the User that the Service is running as, doesn't have access to that printer.
You could try changing StartInfo.UseShellExecute to true but I would also check the permissions of your services user.

Printing multiple files using process.start

Here I have code that opens Adobe and causes it to print a PDF file using Process.Start(). I am trying to print multiple files (two different pdfs) using the same command. How would I go about this? This is what I have so far:
Process profilePrintProcess = new Process();
profilePrintProcess.EnableRaisingEvents = true;
profilePrintProcess.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "PrintTo",
FileName = "[" + profFileName + " " + contractFileName + "]",
WindowStyle = ProcessWindowStyle.Hidden,
};
profilePrintProcess.Start();
I've been using this link as a guide per another SO question that referenced a similar issue.
to print a single file trough Acrobat Reader try this and let me know if worked:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "AcroRd32.exe";
startInfo.Arguments = "/p " + YourPDFFilePath;
process.StartInfo = startInfo;
process.Start();
to printig multipile pdf files first combine them and next print combined document trough PDFSharp
var outputPDFDocument = PdfDocument.Combine("Output.pdf", "doc1.pdf", "doc2.pdf", "doc3.pdf");
outputPDFDocument.Save("C:\\temp.pdf");
PdfFilePrinter.AdobeReaderPath = #"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe";
PdfFilePrinter.DefaultPrinterName = "Take printer";
PdfFilePrinter printer = new PdfFilePrinter(#"C:\\temp.pdf");
printer.Print();

How can I open windows explorer to a certain directory specified by user?

My code,
string pcname = #"remotepc";
string path = #"dir\sub\";
string remotepath = #"\\" + pcname + #"\C$\" + path;
System.Diagnostics.Process pexplorer = new System.Diagnostics.Process();
pexplorer.StartInfo.FileName = "explorer.exe";
pexplorer.StartInfo.Arguments = remotepath;
pexplorer.Start();
remotepath contains string like "\\remotepc\C$\dir\sub". If I copy this string into addressbar of Windows Explorer, it shows me this directory.
But into my program Explorer is opened at home directory.
When I look at pexplorer.StartInfoArguments, it contains something like
"\\\\remotepc\\C$\\Users\\use\\AppData\\Local\\"
If I set remotepath in program,
System.Diagnostics.Process pexplorer = new System.Diagnostics.Process();
pexplorer.StartInfo.FileName = "explorer.exe";
pexplorer.StartInfo.Arguments = #"\\remotepc\C$\dir\sub";
pexplorer.Start();
It works correctly, what is wrong here?
Process p = new Process();
p.StartInfo.FileName = #"explorer.exe";
p.StartInfo.Arguments = #"file:\\\" + #"Q:\somepath\...";
p.Start();
The problem is purely the URL. Adding "file:\\" to your full path did solve my problem.
http://blogs.msdn.com/b/freeassociations/archive/2005/05/19/420059.aspx
csharphardcoreprogramming.wordpress.com

Create pdf from html using wkhtmltopdf

i am genrating pdf from html code.Here i find code for generating using wkhtmltopdf
private void WritePDF(string HTML)
{
string inFileName,
outFileName,
tempPath;
Process p;
System.IO.StreamWriter stdin;
ProcessStartInfo psi = new ProcessStartInfo();
tempPath = Request.PhysicalApplicationPath + "ExcelFiles\\";
inFileName = Session.SessionID + ".htm";
outFileName = Session.SessionID + ".pdf";
// run the conversion utility
psi.UseShellExecute = false;
psi.FileName = "E:\\wkhtmltopdf";
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
// note that we tell wkhtmltopdf to be quiet and not run scripts
// NOTE: I couldn't figure out a way to get both stdin and stdout redirected so we have to write to a file and then clean up afterwards
psi.Arguments = "-q -n - " + tempPath + outFileName;
p = Process.Start(psi);
try
{
stdin = p.StandardInput;
stdin.AutoFlush = true;
stdin.Write(HTML);
stdin.Close();
if (p.WaitForExit(15000))
{
// NOTE: the application hangs when we use WriteFile (due to the Delete below?); this works
Response.BinaryWrite(System.IO.File.ReadAllBytes(tempPath + outFileName));
//Response.WriteFile(tempPath + outFileName);
}
}
finally
{
p.Close();
p.Dispose();
}
// delete the pdf
System.IO.File.Delete(tempPath + outFileName);
}
I found this answer from here
how to pass html as a string using wkhtmltopdf?
Here i get error Could not find file on
if (p.WaitForExit(15000))
{
// NOTE: the application hangs when we use WriteFile (due to the Delete below?); this works
Response.BinaryWrite(System.IO.File.ReadAllBytes(tempPath + outFileName));
//Response.WriteFile(tempPath + outFileName);
}
how i can create file for this now?
It gets Created on stdin.Close();
Please check if the html in stdin.Write(HTML); has some content in it

Invalid option file options: pad=oper

Getting this error: Invalid option file options on trying to invoke Astyle via C#
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + #"\Astyle.exe";
//strCommandParameters are parameters to pass to program
//pProcess.StartInfo.Arguments = "--style=ansi --recursive "+System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)+"/*.cpp";
pProcess.StartInfo.Arguments = " options=none test.cpp";
//pProcess.StartInfo.Arguments = " -h";
pProcess.StartInfo.UseShellExecute = false;
//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.RedirectStandardError = true;
//Optional
pProcess.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//Start the process
pProcess.Start();
//Get program output
string strOutput = pProcess.StandardOutput.ReadToEnd();
string strError = pProcess.StandardError.ReadToEnd();
//Wait for process to finish
pProcess.WaitForExit();
If you take a look at this link the correct syntax seems to be
--options=none

Categories