Printing PDF on OSX - c#

i am working in unity and i have a task of creating and then printing pdf from some of the snapshots taken through cameras in unity.
On windows after creating the pdf that can easily be done by calling the ShellExecute function and passing print as the parameter or using a function posted on stackoverflow i.e:
private void SendToPrinter()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"c:\output.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
, but i have no clue at all how would i be able to achieve the same for the OSX build?
Any help will be really appreciated.

You need to look at Apple's "Printing Programming Guide for Mac". (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Printing/osxp_aboutprinting/osxp_aboutprt.html)
To print a PDF page, you need a CGContextRef. In your views drawRect: method, you can get the the correct graphics context like this:
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
The method you use to draw a PDF page into a context is CGContextDrawPDFPage(context,page);
The easiest way to open a PDF document is to use CGPDFDocumentCreateWithURL
You get the pages using CGPDFDocumentGetPage.

Related

Adding a PDF to my visual studios form

So I am adding a help manual to my form and would like it to open a pdf in a separate window. Right now I am using this
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = #"Filepath";
process.Start();
}
But it obviously requires the full filepath. I want to include the pdf with the finished product and thus I won't know the full filepath on their system. I'm not sure if I can add this as a resource somehow? Sorry if I'm overlooking something simple.

Monitor printing of pdf files

So I have to create a console application that prints PDF files. Since I cannot use non-free libraries to print PDF, and I found out that most of free libraries for printing PDF files in .NET use a code similar to this
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "Print";
info.FileName = fileName;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo = info;
process.Start();
I use this code to start the printing process.
My problem is that I need to monitor the printing in case of an error occuring.
I've been searching for many days but I didn't find anything that works for me, since I can't use WMI (printer driver does not report its status to the spooler), I can't use PrintSystemJobInfo and PrintJobInfoCollection (I found out that it works with files natively handled by .NET but not PDF files), so the only thing that seems to work for now is to manage the printing with PrintQueue.
Now I'm looking for the proper way to use it, what I'm doing now is entering a while loop when the PrintQueueStatus is going from "None" to "Printing", and leaving the loop when the status goes to "Printing to "None", like this :
bool isCompleted = false;
bool isPrinting = false;
while (!isCompleted)
{
_printQueue.Refresh();
switch (_printQueue.QueueStatus)
{
[...]
case PrintQueueStatus.None:
Console.WriteLine("L'état n'est pas spécifié");
if (isPrinting)
{
isPrinting = false;
isCompleted = true;
}
break;
[...]
case PrintQueueStatus.Printing:
Console.WriteLine("Le périphérique imprime");
isPrinting = true;
break;
[...]
}
}
This doesn't allow me to check easily every case of the PrintQueueStatus...
I thought of using events, but since I'm a beginner in .NET and I'm not familiar with events, and can't find how to do it.
So the question is mainly, is it possible to use events to monitor a change in the PrintQueueStatus and how to do it. If it's not possible, how can I do to monitor the printing.
Thanks.

Starting process from .NET app and Attachment Execution Service

I'm using VLC player for playing videos from my WPF app (vlc palyer is distributed with app) It is possible to avoid alert "Open File - Security Warnng" during first start of video (vlc) on new machine without changing system settings?
I'm using this code for starting VLC:
var vlcArgs = string.Format("\"{0}\" --config=\"{1}\" -Incurse --play-and-exit",
videoFilePath, vlcConfigPath);
var psi = new ProcessStartInfo(#"vlc\vlc.exe", vlcArgs);
VlcProcess = Process.Start(psi);
I found that
Every script or program that is run by using the ShellExecute() API
passes through AES
So, if I set UseShellExecute property of ProcessStartInfo object to false, will it help? Or any other idea how to avoid AES check?
Use CreateProcess Instead of ShellExecute. Can you try the below code?
var vlcArgs = string.Format("\"{0}\" --config=\"{1}\" -Incurse --play-and-exit",
videoFilePath, vlcConfigPath);
var psi = new ProcessStartInfo(#"vlc\vlc.exe", vlcArgs);
psi.UseShellExecute = false;
VlcProcess = Process.Start(psi);
You can try the following :
If proc.StartInfo.UseShellExecute is false, then you are launching the process and can use:
proc.StartInfo.CreateNoWindow = true;
If proc.StartInfo.UseShellExecute is true,
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

Trigger Apache Nutch Crawl Programmatically

I'm trying to create a ASP.NET web api to trigger a crawl event to happen. I can't seem to get cygwin to process any of the commands I give it. The only thing I can really do is get it to open a terminal. Once the terminal is open I'd have to redirect the pwd to another location and then trigger my command I want.
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = false;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.FileName = "C:\\cygwin64\\bin\\mintty.exe";
p.StartInfo = info;
p.Start();
StreamWriter sw = p.StandardInput;
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(#"cd C:\Users\UName\Desktop\apache-nutch-2.3-mongodb\runtime\local\");
sw.WriteLine("bin/autoCrawl");
}
sw.Close();
p.WaitForExit();
I've tried many approaches, this is the last one I've tried but it just does nothing. Is there a way to launch this crawl from my .NET application? I've looked into the NutchApi about creating a new job with a type of crawl but I'm not sure if that applies here or not.
I ended up figuring out how to use the NutchApi to answer my question.

Passing arguments to running process in C#

I've some troubles with running processes and passing args to them.
I know how to run process with some args
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c something");
Process p = Process.Start(psi)
The problem is that after script is executed process is terminated. That's why there is "/c"
But I'm running multiple scripts and I would like to run them in one process ("cmd.exe") not to start new process every time.
Is there some solutions for it ?
I hope somebody understand what I'm talking about ;)
I recommend you utilize a batch file to script the execution of your executables and call your batch file instead. Or, you can do this -
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = new StreamWriter(p.StandardInput))
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("mysql -u root -p");
sw.WriteLine("mypassword");
sw.WriteLine("use mydb;");
}
}
It sounds like you ought to investigate redirecting the standard input - be sure to also set psi.UseShellExecute to false. You'll probably also want to redirect standard output, so you can have some way of knowing what your child process is doing.
Read more about redirection here.

Categories

Resources