Dalam membangun sebuah sistem terkadang di butuhkan sebuah keperluan untuk menampilkan dakument berekstensi pdf. Kali ini saya akan membahas bagaimana cara menampilkan document pdf di Java. sebelumnya kita membutuhkan beberapa librari icepdf-core & icepdf-viewer.
Langkah-langkah
1.Buatlah projek dengan nama terserah anda lalu buatlah sebuah Jframeform dan menggunakan Jscrolepane
2.buatlah fungsi di source code untuk menampilkan pdfnya seperti berikut
void openpdf(String file){
try {
SwingController control=new SwingController();
SwingViewBuilder factry=new SwingViewBuilder(control);
JPanel veiwerCompntpnl=factry.buildViewerPanel();
ComponentKeyBinding.install(control, veiwerCompntpnl);
control.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
control.getDocumentViewController()));
control.openDocument(file);
jScrollPane2.setViewportView(veiwerCompntpnl);
} catch (Exception ex) {
}
}
3. agar kita dapat menggunakan fungsi tersebut masukan coding openpdf("src/report/Umum.pdf");
di bawah initComponents(); anda dapat mencarinya menggunakan ctrl+f
Berikut adalah Keseluruhan dari Codingnya
/*Untuk mempermudah lagi saya menyediakan File *.form dan File File *.java
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package view;
import com.lowagie.text.pdf.ArabicLigaturizer;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Event;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.icepdf.ri.common.ComponentKeyBinding;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
/**
*
* @author User
*/
public class Help1 extends javax.swing.JFrame {
/**
* Creates new form Help
*/
public Help1() {
initComponents();
openpdf("src/report/Umum.pdf");
this.setExtendedState(MAXIMIZED_BOTH);
}
/**
* 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() {
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 363, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 449, Short.MAX_VALUE)
);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/background1.jpg"))); // NOI18N
jLabel1.setToolTipText("");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Help");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @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(Help1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Help1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Help1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Help1.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() {
new Help1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane2;
// End of variables declaration
void openpdf(String file){
try {
SwingController control=new SwingController();
SwingViewBuilder factry=new SwingViewBuilder(control);
JPanel veiwerCompntpnl=factry.buildViewerPanel();
ComponentKeyBinding.install(control, veiwerCompntpnl);
control.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
control.getDocumentViewController()));
control.openDocument(file);
jScrollPane2.setViewportView(veiwerCompntpnl);
} catch (Exception ex) {
}
}
}
Terima Kasih
0 Comments