Skip to main content

Error com.sun.pdfview.PDFParseException: Expected 'xref' at start of table

No replies
udaykiranpulipati
Offline
Joined: 2014-01-11
Points: 0

I am using pdfrenderer 1.0.5 jar.
When I convert a pdf file into image, following exception is occurred.

com.sun.pdfview.PDFParseException: Expected 'xref' at start of table
at com.sun.pdfview.PDFFile.readTrailer(PDFFile.java:974)
at com.sun.pdfview.PDFFile.parseFile(PDFFile.java:1175)
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:126)
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:102)
at com.pdfrenderer.maven.PdfToImage.main(PdfToImage.java:29)

Code:

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:/PDFCopy/1.pdf";
String destinationDir = "C:/PDFCopy/";

File pdfFile = new File(sourceDir);
if (pdfFile.exists()) {
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
PDFPage page = pdf.getPage(0);

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

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

// 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);
ImageIO.write(bufferedImage, "png", new File( destinationDir + fileName +".png" ));
} else {
System.err.println(pdfFile.getName()+"> File not exists");
}

} catch (Exception e) {
e.printStackTrace();
}
}
}
AttachmentSize
1.pdf101.53 KB