Automatic printing in C# with ProcessStartInfo cutting off bottom and top - c#

I am using ProcessStartInfo to print my pdf file in my C# project in background. But the file that I printed is cutting off my top and bottom of the pdf file when printed. But when I print the same file by right clicking the pdf file it prints correctly.
Here is my code:
System.Diagnostics.Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = filename; // use default app to execute verb
//assert: can only go to local default printer
startInfo.Verb = "Print"; //prints to default printer
//try to keep Window hidden - work in background
startInfo.UseShellExecute = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
// set process to startInfo and execute start
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(10000);
process.CloseMainWindow();
process.Close();

this is in milliseconds. so it's only going to wait 10 seconds. Is that long enough?
process.WaitForExit(10000);
consider
process.WaitForExit();

Related

SEP (Symantec Endpoint Protection) for C#

I am using Doscan.exe of SEP to scan files before uploading but there is no provision provided by SEP to generate a separate log file for each scanned file, so that's why I am not able to make sure whether the file is ok or not for uploading.
If anyone has a way to check this thing then please let me know.
I am using version 14.2
Thanks.
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = " /ScanFile C:\\Users\\New Text Document (2).txt";
start.FileName = #"C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\14.2.3332.1000.105\Bin\DoScan.exe";
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
}

C# - Printing files regardles of type to printer

currently I am working with application that should silently print files regardless of their type to specific printer.
I have method to print:
public static void SendToPrinter(string filePath, string fileName, string printerName)
{
ProcessStartInfo info = new ProcessStartInfo(filePath);
info.Arguments = "\"" + printerName + "\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "PrintTo";
Process.Start(info);
}
and this works almost perfect.
I do not know how to force the printer to leave the original image size.
Currently the image is scaled to the page size.
Have you met someone with a similar problem?
Shell only sends print command to printer. There is no printer settings available. Instead try to use Print document. It has various printer settings.

open .msg file using Process.Start()

ProcessStartInfo startInfo = new ProcessStartInfo();
Process first = new Process();
startInfo.FileName = "OUTLOOK";
startInfo.Arguments = "http:\\blabla.com\EMAIL.msg";
startInfo.CreateNoWindow = true;
first.StartInfo = startInfo;
first.Start();
i used Process.Start to start up Outlook and open a .Msg file. how can i reuse the same process to open another .msg file without opening multiple processes/threads/instances of outlook?
i have tried something like
Process[] outlook = Process.GetProcessesByName("OUTLOOK");
Process existing = outlook[0];
startInfo.FileName = "outlook";
startInfo.Arguments = "http:\\blabla.com\2ndEMAIL.msg";
startInfo.CreateNoWindow = true;
existing.StartInfo = startInfo;
existing.Start();
to reuse the same process but i'm still opening multiple windows of outlook instead of just the .MSG file it
Slightly modified your code, this might work.
var first = new Process();
var pinfo = new ProcessStartInfo
{
FileName = "http:\\blabla.com\EMAIL.msg",
Arguments = "/quiet",
CreateNoWindow = true
};
first.StartInfo = pinfo;
first.Start();
Only one instance of Outlook can be run at the same time.
how can i reuse the same process to open another .msg file without opening multiple processes/threads/instances of outlook?
You can use the Process.Start method to open the message in Outlook. There is no need to specify Outlook, only path to the .msg file.
Be aware, the Application class in Outlook provides you the CreateItemFromTemplate method. It creates a new Outlook item based on the specified template and returns the newly created Outlook item. You can use it to create an Outlook item based on the .MSG file. See How To: Create a new Outlook message based on a template for more information.
If you want to close the already open Outlook messages, it is your responsibility to do so - use Application.Inspectors collection to enumerate all messages that Outlook is currently displaying and close them.
Just do it
var process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = fullPath //path of msg file
};
process.StartInfo = startInfo;
process.Start();

Print Save as PDF

I have the below code. As per my knowledge it is converting and saving as pdf. Can anyone explain this code?
Process cnp = new Process();
cnp.StartInfo.FileName = "AcroRd64.exe";
cnp.StartInfo.Arguments = "/n /t c:/test.jpg Microsoft Office Document Image Writer";
Updates:
I created a sample console application to trigger print and it is not working
class Program
{
static void Main(string[] args)
{
try
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";
p.StartInfo.Verb = "Print";
p.StartInfo.Arguments = "/n /t c:/test.png " + "Microsoft Office Document Image Writer";
p.StartInfo.CreateNoWindow = false;
p.StartInfo.UseShellExecute = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
}
Process is used to start and stop processes on your system.
Seems pretty clear that your code attempts to start a process "AcroRd64.exe", which I would guess is the pdf-reader Adobe Acrobat Reader. Argumens are arguments to the process, so basically, this is the equivalent of writing the following in command line:
AcroRd64.exe /n /t c:/test.jpg Microsoft Office Document Image Writer
There's a little more info on this under this other SO question.
Your code might doesn't work because the single argument Microsoft Office Document Image Writer contains whitespace. Try:
cnp.StartInfo.Arguments =
"AcroRd64.exe /n /t c:/test.jpg \"Microsoft Office Document Image Writer\"";

doc/docx to postscript/ps

I want to convert a doc/docx file to postscript by C# without using Word save to file since Word saved a big ps file.
Also, I want to know if there is a way to optimize a PDF by C#.
can I do that?
did you find anyway so you can convert doc/docx to ps?I have the same problem.
I know that it's too late to help you, but you can use ghostscript for optimizing pdf
int ExitCode;
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new System.Diagnostics.ProcessStartInfo("gswin64.exe", "-dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=C:\\26178DATA\\LowResOutput.pdf -dCompatibilityLevel=1.4 C:\\26178DATA\\Holding.pdf");
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
ProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Process = Process.Start(ProcessInfo);
Process.WaitForExit();
ExitCode = Process.ExitCode;
Process.Close();

Categories

Resources