我一直在尝试加快从PDF文件中提取文本。我试过了SnowTide的PDFTextStream
。它在我的电脑上比iText慢。
我最近的尝试是使用多个线程。
我使用字节数组而不是文件名初始化PdfReader
。
这样我就可以通过将所有内容都拉入内存来避免IO延迟。据我所知,iText可以根据需要从文件中获取数据。使用字节数组时,它不应该有这个问题。
我使用线程提取多个页面范围。
然而,这使它变得更慢。使用一个线程与五个线程更快。我不知道为什么会这样。据我所知,瓶颈正在提取页面本身PdfTextExtractor.getTextFromPage(mReader, i)
。我定时使用for循环来连接线程的文本,这不会影响整个时间。
我的问题是,为什么它会变慢,我怎样才能让它变得更快?
访问同一个PdfReader
是否会造成瓶颈?
我应该使用相同的字节数组初始化多个PdfReader
吗?
我的任何研究都错了吗?
如果有人对全新的开源PDF提取器或其他建议有任何建议,那就太棒了!
import java.io.*;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.PdfReader;
import java.nio.channels.FileChannel;
import java.nio.MappedByteBuffer;
public class PDFParser
{
private PdfReader mReader;
private int numPages;
private String bodyText;
public PDFParser(String pdf)
throws IOException
{
/*
scrapped this
mReader = new PdfReader(pdf);
and replaced with the following.
From what I can tell Pdfreader with the above constructor
probably uses a stream to get the pages one at a time.
I want to load it all into memory to increase the speed.
*/
final FileChannel channel = new FileInputStream(pdf).getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
byte[] byteArray = new byte[buffer.remaining()];
buffer.get(byteArray);
channel.close();
mReader = new PdfReader(byteArray);
numPages = mReader.getNumberOfPages();
bodyText = "";
}
public String parse()
{
int numThreads = 5;
int offset = (numPages / numThreads) - 1;
int currentFirstPage = 1;
ParserThread[] threads = new ParserThread[numThreads];
int forEnd = numThreads - 1;
for (int i = 0; i < forEnd; i++)
{
threads[i] = new ParserThread(mReader, currentFirstPage, currentFirstPage + offset);
threads[i].start();
currentFirstPage += offset + 1;
}
threads[forEnd] = new ParserThread(mReader, currentFirstPage, numPages);
threads[forEnd].start();
try
{
long before = System.currentTimeMillis();
for(int i = 0; i < numThreads; i++)
threads[i].join();
System.out.println(System.currentTimeMillis() - before);
}
catch(InterruptedException ie){ie.printStackTrace();}
mReader.close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numThreads;i++)
{
sb.append(threads[i].getText());
}
bodyText = sb.toString();
if (bodyText != "")
return bodyText;
else
return "error";
}
public static void main(final String args[]) throws IOException
{
for (int i = 0; i < args.length; i++)
{
String text = new PDFParser(args[i]).parse();
// System.out.println(text);
}
}
}
class ParserThread extends Thread
{
private PdfReader mReader;
private int mFirstPage;
private int mLastPage;
private String text;
ParserThread(PdfReader reader, int firstPage, int lastPage)
{
mReader = reader;
mFirstPage = firstPage;
mLastPage = lastPage;
text = "";
}
@Override
public void run()
{
StringBuilder sb = new StringBuilder();
try
{
for (int i = mFirstPage; i <= mLastPage; i++)
sb.append(PdfTextExtractor.getTextFromPage(mReader, i));
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
text = sb.toString();
}
public String getText()
{
return text;
}
}