我在 PagePanel 中查看页面时遇到问题,尝试显示 pdf 时,出现错误“java.nio.BufferUnderflowException”。
错误:
java.nio.BufferUnderflowException at java.nio.HeapByteBuffer.get(Unknown Source) at com.sun.pdfview.decode.RunLengthDecode.decode(RunLengthDecode.java:64) at com.sun.pdfview.decode.RunLengthDecode.decode(RunLengthDecode .java:96) 在 com.sun.pdfview.decode.PDFDecoder.decodeStream(PDFDecoder.java:134) 在 com.sun.pdfview.PDFObject.decodeStream(PDFObject.java:347) 在 com.sun.pdfview.PDFObject。 getStream(PDFObject.java:261) at com.sun.pdfview.PDFObject.getStream(PDFObject.java:298) at com.sun.pdfview.PDFImage.getImage(PDFImage.java:306) at com.sun.pdfview.PDFRenderer .drawImage(PDFRenderer.java:274) 在 com.sun.pdfview.PDFImageCmd.execute(PDFPage.java:665) 在 com.sun.pdfview.PDFRenderer.iterate(PDFRenderer.java:577) 在 com.sun.pdfview。 BaseWatchable.run(BaseWatchable.java:101)在java.lang.Thread.run(未知来源)
代码:
public class Test {
public static void setup() throws IOException {
//set up the frame and panel
JFrame frame = new JFrame("PDF Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PagePanel panel = new PagePanel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
//load a pdf from a byte buffer
File file = new File("C:\\testeImpressora\\Print\\pdf\\tutorial.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// show the first page
PDFPage page = pdffile.getPage(0);
panel.showPage(page);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Test.setup();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}}