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();
        }
    }
}
相关推荐
lambo mercy26 分钟前
Day3:结构化输出
java·数据库·oracle
程序员清风35 分钟前
最短路径算法:BFS、Dijkstra 与 Floyd
java·算法·宽度优先
jimy11 小时前
c++的 Type Traits (一)
开发语言·c++
智能直播2 小时前
RK3528 V4L2 框架 UVC 接口自动获取 H.264 / MJPEG / YUYV 格式:从内核采集到硬解分发
java·网络·实时音视频·视频编解码·h.264
171320330计算机毕设编程2 小时前
计算机毕业设计 基于YOLO改进算法的车辆特征识别系统设计与实现--lw
java·大数据·python·sql·算法
Web极客码2 小时前
Python Ollama 本地 Agent 入门与订单查询实践
开发语言·jvm·python
badhope2 小时前
Spring AI Alibaba停更:Java程序员的信任,还能被辜负几次?
java·spring
const_qiu2 小时前
第一章 1.1-1.2:基础模型调用与 TypeScript 入门 —— Java 开发者的视角
java·javascript·typescript
Wang's Blog2 小时前
Java 项目实战: 交友项目 - Dubbo 负载均衡策略与实战验证
java·dubbo·交友
SL_staff2 小时前
销售知识库7天落地实践:面向业务侧的零代码嵌入方案
java·开源·产品