GhostscriptExample GS pdf转曲 pdf去白边

pdf转曲

pdf去白边

java 复制代码
package cn.net.haotuo.pojo;


import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;

import java.io.*;


public class GhostscriptExample {
    public static String gsPath = "C:/Program Files/gs/gs9.54.0/bin/gswin64c.exe";

    public static void main(String[] args) throws IOException {

        String inputFilePath = "C:\\Users\\Administrator\\Desktop\\2535.pdf";

        try {
            float pt = 72f/25.4f;
            String quchubaibian = quchubaibian(inputFilePath);
            PdfReader reader = new PdfReader(quchubaibian);
            String outPath  = quchubaibian.replaceAll("ok.pdf","白.pdf");
            Rectangle boxSize = reader.getBoxSize(1, "art");
            Document document = new Document(new Rectangle(boxSize.getWidth(), boxSize.getHeight()));
            FileOutputStream outputStream = new FileOutputStream(outPath);//新建一个pdf文档;
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);//把新建的pdf 赋值给 document
            writer.setBoxSize("trim",boxSize);
            writer.setPdfVersion(PdfWriter.VERSION_1_5);
            document.open();//打开 document文档
            PdfContentByte cb = writer.getDirectContent();
            for(int i=1;i<=reader.getNumberOfPages();i++){
                PdfImportedPage importedPage = writer.getImportedPage(reader, i);
                Rectangle boxSizeTemp = reader.getBoxSize(i, "art");
                Rectangle rectangle = new Rectangle(0,0,boxSizeTemp.getWidth(),boxSizeTemp.getHeight());
                document.setPageSize(rectangle);
                document.newPage();
                cb.addTemplate(importedPage,-boxSizeTemp.getLeft(),-boxSizeTemp.getBottom());
            }
            outputStream.flush();//关闭文件
            document.close();//关闭文件
            outputStream.close();//关闭文件
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    public static String quchubaibian(String pdfPath){
        String outPath = pdfPath.substring(0,pdfPath.length()-4)+"_ok.pdf";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(gsPath);
        stringBuffer.append(" -o " + outPath);
        stringBuffer.append(" -sDEVICE=pdfwrite -dFIXEDMEDIA -dPDFFitPage -c \"<</AutoRotatePages=/None>> setpagedevice\"");
        stringBuffer.append(" -f " + pdfPath);
        GhostscriptExample.run(stringBuffer.toString());
        return outPath;
    }

    /**
     * pdf转曲
     * @param pdfPath
     */
    public static void zhuanqu(String pdfPath){
        String outPath = pdfPath.substring(0,pdfPath.length()-4)+"_ok.pdf";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(gsPath);
        stringBuffer.append(" -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dNoOutputFonts");
        stringBuffer.append(" -sOutputFile=" + outPath);
        stringBuffer.append(" -f " + pdfPath);
        GhostscriptExample.run(stringBuffer.toString());

    }
    public static String pdf2ps(String pdfPath){
        String ps = pdfPath.substring(0,pdfPath.length()-3)+"ps";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(gsPath);
        stringBuffer.append(" -dNOPAUSE -dBATCH -sDEVICE=ps2write -dFILTERTEXT");
        stringBuffer.append(" -sOutputFile=" + ps);
        stringBuffer.append(" -f " + pdfPath);
        GhostscriptExample.run(stringBuffer.toString());
        return ps;
    }

    /**
     * 导出jpg
     *
     * @param pdfPath 输入pdf文件
     * @param outPath 输出jpg路径
     * @param dJPEGQ  jpg质量 0-100
     */
    public static void exportJpg(String pdfPath, String outPath, String dpi, String dJPEGQ) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(gsPath);
        stringBuffer.append(" -dNOPAUSE -dBATCH -sDEVICE=jpeg");
        stringBuffer.append(" -r" + dpi);
        stringBuffer.append(" -dJPEGQ=" + dJPEGQ);
        stringBuffer.append(" -sOutputFile=" + outPath);
        stringBuffer.append(" -f " + pdfPath);
        GhostscriptExample.run(stringBuffer.toString());
    }

    /**
     * 导出png
     *
     * @param pdfPath pdf路径
     * @param outPath 输出png路径
     * @param dpi     分辨率
     */
    public static void exportPng(String pdfPath, String outPath, String dpi) {
        /**
         * gsPath: Ghostscript 执行文件的路径
         * -dNOPAUSE: 防止 Ghostscript 在每一页处理完成后暂停等待用户操作
         * -dBATCH: 让 Ghostscript 在处理完最后一页后退出而不是等待新的输入
         * -sDEVICE=png16m: 指定输出设备为 PNG16m 格式。PNG16m 是一种支持多种颜色深度的 PNG 图像格式,其具有较高的图像质量。
         *      -sDEVICE=png16m 表示png-24 较大
         *      -sDEVICE=png256 表示png-8 较小
         */
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(gsPath);
        stringBuffer.append(" -dNOPAUSE -dBATCH -sDEVICE=png16m");
        stringBuffer.append(" -r" + dpi);
        stringBuffer.append(" -sOutputFile=" + outPath);
        stringBuffer.append(" -f " + pdfPath);
        GhostscriptExample.run(stringBuffer.toString());
    }

    public static void run(String command) {
        System.out.println(command);
        try {
            Process process = Runtime.getRuntime().exec(command);
            int exitCode = process.waitFor();

            if (exitCode == 0) {
                System.out.println("Conversion successful!");
            } else {
                System.out.println("Conversion failed with error code: " + exitCode);
                InputStream errorStream = process.getErrorStream();
                BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
                String line;
                while ((line = errorReader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
相关推荐
syt_biancheng7 小时前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法
影子24017 小时前
oralce创建种子表,使用存储过程生成最大值sql,考虑并发,不考虑并发的脚本,plsql调试存储过程,java调用存储过程示例代码
java·数据库·sql
武子康8 小时前
Java-172 Neo4j 访问方式实战:嵌入式 vs 服务器(含 Java 示例与踩坑)
java·服务器·数据库·sql·spring·nosql·neo4j
864记忆8 小时前
Qt Network 模块中的函数详解
开发语言·网络·qt
864记忆8 小时前
Qt Sql 模块中的函数详解
开发语言·网络·qt
程序猿DD8 小时前
深入探索剖析 JVM 的启动过程
java
是店小二呀8 小时前
五分钟理解Rust的核心概念:所有权Rust
开发语言·后端·rust
她说人狗殊途8 小时前
存储引擎MySQL
开发语言
自然数e8 小时前
C++多线程【线程管控】之线程转移以及线程数量和ID
开发语言·c++·算法·多线程