我正在尝试使用命令行程序转换将PDF转换为图像(JPEG或PNG)。这是我正在转换的pdf文件之一。
我想让程序去掉多余的空白,并返回足够高质量的图像,以便上标可以轻松读取。
这是我目前最好的尝试。正如你所看到的,修剪工作很好,我只是需要锐化的分辨率相当多。这是我正在使用的命令:
convert -trim 24.pdf -resize 500% -quality 100 -sharpen 0x1.0 24-11.jpg
我试着做了以下有意识的决定:
调整它的大小(对分辨率没有影响)
尽可能提高质量
使用-锐化(我已经尝试了一系列值)
任何建议,请在最终的PNG/JPEG图像的分辨率更高,将非常感谢!
我使用开源java pdf引擎icepdf。检查办公室演示。
package image2pdf;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class pdf2image {
public static void main(String[] args) {
Document document = new Document();
try {
document.setFile("C:\\Users\\Dell\\Desktop\\test.pdf");
} catch (PDFException ex) {
System.out.println("Error parsing PDF document " + ex);
} catch (PDFSecurityException ex) {
System.out.println("Error encryption not supported " + ex);
} catch (FileNotFoundException ex) {
System.out.println("Error file not found " + ex);
} catch (IOException ex) {
System.out.println("Error IOException " + ex);
}
// save page captures to file.
float scale = 1.0f;
float rotation = 0f;
// Paint each pages content to an image and
// write the image to file
for (int i = 0; i < document.getNumberOfPages(); i++) {
try {
BufferedImage image = (BufferedImage) document.getPageImage(
i, GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = image;
try {
System.out.println(" capturing page " + i);
File file = new File("C:\\Users\\Dell\\Desktop\\test_imageCapture1_" + i + ".png");
ImageIO.write(rendImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
}catch(Exception e){
e.printStackTrace();
}
}
// clean up resources
document.dispose();
}
}
我也尝试过imagemagick和pdftoppm, pdftoppm和icepdf的分辨率都比imagemagick高。
这里的Linux用户:我尝试了转换命令行实用程序(PDF到PNG),我对结果不满意。我发现这样做更简单,效果也更好:
使用PDFTK提取PDF页面
例如:PDFTK file.pdf cat 3输出page3.pdf
用GIMP打开(导入)该pdf
重要:改变导入分辨率从100到300或600像素/英寸
在GIMP导出为PNG(更改文件扩展名为。PNG)
编辑:
根据评论的要求,添加了图片。转换命令使用:
Convert -density 300 -trim struct2vec.pdf -quality 100 struct2vec.png
GIMP: 300dpi进口(px/in);导出为PNG压缩级别3。
我没有在命令行上使用GIMP(回复:我的评论,在下面)。
我使用的是pdf2image。一个简单的python库,工作起来很有魅力。
首先在非linux机器上安装poppler。你可以下载压缩包。在“程序文件”中解压,并将bin添加到“机器路径”中。
之后,你可以像这样在python类中使用pdf2image:
from pdf2image import convert_from_path, convert_from_bytes
images_from_path = convert_from_path(
inputfile,
output_folder=outputpath,
grayscale=True, fmt='jpeg')
我不擅长python,但能够使它的exe。
稍后,您可以使用带有文件输入和输出参数的exe。我已经在c#中使用了它,事情工作得很好。
图像质量好。OCR工作正常。