简体   繁体   English

从命令行关闭acrobat reader的实例

[英]closing an instance of acrobat reader from command line

I am using the following cmd to print the pdf: 我使用以下cmd打印pdf:

acroRD32.exe /t "file1.pdf" "printerName"

Everything works fine but one window pops up. 一切正常,但会弹出一个窗口。 Can anybody help me to disable it. 任何人都可以帮我禁用它。 I tried with various options included in this question but cannot succeed. 我尝试了这个问题中包含的各种选项,但不能成功。

Any help is appreciated. 任何帮助表示赞赏。

Why even use Acrobat at all? 为什么甚至使用Acrobat呢? This class that print silently for you without any executables or even a printer setup: 此类无需任何可执行文件甚至打印机设置即可为您静默打印:

Sample Usage: 样品用法:
bool isPrinted = BatchPrint.PrintBinaryFile("file path", "Printer IP Address", "Queue Name", "User"); bool isPrinted = BatchPrint.PrintBinaryFile(“文件路径”,“打印机IP地址”,“队列名称”,“用户”);


public class BatchPrint
{

    private const int cPort = 515;
    private const char cLineFeed = '\n';
    private const int cDefaultByteSize = 4;
    public static string ErrorMessage = string.Empty;
    private static string mHost;
    private static string mQueue;
    private static string mUser;
    private static readonly Queue mPrintQueue = new Queue();
    private static readonly Dictionary<string, int> mLastPrintId = new Dictionary<string, int>();

    public static bool PrintBinaryFile(string filePath, string printerName, string queueName, string userName)
    {
        try
        {
            mHost = printerName;
            mQueue = queueName;
            mUser = userName;
            BeginPrint(filePath);
        }
        catch (Exception ex)
        {
            ErrorMessage += ex.Message + cLineFeed + ex.StackTrace;
        }
        return ErrorMessage.Length <= 0;
    }

    private static void BeginPrint(string filePath)
    {      
        mPrintQueue.Enqueue(filePath);
        ThreadStart myThreadDelegate = SendFileToPrinter;
        var myThread = new Thread(myThreadDelegate);
        myThread.Start();
    }

    private static void SendFileToPrinter()
    {
        ErrorMessage = string.Empty;
        var fileFromQueue = (string)mPrintQueue.Dequeue();
        var tcpClient = new TcpClient();
        tcpClient.Connect(mHost, cPort);
        const char space = ' ';
        using (var networkStream = tcpClient.GetStream())
        {
            if (!networkStream.CanWrite)
            {
                ErrorMessage = "NetworkStream.CanWrite failed";
                networkStream.Close();
                tcpClient.Close();
                return;
            }
            var thisPc = Dns.GetHostName();
            var printId = GetPrintId();
            var dfA = string.Format("dfA{0}{1}", printId, thisPc);
            var cfA = string.Format("cfA{0}{1}", printId, thisPc);
            var controlFile = string.Format("H{0}\nP{1}\n{5}{2}\nU{3}\nN{4}\n", thisPc, mUser, dfA, dfA, Path.GetFileName(fileFromQueue), true);
            const int bufferSize = (cDefaultByteSize * 1024);
            var buffer = new byte[bufferSize];
            var acknowledgement = new byte[cDefaultByteSize];
            var position = 0;
            buffer[position++] = 2;
            ProcessBuffer(mQueue, ref buffer, ref position, (byte)cLineFeed);
            if (!IsAcknowledgementValid(buffer, position, acknowledgement, networkStream, tcpClient, "No response from printer"))
                return;               
            position = 0;
            buffer[position++] = 2;
            var cFileLength = controlFile.Length.ToString();
            ProcessBuffer(cFileLength, ref buffer, ref position, (byte)space);
            ProcessBuffer(cfA, ref buffer, ref position, (byte)cLineFeed);
            if (!IsAcknowledgementValid(buffer, position, acknowledgement, networkStream, tcpClient, "Error on control file len"))
                return;
            position = 0;
            ProcessBuffer(controlFile, ref buffer, ref position, 0); 
            if (!IsAcknowledgementValid(buffer, position, acknowledgement, networkStream, tcpClient, "Error on control file"))
                return;
            position = 0;
            buffer[position++] = 3;
            var dataFileInfo = new FileInfo(fileFromQueue);
            cFileLength = dataFileInfo.Length.ToString();
            ProcessBuffer(cFileLength, ref buffer, ref position, (byte)space);
            ProcessBuffer(dfA, ref buffer, ref position, (byte)cLineFeed);
            if (!IsAcknowledgementValid(buffer, position, acknowledgement, networkStream, tcpClient, "Error on dfA"))
                return;              
            long totalbytes = 0;
            using (var fileStream = new FileStream(fileFromQueue, FileMode.Open))
            {
                int bytesRead;
                while ((bytesRead = fileStream.Read(buffer, 0, bufferSize)) > 0)
                {
                    totalbytes += bytesRead;
                    networkStream.Write(buffer, 0, bytesRead);
                    networkStream.Flush();
                }
                fileStream.Close();
            }
            if (dataFileInfo.Length != totalbytes)
                ErrorMessage = fileFromQueue + "File length error";
            position = 0;
            buffer[position++] = 0;
            if (!IsAcknowledgementValid(buffer, position, acknowledgement, networkStream, tcpClient, "Error on file"))
                return;
            networkStream.Close();
            tcpClient.Close();
        }
    }

    private static int GetPrintId()
    {
        var count = 0;
        lock (mLastPrintId)
        {
            if (mLastPrintId.ContainsKey(mUser))
                count = mLastPrintId[mUser];
            count++;          
            count %= 1000; 
            if (mLastPrintId.ContainsKey(mUser))
                mLastPrintId[mUser] = count;
            else
                mLastPrintId.Add(mUser, count);
        }
        return count;
    }

    private static void ProcessBuffer(string item, ref byte[] buffer, ref int position, byte nextPosition)
    {
        foreach (var t in item)
        {
            buffer[position++] = (byte)t;
        }
        buffer[position++] = nextPosition;
    }

    private static bool IsAcknowledgementValid(byte[] buffer, int position, byte[] acknowledgement, NetworkStream networkStream, TcpClient tcpClient, string errorMsg)
    {
        networkStream.Write(buffer, 0, position);
        networkStream.Flush();
        networkStream.Read(acknowledgement, 0, cDefaultByteSize);
        if (acknowledgement[0] == 0) 
            return true;
        ErrorMessage = errorMsg;
        networkStream.Close();
        tcpClient.Close();
        return false;
    }

}

You cannot close the last open Acrobat window through the command line. 您无法通过命令行关闭上次打开的Acrobat窗口。 From ancient history of programming scripts for Acrobat, I believe that there is no way to do this in an Acrobat script, either. 从古老的Acrobat编程脚本历史来看,我相信在Acrobat脚本中也无法做到这一点。

But you can close Acrobat from the command line, though it is a bit convoluted. 但是你可以从命令行关闭Acrobat,虽然它有点复杂。 For a pure MSDOS method for most Windows systems, create two files: called.bat and caller.bat . 对于大多数Windows系统的纯MSDOS方法,请创建两个文件: called.batcaller.bat

caller.bat : caller.bat

REM call the batch file that runs the program:
start "ProgramRunner" called.bat

REM wait 5 seconds -- if this closes Acrobat too soon, then increase the time:
ping 1.1.1.1 -n 1 -w 5000 >nul

REM kill the called program -- should be the program name that was started in called.bat:
REM (If the program does not close, type "taskkill /?" in the command line to see options.)
taskkill /F /IM acroRD32.exe

called.bat called.bat

"path\to\the\program\acroRD32.exe" /t "path\to\the\program\file1.pdf" "printerName"

exit

Note that you can write scripts in many programming languages that accomplish the same task more elegantly. 请注意,您可以使用许多编程语言编写脚本,以更优雅地完成相同的任务。

I successfully tested forking a delayed kill command before starting the print job. 我在开始打印作业之前成功测试了延迟kill命令。

start cmd /c "timeout 15 & taskkill /f /im acrord32.exe"
acroRD32.exe /t "file1.pdf" "printerName"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM