我正在尝试使用iText API将10个PDF文件(每个约1000页)合并为一个文件。以下是合并的代码。在8或9个文件之后,它正在打破OOM错误。如何将此最大化为100个文件,因为这是我想要的结果。
public class IText {
public static void main(String[] args) throws IOException, DocumentException{
String ifs2 = "C:\\Downloads\\02.pdf";
String result = "C:\\Merge\\final.pdf";
String[] stArray = new String[10];
for(int i = 0; i<10; i++){
stArray[i]=ifs2;
}
mergeFiles(stArray,result, false);
}
public static void mergeFiles(String[] files, String result, boolean smart) throws IOException, DocumentException {
Document document = new Document();
PdfCopy copy;
if (smart)
copy = new PdfSmartCopy(document, new FileOutputStream(result));
else
copy = new PdfCopy(document, new FileOutputStream(result));
document.open();
for (int i = 0; i < files.length; i++) {
PdfReader reader = new PdfReader(files[i]);
copy.addDocument(reader);
reader.close();
}
document.close();
}
更新:我在for循环中尝试了以下方法。仍然得到错误。
for (int i = 0; i < files.length; i++) {
RandomAccessSourceFactory rasf = new RandomAccessSourceFactory();
RandomAccessSource ras = rasf.createBestSource(files[i]);
RandomAccessFileOrArray raf = new RandomAccessFileOrArray(ras);
PdfReader reader = new PdfReader(raf, null);
copy.addDocument(reader);
raf.close();
reader.close();
System.out.println(i);
}