GhostscriptExample GS pdf转曲 pdf去白边

本文介绍了如何使用Ghostscript库在Java中处理PDF文件,包括去除PDF页面的白边、转曲PDF以及将PDF转换为其他格式(如PS、JPEG和PNG),展示了如何通过命令行调用Ghostscript执行相应的转换操作。
摘要由CSDN通过智能技术生成

pdf转曲
pdf去白边

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
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值