0

以下のリンクをたどってPDFファイルを表示しています。 JavaSwingPDFビューア

JFrame次に、 (ナビゲーションボタンのほかに)検索ボタンを追加する必要があります。その検索ボタンをクリックすると、テキストフィールドのあるラベルが開きます。クライアントがそのテキストフィールドに値を入力すると、PDF全体で検索を実行し、一致した単語の数をテキストフィールドに表示する必要があります。私はスイングして3日から解決策を見つけようとしているのは非常に新しいです。しかし、私は正確な解決策を得ることができませんでした。それは私にとって非常に緊急です。どんな助けでも大歓迎です。

最初の編集:

public class PdfViewer extends JPanel {
    private static enum Navigation {
        GO_FIRST_PAGE, FORWARD, BACKWARD, GO_LAST_PAGE, GO_N_PAGE
    }

    private static final CharMatcher POSITIVE_DIGITAL = CharMatcher.anyOf("0123456789");
    private static final String GO_PAGE_TEMPLATE = "%s of %s";
    private static final int FIRST_PAGE = 1;
    private int currentPage = FIRST_PAGE;
    private JButton btnFirstPage;
    private JButton btnPreviousPage;
    private JTextField txtGoPage;
    private JButton btnNextPage;
    private JButton btnLastPage;
    private JButton print;
    private JButton search;
    private PagePanel pagePanel;
    private PDFFile pdfFile;

    public PdfViewer() {
        initial();
    }

    private void initial() {
        setLayout(new BorderLayout(0, 0));
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(topPanel, BorderLayout.NORTH);
        btnFirstPage = createButton("|<<");
        topPanel.add(btnFirstPage);
        btnPreviousPage = createButton("<<");
        topPanel.add(btnPreviousPage);
        txtGoPage = new JTextField(10);
        txtGoPage.setHorizontalAlignment(JTextField.CENTER);
        topPanel.add(txtGoPage);
        btnNextPage = createButton(">>");
        topPanel.add(btnNextPage);
        btnLastPage = createButton(">>|");
        topPanel.add(btnLastPage);
        
        search = new JButton("search");
        topPanel.add(search);
        JScrollPane scrollPane = new JScrollPane();
        add(scrollPane, BorderLayout.CENTER);
        JPanel viewPanel = new JPanel(new BorderLayout(0, 0));
        scrollPane.setViewportView(viewPanel);

        pagePanel = new PagePanel();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        pagePanel.setPreferredSize(screenSize);
        viewPanel.add(pagePanel, BorderLayout.CENTER);

        disableAllNavigationButton();

        btnFirstPage.addActionListener(new PageNavigationListener(Navigation.GO_FIRST_PAGE));
        btnPreviousPage.addActionListener(new PageNavigationListener(Navigation.BACKWARD));
        btnNextPage.addActionListener(new PageNavigationListener(Navigation.FORWARD));
        btnLastPage.addActionListener(new PageNavigationListener(Navigation.GO_LAST_PAGE));
        txtGoPage.addActionListener(new PageNavigationListener(Navigation.GO_N_PAGE));
        
        search.addActionListener(new Action1());
    }
    
    static class Action1 implements ActionListener {        
          public void actionPerformed (ActionEvent e) {  
              JFrame parent = new JFrame();
              JDialog jDialog = new JDialog();
              Label label = new Label("Search: ");
              final JTextField jTextField = new JTextField(10);
              JPanel panel = new JPanel();
              parent.add(panel);
              panel.add(label);
              panel.add(jTextField);
              parent.setVisible(true);
              parent.setSize(800,400);
              parent.setLocationRelativeTo(null);
          }
        }   
}

これは私が変更したコードです。残りのコードはリンクにあるものと同じです。このコードを使用すると、JFrameに検索ボタンが表示されます。そのボタンをクリックすると、ラベル名として検索とテキストフィールドが表示されたウィンドウが開きます。そのテキストフィールドに値を入力すると、テキストフィールドの値を使用してPDF全体を検索し、そのテキストフィールドに一致する数を表示する必要があります。

2番目の編集:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.google.common.base.CharMatcher;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PagePanel;

public class PdfViewer extends JPanel {
    private static enum Navigation {
        GO_FIRST_PAGE, FORWARD, BACKWARD, GO_LAST_PAGE, GO_N_PAGE
    }

    private static final CharMatcher POSITIVE_DIGITAL = CharMatcher.anyOf("0123456789");
    private static final String GO_PAGE_TEMPLATE = "%s of %s";
    private static final int FIRST_PAGE = 1;
    private int currentPage = FIRST_PAGE;
    private JButton btnFirstPage;
    private JButton btnPreviousPage;
    private JTextField txtGoPage;
    private JButton btnNextPage;
    private JButton btnLastPage;
    private JButton print;
    private JButton search;
    private PagePanel pagePanel;
    private static PDFFile pdfFile;

    static String text;

    public PdfViewer() {
        initial();
    }

    private void initial() {
        setLayout(new BorderLayout(0, 0));
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(topPanel, BorderLayout.NORTH);
        btnFirstPage = createButton("|<<");
        topPanel.add(btnFirstPage);
        btnPreviousPage = createButton("<<");
        topPanel.add(btnPreviousPage);
        txtGoPage = new JTextField(10);
        txtGoPage.setHorizontalAlignment(JTextField.CENTER);
        topPanel.add(txtGoPage);
        btnNextPage = createButton(">>");
        topPanel.add(btnNextPage);
        btnLastPage = createButton(">>|");
        topPanel.add(btnLastPage);

        search = new JButton("search");
        topPanel.add(search);
        JScrollPane scrollPane = new JScrollPane();
        add(scrollPane, BorderLayout.CENTER);
        JPanel viewPanel = new JPanel(new BorderLayout(0, 0));
        scrollPane.setViewportView(viewPanel);

        pagePanel = new PagePanel();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        pagePanel.setPreferredSize(screenSize);
        viewPanel.add(pagePanel, BorderLayout.CENTER);

        // disableAllNavigationButton();

        search.addActionListener(new Action1());
    }

    private JButton createButton(String string) {
        return new JButton(string);
    }

    class Action1 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JDialog jDialog = new JDialog(SwingUtilities.getWindowAncestor(search));
            Label label = new Label("Search: ");
            final JTextField jTextField = new JTextField(10);
            jTextField.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    // Here perform search in PDF
            text = jTextField.getText();
            search(pdfFile,text);
                    System.out.println("Search for text " + jTextField.getText() + " requested");
                }
            });
            // If you want to react to every change of text in the textfield, you can
            // use a DocumentListener and invoke the search method for all events.
            jTextField.getDocument().addDocumentListener(new DocumentListener() {

                @Override
                public void removeUpdate(DocumentEvent e) {

                }

                @Override
                public void insertUpdate(DocumentEvent e) {

                }

                @Override
                public void changedUpdate(DocumentEvent e) {

                }
            });
            JPanel panel = new JPanel();
            jDialog.add(panel);
            panel.add(label);
            panel.add(jTextField);
            jDialog.pack();
            jDialog.setLocationRelativeTo(search);
            jDialog.setVisible(true);
        }
    }

    public static void search(PDFFile pdffile2,String text) {
                
                System.out.println("Inside searh" +text);
                System.out.println("Inside Search Page ::::::::" + pdffile2.getNumPages()); //43 pages
                for (int i = 0;i <= pdffile2.getNumPages(); i++) {
                PDFPage pdfPage = pdffile2.getPage(i);
                if(pdfPage.equals(text)) {
                    System.out.println("equal");
                }
                else{
                    System.out.println("Not Equal");
                }
                    
                }
            }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new PdfViewer());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
4

1 に答える 1

1

ここにそれを行う1つの方法があります。

  1. ActionListenerテキストフィールドにを追加します (actionPerformedユーザーが「Enter」を押すたびに呼び出されます)
  2. テキストフィールドに a を追加しDocumentListenerます (テキストフィールドのテキストが変更されるたびに DocumentListener のメソッドが呼び出されます)

クラスに search メソッドを実装する必要があります。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.google.common.base.CharMatcher;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PagePanel;

public class PdfViewer extends JPanel {
    private static enum Navigation {
        GO_FIRST_PAGE, FORWARD, BACKWARD, GO_LAST_PAGE, GO_N_PAGE
    }

    private static final CharMatcher POSITIVE_DIGITAL = CharMatcher.anyOf("0123456789");
    private static final String GO_PAGE_TEMPLATE = "%s of %s";
    private static final int FIRST_PAGE = 1;
    private int currentPage = FIRST_PAGE;
    private JButton btnFirstPage;
    private JButton btnPreviousPage;
    private JTextField txtGoPage;
    private JButton btnNextPage;
    private JButton btnLastPage;
    private JButton print;
    private JButton search;
    private PagePanel pagePanel;
    private PDFFile pdfFile;

    public PdfViewer() {
        initial();
    }

    private void initial() {
        setLayout(new BorderLayout(0, 0));
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(topPanel, BorderLayout.NORTH);
        btnFirstPage = createButton("|<<");
        topPanel.add(btnFirstPage);
        btnPreviousPage = createButton("<<");
        topPanel.add(btnPreviousPage);
        txtGoPage = new JTextField(10);
        txtGoPage.setHorizontalAlignment(JTextField.CENTER);
        topPanel.add(txtGoPage);
        btnNextPage = createButton(">>");
        topPanel.add(btnNextPage);
        btnLastPage = createButton(">>|");
        topPanel.add(btnLastPage);

        search = new JButton("search");
        topPanel.add(search);
        JScrollPane scrollPane = new JScrollPane();
        add(scrollPane, BorderLayout.CENTER);
        JPanel viewPanel = new JPanel(new BorderLayout(0, 0));
        scrollPane.setViewportView(viewPanel);

        pagePanel = new PagePanel();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        pagePanel.setPreferredSize(screenSize);
        viewPanel.add(pagePanel, BorderLayout.CENTER);

        // disableAllNavigationButton();

        search.addActionListener(new Action1());
    }

    private JButton createButton(String string) {
        return new JButton(string);
    }

    class Action1 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JDialog jDialog = new JDialog(SwingUtilities.getWindowAncestor(search));
            Label label = new Label("Search: ");
            final JTextField jTextField = new JTextField(10);
            jTextField.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    // Here perform search in PDF
                    System.out.println("Search for text " + jTextField.getText() + " requested");
                }
            });
            // If you want to react to every change of text in the textfield, you can
            // use a DocumentListener and invoke the search method for all events.
            jTextField.getDocument().addDocumentListener(new DocumentListener() {

                @Override
                public void removeUpdate(DocumentEvent e) {

                }

                @Override
                public void insertUpdate(DocumentEvent e) {

                }

                @Override
                public void changedUpdate(DocumentEvent e) {

                }
            });
            JPanel panel = new JPanel();
            jDialog.add(panel);
            panel.add(label);
            panel.add(jTextField);
            jDialog.pack();
            jDialog.setLocationRelativeTo(search);
            jDialog.setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new PdfViewer());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
于 2013-01-29T12:37:00.153 に答える