GhostscriptExample GS pdf转曲 pdf去白边
2024-01-10 16:09:48
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();
}
}
}
文章来源:https://blog.csdn.net/jialan75/article/details/135431127
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!