Basic code to display a pdf in an existing JPanel?

前端 未结 3 548
情话喂你
情话喂你 2020-12-09 22:14

I have an existing interface that has a JPanel for displaying pdf files.

It is important to display the pdf inside this inteface and not open a new window. How can I

3条回答
  •  遥遥无期
    2020-12-09 22:47

    I think the best alternative is to use ICEpdf.

    The ICEpdf API is 100% Java, lightweight, fast, efficient, and very easy to use.

    ICEpdf can be used as standalone open source Java PDF viewer, or can be easily embedded in any Java application to seamlessly load or capture PDF documents. Beyond PDF document rendering, ICEpdf is extremely versatile, and can be used in a multitude of innovative ways.

    In your project managed with Maven you could include:

    
            
                org.icepdf.os
                icepdf-core
                ${icepdf.version}
                
                    
                        javax.media
                        jai_core
                    
                
            
    
            
            
                org.icepdf.os
                icepdf-viewer
                ${icepdf.version}
            
    

    Then, you could use a code similar to the following to visualize the pdf in a panel:

    // Instance the controller
    controller = new SwingController();
    // We created the SwingViewFactory configured with the controller
    SwingViewBuilder factory = new SwingViewBuilder(controller);
    // We use the factory to build a preconfigured JPanel
    // with a full and active viewer user interface.
    viewerComponentPanel = factory.buildViewerPanel();
    viewerComponentPanel.setPreferredSize(new Dimension(400, 243));
    viewerComponentPanel.setMaximumSize(new Dimension(400, 243));
    // We add keyboard command
    ComponentKeyBinding.install(controller, viewerComponentPanel);
    // add interactive mouse link annotation support via callback
    controller.getDocumentViewController().setAnnotationCallback(
                  new org.icepdf.ri.common.MyAnnotationCallback(
                         controller.getDocumentViewController()));
    
    // We add the component to visualize the report
    reportViewerContainer.add(viewerComponentPanel, BorderLayout.CENTER);
    reportViewerContainer.invalidate();
    // We open the generated document
    controller.openDocument(reportLocationUri.toURL());
    

    As a result you could get something like the following:

    Hope this can help you.

提交回复
热议问题