editorframework.PDDocumentAdapter.java Source code

Java tutorial

Introduction

Here is the source code for editorframework.PDDocumentAdapter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package editorframework;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import org.apache.pdfbox.pdfviewer.PDFPagePanel;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

/**
 *
 * @author Mrcio
 */
public class PDDocumentAdapter {

    public PDDocumentAdapter() {
    }

    public PDDocumentAdapter(String fileName) {
        try {

            this.pdPanel = new PDFPagePanel();
            pdDocument = PDDocument.load(fileName);

            allPages = pdDocument.getDocumentCatalog().getAllPages();
        } catch (IOException ex) {
            Logger.getLogger(PDDocumentAdapter.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public boolean open(String fileName) {

        try {
            pdDocument = PDDocument.load(new FileInputStream(fileName));

            allPages = pdDocument.getDocumentCatalog().getAllPages();
            return true;
        } catch (IOException ex) {
            Logger.getLogger(PDDocumentAdapter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public void close() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    public boolean save() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public JComponent getPage(int numberPage) {

        pdPanel.setPage(allPages.get(numberPage));
        return pdPanel;

    }

    public List<JComponent> getPages() {

        List<JComponent> list = new LinkedList<>();
        for (PDPage page : allPages) {

            try {
                PDFPagePanel panel = new PDFPagePanel();
                panel.setPage(page);
                list.add(panel);
            } catch (IOException ex) {
                Logger.getLogger(PDDocumentAdapter.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        return list;

    }

    public int getNumberPages() {
        return pdDocument.getNumberOfPages();
    }

    private PDDocument pdDocument;
    private List<PDPage> allPages;
    private PDFPagePanel pdPanel;

}