How to diplay the pdf files in java panel using scrollpane...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
class PDFViewer
{
public static void main(String[] args)
{
final JFrame f=new JFrame();
f.setLayout(null);
JLabel label=new JLabel("Select File");
final JTextField text=new JTextField(20);
final JTextArea area=new JTextArea(20,10);
final JScrollPane pane=new JScrollPane(area);
JButton b1=new JButton("Browse");
JButton b2=new JButton("Show Data");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JFileChooser fileChooser = new JFileChooser(new File("C:/"));
fileChooser.addChoosableFileFilter(new MyFilter());
fileChooser.showOpenDialog(f);
File f=fileChooser.getSelectedFile();
String path=f.getPath();
text.setText(path);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
File f=new File(text.getText());
try {
Document document = new Document();
document.open();
PdfReader reader = new PdfReader(f.getPath());
PdfDictionary dictionary = reader.getPageN(1);
PRIndirectReference reference = (PRIndirectReference)
dictionary.get(PdfName.CONTENTS);
PRStream stream = (PRStream) PdfReader.getPdfObject(reference);
byte[] bytes = PdfReader.getStreamBytes(stream);
PRTokeniser tokenizer = new PRTokeniser(bytes);
StringBuffer buffer = new StringBuffer();
while (tokenizer.nextToken()) {
if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
buffer.append(tokenizer.getStringValue());
buffer.append("\n");
}
}
String test=buffer.toString();
area.append(test);
document.close();
}
catch (Exception ex) {}
pane.setVisible(true);
}
});
label.setBounds(10,10,100,20);
text.setBounds(120,10,120,20);
b1.setBounds(260,10,100,20);
b2.setBounds(380,10,100,20);
pane.setBounds(120,40,360,100);
pane.setVisible(false);
f.add(label);
f.add(text);
f.add(b1);
f.add(b2);
f.add(pane);
f.setVisible(true);
f.setSize(500,300);
}
}
class MyFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
String filename = file.getName();
return filename.endsWith(".pdf");
}
public String getDescription() {
return "*.pdf";
}
}
For the above code, you need itext api