我想提高图像的分辨率。我使用了PDFRenderer-0.9.0
jar。它是从java.net 下载的,用于将PDF页面转换为图像。
我想将46_2.pdf
PDF文件转换为image。转换后的46_2.png图像尺寸较小612 x 792 [ width x height ]
,
所以我想将图像尺寸增加到1200 x 1400 [ width x height]
。
我以前尝试过 PdfBox 将PDF页面转换为PNG图像文件。有个问题only page is converted but text is missing
。因此,我尝试使用 PdfRenderer 库进行图像转换。
码:
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 ConvertPdfPagesToImage {
public static void main(String[] args) {
try {
String sourceDir = "C:/PDFCopy/46_2.pdf";
String destinationDir = "C:/PDFCopy/";
File sourceFile = new File(sourceDir);
String fileName = sourceFile.getName().replace(".pdf", "");
if (sourceFile.exists()) {
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 = 1;
for (int i = 0; i < pdf.getNumPages(); i++) {
PDFPage page = pdf.getPage(i);
// 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);
// image width, // image 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);
ImageIO.write(bufferedImage, "png", new File( destinationDir + fileName +"_"+ pageNumber +".png"));
pageNumber++;
}
} else {
System.err.println(sourceFile.getName() +" File not exists");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
pageNumber
将从PDF文件46_2.pdf中选择的内容转换为具有所需尺寸的图像格式46_2.png。图像的分辨率随所需尺寸而增加。
如何将单个PDF页面转换为具有分辨率的png或jpeg图像格式。
码:
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 PdfToImageWithDimensions {
public static void main(String[] args) {
try {
String sourceDir = "C:/PDFCopy/46_2.pdf";// PDF file must be placed in DataGet folder
String destinationDir = "C:/PDFCopy/Converted/";//Converted PDF page saved in this folder
File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);
String fileName = sourceFile.getName().replace(".pdf", "");
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 = 1;// which PDF page to be convert
PDFPage page = pdf.getPage(pageNumber);
// image dimensions
int width = 1200;
int height = 1400;
// create the image
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage(width, 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(width, 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();
}
}
}
输出: 转换后的文件保存在C:\PDFCopy\Converted
文件夹中。下面是控制台输出。
46_2_1.png File created in: C:\PDFCopy\Converted
感谢Jeff Friesen基于您的jRebel
示例,我在pdfrenderer中尝试过
另一个解决方案:
如何在Java中使用PDF渲染器将所有PDF页面转换为png / jpeg / jpg / gif / bmp图像格式。待转换文件04-Request-
Headers.pdf
码:
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 ConvertAllPDFPagesToImageWithDimenstions {
public static void main(String[] args) {
try {
String sourceDir = "C:/Documents/04-Request-Headers.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", "");
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);
System.out.println("Total Pages: "+ pdf.getNumPages());
int pageNumber = 1;
for (int i = 0; i < pdf.getNumPages(); i++) {
PDFPage page = pdf.getPage(i);
// image dimensions
int width = 1200;
int height = 1400;
// create the image
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage(width, 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(width, 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);
pageNumber++;
System.out.println(imageFile.getName() +" File created in Folder: "+ destinationFile.getCanonicalPath());
}
} else {
System.err.println(sourceFile.getName() +" File not exists");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出: 所有文件都保存在“ C:/Documents/Converted/
文件夹”中。下面是控制台输出
Total Pages: 13
04-Request-Headers_1.png File created in Folder: C:\Documents\Converted
04-Request-Headers_2.png File created in Folder: C:\Documents\Converted
04-Request-Headers_3.png File created in Folder: C:\Documents\Converted
04-Request-Headers_4.png File created in Folder: C:\Documents\Converted
04-Request-Headers_5.png File created in Folder: C:\Documents\Converted
04-Request-Headers_6.png File created in Folder: C:\Documents\Converted
04-Request-Headers_7.png File created in Folder: C:\Documents\Converted
04-Request-Headers_8.png File created in Folder: C:\Documents\Converted
04-Request-Headers_9.png File created in Folder: C:\Documents\Converted
04-Request-Headers_10.png File created in Folder: C:\Documents\Converted
04-Request-Headers_11.png File created in Folder: C:\Documents\Converted
04-Request-Headers_12.png File created in Folder: C:\Documents\Converted
04-Request-Headers_13.png File created in Folder: C:\Documents\Converted
我看过使用PDFBox基于图像DPI提取图像的代码,如下所示 在上面的代码中,我可以指定图像分辨率(150),同时从pdf中提取图像。更高的分辨率,我得到更大的图像作为回报。 现在,我想反转它的意思是在将图像写入PDF时指定图像的分辨率/dpi,但下面的代码没有提供指定dpi的选项吗?有谁能指引我在哪里失踪 在将图像写入pdf时,请告诉我在哪里可以传递分辨率/DPI参数(因为图像大于pdf页面大小
问题内容: 我正在使用flyingsaucer通过一个servlet将xhtml文档呈现为pdf,该servlet返回生成的pdf文档。xhtml文档具有一个图像,该图像是从另一个servlet请求的。映像servlet在返回适当的映像之前检查谁登录。以下代码显示了如何请求图像: 我的问题是对图像的http请求来自pdf渲染器,而不是登录的用户,因此图像servlet不知道谁登录了,因此未返回所需
问题内容: 当我第一次问这个问题时,我尽可能地提供了很多信息,因为我不知道如果有人可以提供解决方案对他们有什么帮助,但是从答案来看,我似乎并没有明确说明我的意思。 我将原始文本留在底部,并将其缩短。我希望这一点很清楚。 我正在处理从另一个程序返回的图像。我可以得到一个图像,例如在72 DPI时为8x10或在16D20时也是72DPI的相同图像。 我在用Java工作。我想做的是拍摄16x20图像并调
本文向大家介绍如何提高组件的渲染效率呢?相关面试题,主要包含被问及如何提高组件的渲染效率呢?时的应答技巧和注意事项,需要的朋友参考一下 function Child({seconds}){ console.log('I am rendering'); return ( I am update every {seconds} seconds ) }; export default React.mem
问题内容: 我已经使用matplotlib绘制了一些实验结果。但是,通过单击图像右侧来保存图片会产生非常差的质量/低分辨率图像。 我正在寻找的示例图:示例图 问题答案: 您可以用来导出到图像文件: 另外,您可以为某些标量值指定参数,例如:
问题内容: 我正在为我的第一个深度Pyglet项目开发2D Minecraft克隆,但遇到了一个问题。每当我在屏幕上有相当数量的块时,帧速率都会急剧下降。 这是我的渲染方法:我使用字典,键为元组(代表块的坐标),项为纹理。 我遍历整个字典并渲染每个块: PS sx和sy是屏幕滚动的坐标偏移 我想知道是否有一种方法可以更有效地渲染每个块。 问题答案: 我将尽力解释为什么以及如何在不真正了解代码外观的