Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


vaithiyanathan
Pdf Viewer
1 Answer(s)      2 years and 9 months ago
Posted in : Java Beginners


How to diplay the pdf files in java panel using scrollpane...


Advertisement
View Answers

March 15, 2012 at 5:58 PM


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



Related Tutorials/Questions & Answers:
Pdf Viewer
Pdf Viewer   How to diplay the pdf files in java panel using... = file.getName(); return filename.endsWith(".pdf"); } public String getDescription() { return "*.pdf"; } } For the above code, you
The Java Applet Viewer
The Java Applet Viewer       Applet viewer is a command line program to run Java applets... viewer. This is a tool that acts as a test bed for Java applets. The working
Advertisements
DB Schema Viewer
DB Schema Viewer       DB Schema Viewer is an Eclipse plugin that builds a graphical view of an existing database. Reverse Engineering is performed through JDBC
TightVNC Java Viewer connection
TightVNC Java Viewer connection  I have source code of the tight VNC and installed 'Real VNC' on my local system. When I run the source of tight VNC, it asked for the host and port so I passed following credentials: Host
Filter Implementation In flexible hierarchy Tree Viewer
Filter Implementation In flexible hierarchy Tree Viewer    I am implementing a tree viewer using flexible hierarchy Tree Viewer. I followed the DSF examples and could successfully implement the tree viewer. Now, I want
Filter Implementation In flexible hierarchy Tree Viewer
Filter Implementation In flexible hierarchy Tree Viewer    I am implementing a tree viewer using flexible hierarchy Tree Viewer. I followed the DSF examples and could successfully implement the tree viewer. Now, I want
pdf from i report
pdf from i report  I have a problem again with my report, i use I Report to make pdf file. but when the application running in java, pdf can't view.../not viewer in Jasperviewer...., thank you, hendra
DB Schema Viewer
DB Schema Viewer       DB Schema Viewer is an Eclipse plugin that builds a graphical view of an existing database. Reverse Engineering is performed through JDBC
The Java Applet Viewer
The Java Applet Viewer       Applet viewer is a command line program to run Java applets.... The other way to run an applet is through Java applet viewer. This is a tool
The Java Applet Viewer
The Java Applet Viewer       Applet viewer is a command line program to run Java applets... viewer. This is a tool that acts as a test bed for Java applets. The working
Setting Applet Viewer Properties - Java Beginners
Setting Applet Viewer Properties  Hi, I'm developing an applet using netbeans 6.1 ide. I want to set the appletviewer's width and height so I can test the applet from the ide and not from the browser through a html page. How
The Java Applet Viewer
The Java Applet Viewer       Applet viewer is a command line program to run Java applets.... The other way to run an applet is through Java applet viewer. This is a tool
PDF to Image
PDF to Image  Java code to convert PDF to Image
pdf generation.
pdf generation.  i want to generate the data which is stored in mysql data base in pdf format with php. how i will do
pdf to text
pdf to text  how to covert pdf file (which contain table and text) into word or excel file using itext api
pdf to database
pdf to database  Hi, I want to read the data from pdf(pdf file is having 50 fields) which is placed in database file and store that into MySQL database. I want this process untill the rows completed in the database file
PDF document
PDF document  hello, How to Open a PDF document on iPhone??   You can use these There's a whole toolkit built in which lets you render PDF pages to a UIView. Check out: CGPDFDocumentCreateWithURL
upload pdf
upload pdf   i want to dispal content of pdf fil and stored into database in human readable form using php . how can i do
PDF Comparator
PDF Comparator  Hi Guys, I need to develop a program which should compare a set of pdf files stored in one folder with a set of pdf files stored in another folder. Both folders should contain the same no. of pdf files with same
itext pdf
itext pdf  i am generating pdf using java i want to do alignment in pdf using java but i found mostly left and right alignment in a row. i want to divide single row in 4 parts then how can i do
pdf restriction
pdf restriction  i have certain pdf files that have restrictions on it as copy ,read,extract text etc.. i want to remove restriction by java code. so is there any way to do this? plz help thanks in advance rohit
Write to PDF
Write to PDF  Hi, Fairly new to this. I want to create a PDF file as a server call, with text/images sent to the app. Seeing that this will have to be a plugin, how do I go about making it a valid plugin for the server
how to load pdf on html
how to load pdf on html  how to load pdf on html
Download PDF file
Download PDF file  How to download PDF file with JSF
open pdf in uiwebview
open pdf in uiwebview  Hi, How to open pdf in uiwebview? Thanks
pdf to voice converter
pdf to voice converter  is it possible to implement PDF to speech converter by extracting text from pdf and then text to speech
Creating PDF in JAVA
Creating PDF in JAVA  How create pdf in java ? Take value from database for particular PDF
PDF Generation for unicode characters
PDF Generation for unicode characters  Hi, How a PDF file having unicode characters is generated in JSP
pdf format - JSP-Servlet
pdf format  hi Sir, how to convert text format into PDF format. thanks & regards, vijayababu.m
download pdf files
download pdf files  pls help me,I don't know how to convert .doc,.docx files into pdf files and download that pdf files using servlet or jsp
Advertisements
 

 

 

DMCA.com