Java is absolutely unknown for me.
I want to use a tool called PDFBox to extract text from a certain area using PHP.
The default jar won't allow me to select a certain area to extract text so I decided to take the ExtractTextByArea.java and make a .jar out of it, where I can input my own parameters in PHP to get the desired text.
This is the .java from the site:
package org.apache.pdfbox.examples.util;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
/**
* This is an example on how to extract text from a specific area on the PDF document.
*
* @author Ben Litchfield
*/
public final class ExtractTextByArea
{
private ExtractTextByArea()
{
//utility class and should not be constructed.
}
/**
* This will print the documents text in a certain area.
*
* @param args The command line arguments.
*
* @throws IOException If there is an error parsing the document.
*/
public static void main( String[] args ) throws IOException
{
if( args.length != 1 )
{
usage();
}
else
{
PDDocument document = null;
try
{
document = PDDocument.load( new File(args[0]) );
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition( true );
Rectangle rect = new Rectangle( 10, 280, 275, 60 );
stripper.addRegion( "class1", rect );
PDPage firstPage = document.getPage(0);
stripper.extractRegions( firstPage );
System.out.println( "Text in the area:" + rect );
System.out.println( stripper.getTextForRegion( "class1" ) );
}
finally
{
if( document != null )
{
document.close();
}
}
}
}
/**
* This will print the usage for this document.
*/
private static void usage()
{
System.err.println( "Usage: java " + ExtractTextByArea.class.getName() + " <input-pdf>" );
}
}
I made a .jar out of it but when I execute it, the CMD says:
Error: main class not found
I don't know how to keep going.
Use java -cp ExtractTextByArea.jar org.apache.pdfbox.examples.util.ExtractTextByArea
If the class is not in a package then simply java -cp ExtractTextByArea.jar ExtractTextByArea
If you are not within the directory where ExtractTextByArea.jar
is located, then you can do:
On Unix or Linux platforms:
java -cp /location_of_jar/ExtractTextByArea.jar org.apache.pdfbox.examples.util.ExtractTextByArea
On Windows:
java -cp [D]:\location_of_jar\org.apache.pdfbox.examples.util..jar org.apache.pdfbox.examples.util.ExtractTextByArea
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.