我正在尝试创建一个获取图像的类,它可以是bufferedimage,renderedimage或png,gif或jpeg类型的图像文件,并将其添加到已存在的PDF中。
我尝试过使用缓冲图像和渲染图像并将其传递给函数并将其添加到PDF但总是出现错误,所以我现在尝试将图像文件添加到PDF中并且我一直在遇到不同的错误我更改任何代码时的运行时。
我得到的当前错误是:
线程“AWT-EventQueue-0”中的异常java.lang.IndexOutOfBoundsException
我目前正在尝试使用的代码是:
public ExportToPDF() throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("pdf.pdf"));
java.awt.Image image1 = ImageIO.read(new File("test.gif"));
Document doc = new Document(pdfDoc);
pdfDoc.addNewPage(PageSize.A4);
doc.add(image1);
doc.close();
}
到目前为止,我花了好几个小时才认真地找不到工作方式,任何帮助都会非常感激。我已经搜索了互联网寻找帮助,我看到的唯一方法是我没有尝试使用.getInstance然而当我尝试使用它时说它无法解析符号。
任何帮助都会受到重视。
完整堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException
at java.io.RandomAccessFile.readBytes(Native Method)
at java.io.RandomAccessFile.read(RandomAccessFile.java:377)
at javax.imageio.stream.FileImageInputStream.read(FileImageInputStream.java:117)
at com.sun.imageio.plugins.common.SubImageInputStream.read(SubImageInputStream.java:61)
at com.sun.imageio.plugins.common.InputStreamAdapter.read(InputStreamAdapter.java:47)
at java.io.SequenceInputStream.read(SequenceInputStream.java:207)
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:238)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1112)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1223)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1338)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1614)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1308)
at CE201.ExportToPDF.<init>(ExportToPDF.java:26)
at CE201.CsvGUI$7.actionPerformed(CsvGUI.java:256)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我用来生成PNG文件的代码如下:
public void actionPerformed(ActionEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
WritableImage img = fxPanel.getScene().snapshot(null);
File file = new File("test.png");
RenderedImage renderedImage = SwingFXUtils.fromFXImage(img, null);
try {
ImageIO.write(renderedImage, "PNG", file);
} catch (IOException e1) {
e1.printStackTrace();
}
}