Get pdf pages and get text from pdf - Java PDF

Java examples for PDF:Page

Description

Get pdf pages and get text from pdf

Demo Code



import java.io.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.util.*;

public class PDFTest {

    public static void main(String[] args) {
        PDDocument pd;//from w  ww .j  ava 2s.  com
        BufferedWriter wr;
        try {
            File input = new File("pdfbox/pdf/extracting2/link.pdf"); 
            File output = new File("pdfbox/pdf/extracting2/link.txt");
            // data
            pd = PDDocument.load(input);

            System.out.println(pd.getNumberOfPages());
            System.out.println(pd.isEncrypted());

            pd.save("pdfbox/pdf/extracting2/CopyOfLink.pdf"); 

            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setStartPage(1); // Start extracting from page 3
            stripper.setEndPage(1); // Extract till page 5

            wr = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(output)));

            stripper.writeText(pd, wr);

            if (pd != null) {
                pd.close();
            }
            // I use close() to flush the stream.
            wr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials