gpt4 book ai didi

android - 将 PDF 转换为图像(具有适当的格式)

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:49 25 4
gpt4 key购买 nike

我有一个pdf文件(附件)。我的目标是按原样使用 pdfbox 将 pdf 转换为图像(与在 Windows 中使用截图工具相同)。pdf 有各种形状和文字。

我正在使用以下代码:

PDDocument doc = PDDocument.load("Hello World.pdf");
PDPage firstPage = (PDPage) doc.getDocumentCatalog().getAllPages().get(67);
BufferedImage bufferedImage = firstPage.convertToImage(imageType,screenResolution);
ImageIO.write(bufferedImage, "png",new File("out.png"));

This is the PDF i want to convert

当我使用代码时,图像文件给出了完全错误的输出(附加的 out.png) This is the image file converted from pdfbox

我如何让 pdfbox 拍摄类似直接快照图像的东西?

另外,我注意到png的图像质量不太好,有什么办法可以提高生成图像的分辨率吗?

编辑:这是pdf(见第68页) https://drive.google.com/file/d/0B0ZiP71EQHz2NVZUcElvbFNreEU/edit?usp=sharing

编辑 2:似乎所有的文字都消失了。我也尝试使用 PDFImageWriter 类

test.writeImage(doc, "png", null, 68, 69, "final.png",TYPE_USHORT_GRAY,200 );

同样的结果

最佳答案

使用 PDFRenderer 可以将 PDF 页面转换为图像格式。

使用 PDF 渲染器在 java 中将 PDF 页面转换为图像。需要 jar PDFRenderer-0.9.0

package com.pdfrenderer.examples;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.imageio.ImageIO;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class PdfToImage {
public static void main(String[] args) {
try {
String sourceDir = "C:/Documents/Chemistry.pdf";// PDF file must be placed in DataGet folder
String destinationDir = "C:/Documents/Converted/";//Converted PDF page saved in this folder

File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);

String fileName = sourceFile.getName().replace(".pdf", "_cover");

if (sourceFile.exists()) {
if (!destinationFile.exists()) {
destinationFile.mkdir();
System.out.println("Folder created in: "+ destinationFile.getCanonicalPath());
}

RandomAccessFile raf = new RandomAccessFile(sourceFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
int pageNumber = 62;// which PDF page to be convert
PDFPage page = pdf.getPage(pageNumber);

System.out.println("Total pages:"+ pdf.getNumPages());

// create the image
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);

// width & height, // clip rect, // null for the ImageObserver, // fill background with white, // block until drawing is done
Image image = page.getImage(rect.width, rect.height, rect, null, true, true );
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, null);

File imageFile = new File( destinationDir + fileName +"_"+ pageNumber +".png" );// change file format here. Ex: .png, .jpg, .jpeg, .gif, .bmp

ImageIO.write(bufferedImage, "png", imageFile);

System.out.println(imageFile.getName() +" File created in: "+ destinationFile.getCanonicalPath());
} else {
System.err.println(sourceFile.getName() +" File not exists");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

转换图像:

Chemistry_cover_62

关于android - 将 PDF 转换为图像(具有适当的格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22332791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com