将PDF颜色现有文件转换为PDF灰色文件或PDF黑白文件

时间:2015-09-02 13:12:23

标签: java pdf converter

我需要将PDF颜色的现有文件转换为PDF格式的PDF灰色或/和B& W文件。

我尝试使用Ghostscript执行cmd Runtime.getRuntime().exec("C:\...gswin64.exe" -o gray.pdf -sDEVICE=pdfwrite ... colors.pdf)并且它有效。

但是,我需要一个返回代码来知道命令执行是否成功(以及为什么不成功),我必须设置最小和最大大小。此外,我需要将PDF内容转换为变量,以便将其转移到另一个程序中。

你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我给出了解决方案代码中最重要的部分:

public enum DPIResolution {DPI72, DPI150, DPI300, DPI300PLUS}

// Program paths
public static final String PROGRAM_GSWIN64C_PATH = "C:/gs/gs9.16/bin/gswin64c.exe";
public static final String PROGRAM_GSWIN64C_OLD_PATH = "C:/gs/gs9.07/bin/gswin64c.exe";
public static final String PS_TMP_PATH = "D:/tmp.ps";

// Convert PDF Color to PDF Gray
private static int convertPDFColorToPDFGray(String inputPath, String outputPath, DPIResolution dpiResolution){
    String[] cmd = {
        PROGRAM_GSWIN64C_PATH, "-sDEVICE=pdfwrite", "-dPDFSETTINGS=/" + dpiForGS,
        "-sColorConversionStrategy=Gray", "-sColorConversionStrategyForImages=Gray", "-sProcessColorModel=DeviceGray",
        "-dCompatibilityLevel=1.4", "-dNOPAUSE", "-dBATCH",
        "-sOutputFile=" + outputPath, inputPath, "-c", "quit"
    };

    int exitCode = ghostScriptCommand(cmd);
}

// Convert PDF Color to PDF Black and White
private static int convertPDFColorToPDFBlackAndWhite(String inputPath, String outputPath){
    String[] cmd = {
        PROGRAM_GSWIN64C_OLD_PATH, "-sDEVICE=psmono", "-dNOPAUSE", "-dBATCH",
        "-sOutputFile=" + PS_TMP_PATH, inputPath, "-c", "quit"
    };

    String[] cmd2 = {
        PROGRAM_GSWIN64C_PATH, "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dBATCH",
        "-sOutputFile=" + outputPath, PS_TMP_PATH, "-c", "quit"
    };

    int exitCode = ghostScriptCommand(cmd);
    exitCode = ghostScriptCommand(cmd2);

    // We remove the PS tmp file
    File file = new File(PS_TMP_PATH);
    try{
        if(!file.delete()) System.out.println("Erreur de suppression de " + file.getName());
    } catch(Exception e){
        System.err.println("Erreur: " + e.getMessage());
        e.printStackTrace();
    }
};

// Command to Ghost Script (GS)
private static int ghostScriptCommand(String[] cmd){
    try {
        Process process = Runtime.getRuntime().exec(cmd);
        int exitCode = process.waitFor();

        // read the output from the command
        String s;
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String stringstdInput = "- Standard output of the command :\n";
        while ((s = stdInput.readLine()) != null) {
            stringstdInput += s + "\n";
        }
        // read any errors from the attempted command
        boolean hasStdError = false;
        BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        String stringstdError = "- Standard error of the command :\n";
        while ((s = stdError.readLine()) != null) {
            hasStdError = true;
            stringstdError += s + "\n";
        }
        System.out.print(stringstdInput);
        if(hasStdError) System.err.print(stringstdError);

    } catch (Exception e) {
        System.err.println("Erreur: " + e.getMessage());
        e.printStackTrace();
    }

    return exitCode;
}

private static String dpiResolutionToGS(DPIResolution dpiResolution){
    String dpiForGS;

    switch(dpiResolution){
        case DPI72 :
            dpiForGS = "screen";
            break;
        case DPI150 :
            dpiForGS = "ebook";
            break;
        case DPI300 :
            dpiForGS = "printer";
            break;
        case DPI300PLUS :
            dpiForGS = "prepress";
            break;
        default :
            dpiForGS = "default";
    }

    return dpiForGS;
}

正如您所看到的,我使用旧版本的Ghost脚本9.07来转换PS黑色和白色的PDF颜色。我没有找到另一个解决方案。