Giống như Theodore nói rằng bạn có thể trích xuất văn bản từ pdf và như Chris chỉ ra
miễn là nó thực sự là văn bản (không phác thảo hoặc bitmap)
nhất điều cần làm là mua Cuốn sách Itext của Bruno Lowagie đã hành động. Trong ấn bản thứ hai chương 15 bao gồm việc trích xuất văn bản.
Nhưng bạn có thể xem trang web của mình để biết ví dụ. http://itextpdf.com/examples/iia.php?id=279
Và bạn có thể phân tích cú pháp để tạo tệp txt thuần túy. Dưới đây là một ví dụ mã:
/*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part4.chapter15;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
public class ExtractPageContent {
/** The original PDF that will be parsed. */
public static final String PREFACE = "resources/pdfs/preface.pdf";
/** The resulting text file. */
public static final String RESULT = "results/part4/chapter15/preface.txt";
/**
* Parses a PDF to a plain text file.
* @param pdf the original PDF
* @param txt the resulting text
* @throws IOException
*/
public void parsePdf(String pdf, String txt) throws IOException {
PdfReader reader = new PdfReader(pdf);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
PrintWriter out = new PrintWriter(new FileOutputStream(txt));
TextExtractionStrategy strategy;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
out.println(strategy.getResultantText());
}
reader.close();
out.flush();
out.close();
}
/**
* Main method.
* @param args no arguments needed
* @throws IOException
*/
public static void main(String[] args) throws IOException {
new ExtractPageContent().parsePdf(PREFACE, RESULT);
}
}
Thông báo giấy phép
Ví dụ này chỉ làm việc với phiên bản AGPL của iText.
Nếu bạn xem các ví dụ khác, nó sẽ cho thấy cách bỏ các phần của văn bản hoặc cách trích xuất các phần của pdf.
Hy vọng điều đó sẽ hữu ích.
Nguồn
2012-07-03 19:00:59
Tôi không hoàn toàn rõ ràng về những gì bạn đang làm. Văn bản đọc và trích xuất văn bản thường giống nhau. iText sẽ không lưu văn bản vào tệp cho bạn nhưng một khi bạn có văn bản, bạn sẽ có thể thực hiện điều đó một cách dễ dàng. iText thực hiện một công việc tuyệt vời khi trích xuất văn bản miễn là nó thực sự là văn bản (không phải phác họa hoặc bitmap). Khi tìm kiếm trang này cũng tìm kiếm 'iTextSharp' là cổng .Net của iText. Nó có nhiều câu hỏi/câu trả lời và mã gần như hoàn toàn giống với C#. –