Mình đang muốn viết 1 app Java để lấy 1 đoạn text trong 1 file PDF dạng Babilon Dict, hay Lạc Việt.
Có ai đã dùng thư viện hay API nào rồi thì có thể cho mình biết hướng tìm hiểu với.
Nếu được có thể cho mình gặp để tham khảo ý kiến luôn.
bên .NET có thư viện iTextSharp được rất nhều người dùng. Có phiên bản cho Java nằm ở http://itextpdf.com/
Tình hình là nghe bạn hỏi mình thử đi kiếm và tìm được cái API xài khá tốt ở trang pdfBox
Đoạn chương trình này mình lấy thử chuyển file .pdf về file .txt
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class Program {
/**
* @param args
*/
public static void main(String[] args) {
String fileName = "";
Scanner scan = new Scanner(System.in);
File file = new File(fileName);
System.out.print("Nhap vao ten file: ");
while(!file.exists()){
fileName = scan.next();
file = new File(fileName);
if(!file.exists())
System.err.println("Khong tim thay file");
}
FileInputStream inStream = null;
PDFParser parser = null;
PDDocument pDoc = null;
PDFTextStripper stripper = null;
String text = "";
FileOutputStream outStream = null;
try{
inStream = new FileInputStream(file);
parser = new PDFParser(inStream);
}
catch(Exception ex){
}
try{
if(parser != null)
{
parser.parse();
pDoc = parser.getPDDocument();
stripper = new PDFTextStripper();
text = stripper.getText(pDoc);
outStream = new FileOutputStream(fileName.replace(".pdf", ".txt"), false);
outStream.write(text.getBytes());
pDoc.close();
outStream.close();
System.out.println("Xong");
}
}
catch(Exception ex){
System.err.println("Co loi xay ra");
}
}
}
P.S: khi sử dụng có thể xảy ra lỗi nếu bạn không add đầy đủ các file .jar cần thiết(đoạn chương trình trên cần một file pdfBox và file Font gì đó
)
Hi vọng giúp được bạn, chúc bạn học tốt!
hihi. Cảm ơn các bạn nhiều!!
Cảm ơn nhiều…