Pdf Viewer

Ads

 
 

Share on Google+Share on Google+

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

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

Ads
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


Ads









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
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
Advertisements
PDF to Image
PDF to Image  Java code to convert PDF to Image
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
Version of com.groupdocs>groupdocs-viewer dependency
List of Version of com.groupdocs>groupdocs-viewer dependency
Version of com.klinkerapps>article_viewer dependency
List of Version of com.klinkerapps>article_viewer dependency
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
pdf marking
pdf marking  Hi i am working on online answer sheet evaluation using jsp. It need pdf marking.drag and drop images onto pdf. please help me regards reshma
pdf marking
pdf marking  Hi i am working on online answer sheet evaluation using jsp. It need pdf marking.drag and drop images onto pdf. please help me regards reshma
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 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
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
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
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
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
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
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
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
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
open pdf in uiwebview
open pdf in uiwebview  Hi, How to open pdf in uiwebview? Thanks
Download PDF file
Download PDF file  How to download PDF file with JSF
how to load pdf on html
how to load pdf on html  how to load pdf on html
Version of com.groupdocs>groupdocs-viewer-parent dependency
List of Version of com.groupdocs>groupdocs-viewer-parent dependency
Creating PDF in JAVA
Creating PDF in JAVA  How create pdf in java ? Take value from database for particular PDF
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
Maven Dependency groupdocs-viewer >> 17.2.0
You should include the dependency code given in this page to add Maven Dependency of com.groupdocs >> groupdocs-viewer version17.2.0 in your project
Maven Dependency groupdocs-viewer >> 3.2.0
You should include the dependency code given in this page to add Maven Dependency of com.groupdocs >> groupdocs-viewer version3.2.0 in your project
Maven Dependency groupdocs-viewer >> 3.2.2
You should include the dependency code given in this page to add Maven Dependency of com.groupdocs >> groupdocs-viewer version3.2.2 in your project
Maven Dependency groupdocs-viewer >> 3.7.0
You should include the dependency code given in this page to add Maven Dependency of com.groupdocs >> groupdocs-viewer version3.7.0 in your project