@@ -0,0 +1,3687 @@
+/*%0A * Licensed to the Apache Software Foundation (ASF) under one or more%0A * contributor license agreements.  See the NOTICE file distributed with%0A * this work for additional information regarding copyright ownership.%0A * The ASF licenses this file to You under the Apache License, Version 2.0%0A * (the %22License%22); you may not use this file except in compliance with%0A * the License.  You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage org.apache.pdfbox.examples.util;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport org.apache.pdfbox.pdmodel.PDDocument;%0Aimport org.apache.pdfbox.pdmodel.encryption.AccessPermission;%0Aimport org.apache.pdfbox.text.PDFTextStripper;%0A%0A/**%0A * This is a simple text extraction example to get started. For more advance usage, see the%0A * ExtractTextByArea and the DrawPrintTextLocations examples in this subproject, as well as the%0A * ExtractText tool in the tools subproject.%0A *%0A * @author Tilman Hausherr%0A */%0Apublic class ExtractTextSimple%0A%7B%0A%0A    /**%0A     * This will print the documents text page by page.%0A     *%0A     * @param args The command line arguments.%0A     *%0A     * @throws IOException If there is an error parsing or extracting the document.%0A     */%0A    public static void main(String%5B%5D args) throws IOException%0A    %7B%0A        if (args.length != 1)%0A        %7B%0A            usage();%0A        %7D%0A%0A        try (PDDocument document = PDDocument.load(new File(args%5B0%5D)))%0A        %7B%0A            AccessPermission ap = document.getCurrentAccessPermission();%0A            if (!ap.canExtractContent())%0A            %7B%0A                throw new IOException(%22You do not have permission to extract text%22);%0A            %7D%0A%0A            PDFTextStripper stripper = new PDFTextStripper();%0A%0A            // This example uses sorting, but in some cases it is more useful to switch it off,%0A            // e.g. in some files with columns where the PDF content stream respects the%0A            // column order.%0A            stripper.setSortByPosition(true);%0A%0A            for (int p = 1; p %3C= document.getNumberOfPages(); ++p)%0A            %7B%0A                // Set the page interval to extract. If you don't, then all pages would be extracted.%0A                stripper.setStartPage(p);%0A                stripper.setEndPage(p);%0A%0A                // let the magic happen%0A                String text = stripper.getText(document);%0A%0A                // do some nice output with a header%0A                String pageStr = String.format(%22page %25d:%22, p);%0A                System.out.println(pageStr);%0A                for (int i = 0; i %3C pageStr.length(); ++i)%0A                %7B%0A                    System.out.print(%22-%22);%0A                %7D%0A                System.out.println();%0A                System.out.println(text.trim());%0A                System.out.println();%0A%0A                // If the extracted text is empty or gibberish, please try extracting text%0A                // with Adobe Reader first before asking for help. Also read the FAQ%0A                // on the website: %0A                // https://pdfbox.apache.org/2.0/faq.html#text-extraction%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * This will print the usage for this document.%0A     */%0A    private static void usage()%0A    %7B%0A        System.err.println(%22Usage: java %22 + ExtractTextSimple.class.getName() + %22 %3Cinput-pdf%3E%22);%0A        System.exit(-1);%0A    %7D%0A%7D%0A

Create simple text extraction example