我正在使用 apache tika 从 pdf 文件中抓取内容。抓取的内容(文本)还包含页眉和页脚。我的要求是获取没有页眉和页脚的文本。下面是我抓取内容的示例代码。示例代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.io.FileUtils;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.sax.BodyContentHandler;
import org.json.simple.JSONObject;
public class test {
public static void main(String[] args) throws Exception {
String file = "C://Sample.pdf";
File file1 = new File(file);
InputStream input = new FileInputStream(file1);
Metadata metadata = new Metadata();
BodyContentHandler handler = new BodyContentHandler(
10 * 1024 * 1024);
AutoDetectParser parser = new AutoDetectParser();
parser.parse(input, handler, metadata);
String path = "C://AUG7th".concat("/").concat(file1.getName())
.concat(".txt");
String content = handler.toString();
File file2 = new File(path);
FileWriter fw = new FileWriter(file2.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
}
}
如何做到这一点请建议我。谢谢