pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: can not get file to display
Date Sat, 26 Dec 2015 21:48:51 GMT
Here's some 1.8 code that "almost" works, i.e. it does display a PDF 
page from a fixed file. There's still a problem with the GUI elements, 
the two "super.add()" are probably not correct. It is reduced from your 
code.

- don't close PDF file
- static object
- loadNonSeq

==============

/*
  * To change this license header, choose License Headers in Project 
Properties.
  * To change this template file, choose Tools | Templates
  * and open the template in the editor.
  */
package pdfhighlighter.gui;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdfviewer.PDFPagePanel;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

/**
  *
  * @author gerry
  */
public class pdfViewer extends javax.swing.JFrame
{

     /**
      * Creates new form pdfViewer
      */
     public pdfViewer()
     {
         super();
         initComponents();
     }

     public void Screen(PDPage testPage) throws IOException
     {
         PDFPagePanel pagePanel = new PDFPagePanel();
         pagePanel.setPage(testPage);
         pagePanel.getFont();
         pagePanel.setVisible(true);

         viewingPanel.add(pagePanel);
         viewingPanel.setBounds(40, 40, pagePanel.getWidth(), 
pagePanel.getHeight());
         viewingPanel.setVisible(true);
         super.add(pagePanel);
         super.add(viewingPanel);

     }

     /**
      * This method is called from within the constructor to initialize 
the form. WARNING: Do NOT
      * modify this code. The content of this method is always 
regenerated by the Form Editor.
      */
     @SuppressWarnings("unchecked")
     // <editor-fold defaultstate="collapsed" desc="Generated Code">
     private void initComponents()
     {

         viewingPanel = new javax.swing.JScrollPane();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

         javax.swing.GroupLayout layout = new 
javax.swing.GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addComponent(viewingPanel, 
javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
         );
         layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addComponent(viewingPanel, 
javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
         );

         pack();
     }// </editor-fold>

     static PDDocument inputPDF;

     /**
      * @param args the command line arguments
      */
     public static void main(String args[])
     {
         /* Set the Nimbus look and feel */
         //<editor-fold defaultstate="collapsed" desc=" Look and feel 
setting code (optional) ">
         /* If Nimbus (introduced in Java SE 6) is not available, stay 
with the default look and feel.
          * For details see 
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
          */
         try
         {
             for (javax.swing.UIManager.LookAndFeelInfo info : 
javax.swing.UIManager.getInstalledLookAndFeels())
             {
                 if ("Nimbus".equals(info.getName()))
                 {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
                     break;
                 }
             }
         }
         catch (ClassNotFoundException ex)
         {
java.util.logging.Logger.getLogger(pdfViewer.class.getName()).log(java.util.logging.Level.SEVERE,

null, ex);
         }
         catch (InstantiationException ex)
         {
java.util.logging.Logger.getLogger(pdfViewer.class.getName()).log(java.util.logging.Level.SEVERE,

null, ex);
         }
         catch (IllegalAccessException ex)
         {
java.util.logging.Logger.getLogger(pdfViewer.class.getName()).log(java.util.logging.Level.SEVERE,

null, ex);
         }
         catch (javax.swing.UnsupportedLookAndFeelException ex)
         {
java.util.logging.Logger.getLogger(pdfViewer.class.getName()).log(java.util.logging.Level.SEVERE,

null, ex);
         }
         //</editor-fold>

         /* Create and display the form */
         java.awt.EventQueue.invokeLater(new Runnable()
         {
             public void run()
             {
                 try
                 {
                     inputPDF = PDDocument.loadNonSeq(new 
File("blah.pdf"), null);
                     List<PDPage> allPages = 
inputPDF.getDocumentCatalog().getAllPages();
                     PDPage testPage = allPages.get(0);
                     pdfViewer pdfviewer = new pdfViewer();
                     pdfviewer.Screen(testPage);
                     pdfviewer.setVisible(true);
                 }
                 catch (IOException ex)
                 {
Logger.getLogger(pdfViewer.class.getName()).log(Level.SEVERE, null, ex);
                 }
             }
         });
     }

     // Variables declaration - do not modify
     private javax.swing.JScrollPane viewingPanel;
     // End of variables declaration
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message