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();
        }
    }
}
相关推荐
你怎么知道我是队长4 小时前
C语言---枚举变量
c语言·开发语言
李慕婉学姐4 小时前
【开题答辩过程】以《基于JAVA的校园即时配送系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·开发语言·数据库
吃茄子的猫4 小时前
quecpython中&的具体含义和使用场景
开发语言·python
云栖梦泽5 小时前
易语言中小微企业Windows桌面端IoT监控与控制
开发语言
数据大魔方5 小时前
【期货量化实战】日内动量策略:顺势而为的短线交易法(Python源码)
开发语言·数据库·python·mysql·算法·github·程序员创富
奋进的芋圆6 小时前
Java 延时任务实现方案详解(适用于 Spring Boot 3)
java·spring boot·redis·rabbitmq
sxlishaobin6 小时前
设计模式之桥接模式
java·设计模式·桥接模式
Edward.W6 小时前
Python uv:新一代Python包管理工具,彻底改变开发体验
开发语言·python·uv
小熊officer6 小时前
Python字符串
开发语言·数据库·python
model20056 小时前
alibaba linux3 系统盘网站迁移数据盘
java·服务器·前端