使用apache common exec写入pdf

问题描述

您好,我正在尝试使用此apache通用exec,因此我正在尝试创建并写入文件。 写入文件的命令行参数如下 示例:PDFAnnotator.exe“ C:\ My Documents \ Test.pdf” 我尝试了以下

public PrintResultHandler print(final File file,final long printJobTimeout,final boolean printInBackground)
            throws IOException {

        int exitValue;
        ExecuteWatchdog watchdog = null;
        PrintResultHandler resultHandler;

        // build up the command line to using a 'java.io.File'
        CommandLine commandLine = new CommandLine("C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe");
        //CommandLine cmdLine = new CommandLine("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

        Map map = new HashMap();
        map.put("file",new File("C:\\test\\invoice.pdf"));
        commandLine.addArgument("/p");
        commandLine.addArgument("/h");
        commandLine.addArgument("${file}");

        // create the executor and consider the exitValue '1' as success
        final Executor executor = new DefaultExecutor();
        executor.setExitValue(1);

        // create a watchdog if requested
        if (printJobTimeout > 0) {
            watchdog = new ExecuteWatchdog(printJobTimeout);
            executor.setWatchdog(watchdog);
        }

        // pass a "ExecuteResultHandler" when doing background printing
        if (printInBackground) {
            System.out.println("[print] Executing non-blocking print job  ...");
            resultHandler = new PrintResultHandler(watchdog);
            executor.execute(commandLine,(Map<String,String>) resultHandler);
        }
        else {
            System.out.println("[print] Executing blocking print job  ...");
            exitValue = executor.execute(commandLine);
            resultHandler = new PrintResultHandler(exitValue);
        }

        return resultHandler;
    }

它不会创建任何pdf文件作为输出,请您提出建议。

解决方法

似乎此代码已从Apache Commons Exec教程代码中进行了修改。似乎您已经对代码进行了一些修改,导致了问题。

首先,您已删除该行

      commandLine.setSubstitutionMap(map);

如果没有这一行,您将创建变量map,将单个值放入此映射,然后对其进行进一步处理。显然,拥有一个您永远不会读取任何值的映射表将无法实现任何目标。恢复这条线很重要。

另一个问题是线路

            executor.execute(commandLine,(Map<String,String>) resultHandler);

此代码与教程代码之间的区别在于,您已将演员表添加到Map<String,String>中。 resultHandlerPrintResultHandler,但是此类未实现Map<String,String>,因此此强制转换将失败。

我根本不明白你为什么要演员。摆脱它,留给您:

            executor.execute(commandLine,resultHandler);

如果您的代码继续无法正常工作,那么我无法说出原因。也许Adode Reader可执行文件不在您认为的位置,也许文件不存在或没有读取权限。无论如何,应将适当的详细信息写入标准输出或标准错误,以帮助您进一步诊断问题。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...