java pdf test

package org.stock;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;

public class PdfTest {

private static String SIGN = null;

public static int preIndex(String s, int start, String sFind, int line) {
int position = start;
for (int i = 0; i < line; i++) {
position = s.substring(0, position).lastIndexOf(sFind);
}
return position;
}

public static int nextIndex(String s, int start, String sFind, int line) {
int position = start;
for (int i = 0; i < line; i++) {
position = s.indexOf(sFind, start);
}
return position;
}

public static void displayOneLine(String[] lines) throws InterruptedException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int str = 0;
int start = 0;
int end = lines.length;
while (true) {
try {
str = br.read();
if (str == 106) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
for (int j = 0; j < 5; j++) {
System.out.println(lines[start]);
start++;
if (start > end) {
start = 0;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void CaptureSign(String[] lines) throws InterruptedException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
SIGN = br.readLine();
if ("q".equals(SIGN)) {
break;
}
}
}

public static void displayAuto(String[] lines) throws InterruptedException {
int start = 0;
int end = lines.length;
String fileName="c:/test/line.txt";
start = getLastLine(fileName);
while (true) {
if ("p".equals(SIGN)) {
Thread.sleep(1000);
continue;
}
if ("q".equals(SIGN)) {
break;
}
try {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} catch (IOException e) {
e.printStackTrace();
}
//System.out.printf("%250s\n",lines[start]);
String s = lines[start];
while (true) {
System.out.printf("%250s\n",s.length() <= 5?s:s.substring(0,4));
Thread.sleep(5000);
if (s.length() <= 5) {
break;
}
s = s.substring(4);
}
start++;
if (start > end) {
start = 0;
}
saveLine(fileName,(int)start);
}
}

public static Integer getLastLine(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
return Integer.parseInt(tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return 1;
}

public static Integer saveLine(String fileName,Integer line) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(fileName));
bw.write(line.toString());
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e1) {
}
}
}
return 1;
}

public static void main(String[] args) throws IOException {

try (PDDocument document = PDDocument.load(new File("c:/test/d_to_m.pdf"))) {

document.getClass();

if (!document.isEncrypted()) {

PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);

PDFTextStripper tStripper = new PDFTextStripper();

String pdfFileInText = tStripper.getText(document);
// System.out.println("Text:" + pdfFileInText);
String lines[] = pdfFileInText.split("\\r?\\n");
//displayOneLine(lines);
//displayAuto(lines);
Thread t = new Thread() {
@Override
public void run() {
try {
displayAuto(lines);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
CaptureSign(lines);

/*
* Pattern p = Pattern.compile(search); Matcher m = p.matcher(pdfFileInText);
* StringBuffer sb = new StringBuffer(); int i = 0; boolean result = m.find();
* // 使用循环将句子里所有的kelvin找出并替换再将内容加到sb里 while (result) { i++;
* System.out.println("第" + i + "次内容是:");
* System.out.println(pdfFileInText.substring(preIndex(pdfFileInText, m.start(),
* "\n", 3), nextIndex(pdfFileInText, m.start(), "\n", 3)));
*
* result = m.find(); } m.appendTail(sb);
*/

// System.out.println("调用m.appendTail(sb)后sb的最终内容是:"+ sb.toString());
// p.split(pdfFileInText)

// split by whitespace
/*
* String lines[] = pdfFileInText.split("\\r?\\n"); for (String line : lines) {
* System.out.println(line); }
*/

}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

posted @ 2021-07-21 10:39  涵旭  阅读(57)  评论(0编辑  收藏  举报