目录
[PDF转换为JPG(带颜色) 代码](#PDF转换为JPG(带颜色) 代码)
[PDF转换为TIF(LZW形式压缩,可以显示子的深浅) 代码](#PDF转换为TIF(LZW形式压缩,可以显示子的深浅) 代码)
[PDF转换为TIF(CCITT形式压缩,无法显示子的深浅) 代码](#PDF转换为TIF(CCITT形式压缩,无法显示子的深浅) 代码)
=========
0.前言:
这个工具是java实现的,
现在电脑上一般都安装了JRE,即使你不会java,不懂什么是Java。
只要你电脑上安装了jre,这个工具一样可以使用。
===
1.工具目录
把要转换的PDF命名为【input.pdf】放到下面的目录中
2.工具功能(效果),如何运行
效果
PDF转换为JPG(带颜色)
===
PDF转换为TIF(LZW形式压缩,可以显示子的深浅)
PDF转换为TIF(CCITT形式压缩,无法显示子的深浅)
如何运行
执行对于的bat即可执行。
===
3.代码
PDF转换为JPG(带颜色) 代码
java
package com.sxz.study.pdf;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
public class TestPDFToJpg {
public static void main(String[] args) {
try {
String currentDirectory = System.getProperty("user.dir");
System.out.println("----------------------");
System.out.println(currentDirectory );
// Load the PDF document
PDDocument document = PDDocument.load(new File((currentDirectory + "\\PDF\\input.pdf")));
// Create a PDF renderer
PDFRenderer pdfRenderer = new PDFRenderer(document);
// Iterate over each page in the PDF document
for (int pageNumber = 0; pageNumber < document.getNumberOfPages(); pageNumber++) {
// Render the PDF page to a BufferedImage
// BufferedImage image = pdfRenderer.renderImageWithDPI(pageNumber, 300,
// ImageType.RGB);
BufferedImage image = pdfRenderer.renderImageWithDPI(pageNumber, 200, ImageType.RGB);
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// writeParam.setCompressionType("LZW");
// 创建输出文件流
FileImageOutputStream output = new FileImageOutputStream(new File(currentDirectory + "\\JPG\\Oupput_" + (pageNumber + 1) + ".jpg"));
writer.setOutput(output);
// 写入压缩后的TIF图像
writer.write(null, new IIOImage(image, null, null), writeParam);
// 关闭输出流
output.close();
}
// Close the PDF document
document.close();
System.out.println("PDF converted to TIF successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
PDF转换为TIF(LZW形式压缩,可以显示子的深浅) 代码
java
package com.sxz.study.pdf;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
public class TestPDFToTIFCompressLZW {
public static void main(String[] args) {
try {
String currentDirectory = System.getProperty("user.dir");
System.out.println("----------------------");
System.out.println(currentDirectory );
// Load the PDF document
PDDocument document = PDDocument.load(new File((currentDirectory + "\\PDF\\input.pdf")));
// Create a PDF renderer
PDFRenderer pdfRenderer = new PDFRenderer(document);
// Iterate over each page in the PDF document
for (int pageNumber = 0; pageNumber < document.getNumberOfPages(); pageNumber++) {
// Render the PDF page to a BufferedImage
// BufferedImage image = pdfRenderer.renderImageWithDPI(pageNumber, 300,
// ImageType.RGB);
BufferedImage image = pdfRenderer.renderImageWithDPI(pageNumber, 200, ImageType.GRAY);
ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("LZW");
// 创建输出文件流
FileImageOutputStream output = new FileImageOutputStream(new File(currentDirectory + "\\TIF_LZW\\Oupput_" + (pageNumber + 1) + ".TIF"));
writer.setOutput(output);
// 写入压缩后的TIF图像
writer.write(null, new IIOImage(image, null, null), writeParam);
// 关闭输出流
output.close();
}
// Close the PDF document
document.close();
System.out.println("PDF converted to TIF successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
PDF转换为TIF(CCITT形式压缩,无法显示子的深浅) 代码
java
package com.sxz.study.pdf;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
public class TestPDFToTIFCompressCCITT {
public static void main(String[] args) {
try {
String currentDirectory = System.getProperty("user.dir");
System.out.println("----------------------");
System.out.println(currentDirectory );
// Load the PDF document
PDDocument document = PDDocument.load(new File((currentDirectory + "\\PDF\\input.pdf")));
// Create a PDF renderer
PDFRenderer pdfRenderer = new PDFRenderer(document);
// Iterate over each page in the PDF document
for (int pageNumber = 0; pageNumber < document.getNumberOfPages(); pageNumber++) {
// Render the PDF page to a BufferedImage
// BufferedImage image = pdfRenderer.renderImageWithDPI(pageNumber, 300,
// ImageType.RGB);
BufferedImage image = pdfRenderer.renderImageWithDPI(pageNumber, 200, ImageType.BINARY);
ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("CCITT T.4");
//writeParam.setCompressionQuality(1.0f);
// 创建输出文件流
FileImageOutputStream output = new FileImageOutputStream(new File(currentDirectory + "\\TIF_CCIT\\Oupput_" + (pageNumber + 1) + ".TIF"));
writer.setOutput(output);
// 写入压缩后的TIF图像
writer.write(null, new IIOImage(image, null, null), writeParam);
// 关闭输出流
output.close();
}
// Close the PDF document
document.close();
System.out.println("PDF converted to TIF successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.代码参考
java实现,PDF转图片(TIF,jpg)、PDF中写入内容_pdf转tif乱码 java-CSDN博客
5.下载地址
== 资源
==