我正在尝试使用PDFBox将多页tiff转换为pdf,但未成功。我无法在公司中使用apache Imaging-commons,因为它不是一个稳定的版本。
问题::无法读取多个TIFF文件并写入PDF。
到目前为止有效的解决方案::仅首页被写入并保存为pdf。当tiff是单个页面时,它也可以工作。
下面是代码:
PDDocument doc = new PDDocument();
log.info("Read Image");
log.info("Process Image parts");
//Get the number of pages
int pages = 0;
try(ImageInputStream imageInputStream = ImageIO.createImageInputStream(new File("src/main/resources/output/testpdf.tiff"))) {
if (imageInputStream != null && imageInputStream.length() != 0) {
Iterator<ImageReader> iteratorIO = ImageIO.getImageReaders(imageInputStream);
if (iteratorIO != null && iteratorIO.hasNext()) {
ImageReader reader = iteratorIO.next();
reader.setInput(imageInputStream);
pages = reader.getNumImages(true);
log.info("Number of pages in the tiff is " + pages);
}
}
}
///需要其他页面的读者吗?
for (int i=0; i<pages; i++) {
BufferedImage bimage = ImageIO.read(file);
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
try {
// the .08F can be tweaked. Go up for better quality,
// but the size of the PDF will increase
PDImageXObject image = JPEGFactory.createFromImage(doc, bimage, 0.08f);
Dimension scaledDim = getScaledDimension(new Dimension(image.getWidth(), image.getHeight()),
new Dimension((int) page.getMediaBox().getWidth(), (int) page.getMediaBox().getHeight()));
contentStream.drawImage(image, 1, 1, scaledDim.width, scaledDim.height);
} finally {
contentStream.close();
}
}
doc.save("src/main/resources/output/testpdf.pdf");
doc.close();
我需要拿出ImageIO不提供的阅读器吗? 要么 我需要将tiff多页拆分为多个页面,然后再写入pdf吗?
我在图像处理方面工作不多,但是在转换过程之后欣赏ImageIO的质量水平!
谢谢
答案 0 :(得分:3)
尝试一下,您需要PDFBox jar和sun.jai.codec jar
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
public class FinalTtoP {
public static void main(String args[]) throws IOException
{
PDDocument document=new PDDocument();
File file = new File("C:/nn.tif"); //Enter Tiff file path
ImageInputStream isb = ImageIO.createImageInputStream(file);
Iterator<ImageReader> iterator = ImageIO.getImageReaders(isb);
if (iterator == null || !iterator.hasNext())
{
throw new IOException("Image file format not supported by ImageIO: ");
}
ImageReader reader = (ImageReader) iterator.next();
iterator = null;
reader.setInput(isb);
int nbPages = reader.getNumImages(true);
System.out.println(nbPages);
for(int p=0;p<nbPages;p++)
{
BufferedImage bufferedImage = reader.read(p);
PDPage page = new PDPage();
document.addPage(page);
PDImageXObject i = LosslessFactory.createFromImage(document, bufferedImage);
PDPageContentStream content =new PDPageContentStream(document, page);
content.drawImage(i, 0,0 ,page.getMediaBox().getWidth(),page.getMediaBox().getHeight());
content.close();
}
document.save("C:/nnnnm.pdf"); //Enter path to save your file with .pdf extension
document.close();
}
}
答案 1 :(得分:1)
参考这个代码会提高速度,但仍然很慢,需要使用itext。
public static byte[] convertTiffToPdf(File tiffFile) throws IOException {
ByteArrayOutputStream outStream = null;
PDDocument document = null;
ImageInputStream imgInputStream = null;
try {
outStream = new ByteArrayOutputStream();
document = new PDDocument();
PDRectangle pageSize = PDRectangle.LETTER;
int noOfPages = 0;
imgInputStream = ImageIO.createImageInputStream(tiffFile);
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imgInputStream);
if (iterator == null || !iterator.hasNext()) {
throw new IOException("Image file format not supported by ImageIO: ");
}
ImageReader reader = (ImageReader) iterator.next();
iterator = null;
reader.setInput(imgInputStream);
noOfPages = reader.getNumImages(true);
for (int i = 0; i < noOfPages; i++) {
PDPageContentStream content = null;
try {
BufferedImage bufferedImage = reader.read(i);
PDPage page = new PDPage(pageSize);
document.addPage(page);
// PDImageXObject imgObject = LosslessFactory.createFromImage(document, bufferedImage); //Commented for PR 1028
PDImageXObject imgObject = CCITTFactory.createFromFile(document, tiffFile, i); //PR 1028
//PDImageXObject imgObject = JPEGFactory.createFromImage(document, bufferedImage);
content = new PDPageContentStream(document, page);
content.drawImage(imgObject, 0, 0, pageSize.getWidth(), pageSize.getHeight());
} catch(Exception e) {
e.printStackTrace();
} finally {
content.close();
}
}
document.save(outStream);
byte[] fileBytes = outStream.toByteArray();
return fileBytes;
} finally {
if (document != null) {
document.close();
}
if (imgInputStream != null) {
imgInputStream.close();
}
if (outStream != null) {
outStream.close();
}
}
}
答案 2 :(得分:0)
您可以使用CCITTFactory.createFromFile(PDDocument document, File file, int number)
,它适用于大多数双色调tiff文件。如果那不起作用(因为TIFF文件是平铺的或彩色的),则将各个页面读入BufferedImage对象(请参见here),然后将LosslessFactory.createFromImage(PDDocument document, BufferedImage image)
与结果一起使用。